how to transform Map to List using google collections

后端 未结 4 2059
抹茶落季
抹茶落季 2021-02-14 02:01

I have a map with strings, I want to transform it to a list of strings with \" \" as a key value separator. Is it possible using google collections?

Code example that I

相关标签:
4条回答
  • 2021-02-14 02:16

    Here you go:

    private static final Joiner JOINER = Joiner.on(' ');
    public List<String> mapToList(final Map<String, String> input){
        return Lists.newArrayList(
            Iterables.transform(
                input.entrySet(), new Function<Map.Entry<String, String>, String>(){
                    @Override
                    public String apply(final Map.Entry<String, String> input){
                        return JOINER.join(input.getKey(), input.getValue());
                    }
                }));
    }
    

    Update: optimized code. Using a Joiner constant should be much faster than String.concat()


    These days, I would of course do this with Java 8 streams. No external lib needed.

    public List<String> mapToList(final Map<String, String> input) {
        return input.entrySet()
                    .stream()
                    .map(e -> new StringBuilder(
                                 e.getKey().length()
                                         + e.getValue().length()
                                         + 1
                         ).append(e.getKey())
                          .append(' ')
                          .append(e.getValue())
                          .toString()
                    )
                    .collect(Collectors.toList());
    }
    
    0 讨论(0)
  • 2021-02-14 02:25

    Here's a functional approach using Java 8 streams:

    List<String> kv = map.entrySet().stream()
        .map(e -> e.getKey() + " " + e.getValue()) //or String.format if you prefer
        .collect(Collectors.toList());
    

    If you're not wedded to the functional style, here's a more concise variant of the obvious for loop:

    List<String> kv = new ArrayList<>();
    map.forEach((k, v) -> kv.add(k + " " + v));
    
    0 讨论(0)
  • 2021-02-14 02:27

    Functional programming is cool, but in Java often adds complexity you really shouldn't be adding (as Java doesn't support it very well) I would suggest you use a simple loop which is much shorter, more efficient and eaiser to maintain and doesn't require an additional library to learn.

    public static List<String> mapToList(Map<String, String> env) {
        List<String> result = new ArrayList<String>();
        for (Entry<String, String> entry : env.entrySet())
            result.add(entry.getKey() + " " + entry.getValue());
        return result;
    }
    

    A simple test of code complexity it to count the number of symbols used. i.e. < ( , { = : . + @ Not counting close brackets.

    plain loop 22 symbols.
    functional approach 30 symbols.
    
    0 讨论(0)
  • 2021-02-14 02:35

    More Current solution

    public static void main(final String[] args)
    {
        final Map<String, String> m = ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3");
        final Collection<String> c = Maps.transformEntries(m, new Maps.EntryTransformer<String, String, String>()
        {
            @Override public String transformEntry(@Nullable final String key, @Nullable final String value)
            {
                return Joiner.on(' ').join(key, value);
            }
        }).values();
        System.out.println(c);
    }
    

    Outputs

    [k1 v1, k2 v2, k3 v3]
    
    0 讨论(0)
提交回复
热议问题