找到你要的答案

Q:Counting all elements in map of string to list

Q:计算字符串列表中所有元素的列表

Is there a better way to calculate size of all lists in map? So firstList.size() + secondList.size()? I have done this, but creating a list just for counting doesn't feel right approach ..

public static void main(String[] args) {
    List<String> firstList = new ArrayList<>();
    firstList.add("first");
    List<String> secondList = new ArrayList<>();
    secondList.add("second");

    Map<String, List<String>> myMap = new HashMap<>();
    myMap.put("one", firstList);
    myMap.put("two", secondList);

    int size = myMap.entrySet().stream()
                               .map(entry -> entry.getValue())
                               .flatMap(list -> list.stream())
                               .collect(Collectors.toList())
                               .size();
    System.out.println("Size of all elements in map is:" + size);
}

Is there a better way to calculate size of all lists in map? So firstList.size() + secondList.size()? I have done this, but creating a list just for counting doesn't feel right approach ..

public static void main(String[] args) {
    List<String> firstList = new ArrayList<>();
    firstList.add("first");
    List<String> secondList = new ArrayList<>();
    secondList.add("second");

    Map<String, List<String>> myMap = new HashMap<>();
    myMap.put("one", firstList);
    myMap.put("two", secondList);

    int size = myMap.entrySet().stream()
                               .map(entry -> entry.getValue())
                               .flatMap(list -> list.stream())
                               .collect(Collectors.toList())
                               .size();
    System.out.println("Size of all elements in map is:" + size);
}
answer1: 回答1:

This is at least more readable

int size = myMap.values().stream()
     .mapToInt(Collection::size)
     .sum();

or if you want to look like a functional programming guru, use:

int size2 = myMap.values().stream()
     .reduce(0, (acc, val) -> acc + val.size(), Integer::sum);

EDIT: using the .values() :]

这至少可读性更强

int size = myMap.values().stream()
     .mapToInt(Collection::size)
     .sum();

或者如果你想看起来像一个功能编程大师,使用:

int size2 = myMap.values().stream()
     .reduce(0, (acc, val) -> acc + val.size(), Integer::sum);

EDIT: using the .values() :]

answer2: 回答2:

Both of the previous answers are good, but can be shortened a bit more by going straight for the values(), rather than iterating the entrySet():

// Streaming version
int size = myMap.values().stream()
     .mapToInt(list -> list.size())
     .sum();

// Standard loop version
int size = 0;
for (List<String> list : myMap.values())
    size += list.size();

既有对前人的答案是好的,但可以缩短更为values()走直线,而不是迭代entryset():

// Streaming version
int size = myMap.values().stream()
     .mapToInt(list -> list.size())
     .sum();

// Standard loop version
int size = 0;
for (List<String> list : myMap.values())
    size += list.size();
answer3: 回答3:

Another way:

private static int size = 0;

public static void main(String[] args) {
    List<String> firstList = new ArrayList<>();
    firstList.add("first");
    List<String> secondList = new ArrayList<>();
    secondList.add("second");

    Map<String, List<String>> myMap = new HashMap<>();
    myMap.put("one", firstList);
    myMap.put("two", secondList);

    myMap.values().forEach(value -> {size += value.size();});
    System.out.println("Size of all elements in map is:" + size); 
}

另一种方式:

private static int size = 0;

public static void main(String[] args) {
    List<String> firstList = new ArrayList<>();
    firstList.add("first");
    List<String> secondList = new ArrayList<>();
    secondList.add("second");

    Map<String, List<String>> myMap = new HashMap<>();
    myMap.put("one", firstList);
    myMap.put("two", secondList);

    myMap.values().forEach(value -> {size += value.size();});
    System.out.println("Size of all elements in map is:" + size); 
}
java  java-8  java-stream