我目前正在努力进行以下练习:
Given a
Stream<String>
collect (usingCollector
) a Collection of allString
s with maximum lenght.
这是我尝试过的:
private static class MaxStringLenghtCollector
implements Collector<String, List<String>, List<String>> {
@Override
public Supplier<List<String>> supplier() {
return LinkedList::new;
}
@Override
public BiConsumer<List<String>, String> accumulator() {
return (lst, str) -> {
if(lst.isEmpty() || lst.get(0).length() == str.length())
lst.add(str);
else if(lst.get(0).length() < str.length()){
lst.clear();
lst.add(str);
}
};
}
@Override
public BinaryOperator<List<String>> combiner() {
return (lst1, lst2) -> {
lst1.addAll(lst2);
return lst1;
};
}
@Override
public Function<List<String>, List<String>> finisher() {
return Function.identity();
}
@Override
public Set<java.util.stream.Collector.Characteristics> characteristics() {
return EnumSet.of(Characteristics.IDENTITY_FINISH);
}
}
所以我写了我的自定义收集器来完成这项工作,但是......它看起来确实很丑。也许有一些标准方法可以做到这一点。例如,我会尝试分组收集器:
public static Collection<String> allLongest(Stream<String> str){
Map<Integer, List<String>> groups = str.collect(Collectors.groupingBy(String::length));
return groups.get(groups.keySet()
.stream()
.mapToInt(x -> x.intValue())
.max()
.getAsInt());
}
但这既丑陋又低效。首先,我们构建一个Map
,然后遍历它来构建 Set
然后遍历它得到max-List
.
最佳答案
我会这样做:
List<String> values = Arrays.asList("abc", "ab", "bc", "bcd", "a");
// I group by length and put it into a TreeMap then get the max value
values.stream().collect(groupingBy(String::length, TreeMap::new, toList()))
.lastEntry()
.getValue()
.forEach(System.out::println);
输出:
abc
bcd
关于java - 收集具有最大长度的所有字符串的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37417358/