Fab*_*ndl 36 java grouping function list guava
我想分组列表的元素.我现在这样做:
public static <E> List<List<E>> group(final List<E> list, final GroupFunction<E> groupFunction) {
List<List<E>> result = Lists.newArrayList();
for (final E element : list) {
boolean groupFound = false;
for (final List<E> group : result) {
if (groupFunction.sameGroup(element, group.get(0))) {
group.add(element);
groupFound = true;
break;
}
}
if (! groupFound) {
List<E> newGroup = Lists.newArrayList();
newGroup.add(element);
result.add(newGroup);
}
}
return result;
}
public interface GroupFunction<E> {
public boolean sameGroup(final E element1, final E element2);
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点,最好是使用番石榴?
Xae*_*ess 71
当然可以,甚至更容易使用番石榴:)使用Multimaps.index(Iterable, Function)
:
ImmutableListMultimap<E, E> indexed = Multimaps.index(list, groupFunction);
Run Code Online (Sandbox Code Playgroud)
如果你给出具体的用例,就可以更容易地展示它.
来自docs的示例:
List<String> badGuys =
Arrays.asList("Inky", "Blinky", "Pinky", "Pinky", "Clyde");
Function<String, Integer> stringLengthFunction = ...;
Multimap<Integer, String> index =
Multimaps.index(badGuys, stringLengthFunction);
System.out.println(index);
Run Code Online (Sandbox Code Playgroud)
版画
{4=[Inky], 6=[Blinky], 5=[Pinky, Pinky, Clyde]}
Run Code Online (Sandbox Code Playgroud)
在您的情况下,如果GroupFunction定义为:
GroupFunction<String> groupFunction = new GroupFunction<String>() {
@Override public String sameGroup(final String s1, final String s2) {
return s1.length().equals(s2.length());
}
}
Run Code Online (Sandbox Code Playgroud)
那么它会转化为:
Function<String, Integer> stringLengthFunction = new Function<String, Integer>() {
@Override public Integer apply(final String s) {
return s.length();
}
}
Run Code Online (Sandbox Code Playgroud)
这是stringLengthFunction
Guava示例中使用的可能实现.
最后,在Java 8中,整个代码段可能更简单,因为lambas和方法引用足够简洁,可以内联:
ImmutableListMultimap<E, E> indexed = Multimaps.index(list, String::length);
Run Code Online (Sandbox Code Playgroud)
对于使用纯Java 8(没有Guava)的示例,Collector.groupingBy
请参阅Jeffrey Bosboom的答案,尽管该方法存在一些差异:
ImmutableListMultimap
,而是Map
与Collection
价值观,对返回的Map(源)的类型,可变性,可序列化或线程安全性没有任何保证,
编辑:如果您不关心索引键,您可以获取分组值:
List<List<E>> grouped = Lists.transform(indexed.keySet().asList(), new Function<E, List<E>>() {
@Override public List<E> apply(E key) {
return indexed.get(key);
}
});
// or the same view, but with Java 8 lambdas:
List<List<E>> grouped = Lists.transform(indexed.keySet().asList(), indexed::get);
Run Code Online (Sandbox Code Playgroud)
是什么让你Lists<List<E>>
查看哪些内容可以轻松复制到ArrayList
或只是按原样使用,如你想要的那样.还要注意的indexed.get(key)
是ImmutableList
.
// bonus: similar as above, but not a view, instead collecting to list using streams:
List<List<E>> grouped = indexed.keySet().stream()
.map(indexed::get)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
编辑2:正如Petr Gladkikh 在下面的评论中提到的,如果Collection<List<E>>
足够,上面的例子可能更简单:
Collection<List<E>> grouped = indexed.asMap().values();
Run Code Online (Sandbox Code Playgroud)
Jef*_*oom 10
Collector.groupingBy
来自Java 8流库提供与Guava相同的功能Multimaps.index
.以下是Xaerxess答案中的示例,重写为使用Java 8流:
List<String> badGuys = Arrays.asList("Inky", "Blinky", "Pinky", "Pinky", "Clyde");
Map<Integer, List<String>> index = badGuys.stream()
.collect(Collectors.groupingBy(String::length));
System.out.println(index);
Run Code Online (Sandbox Code Playgroud)
这将打印
{4=[Inky], 5=[Pinky, Pinky, Clyde], 6=[Blinky]}
Run Code Online (Sandbox Code Playgroud)
如果要以除创建列表之外的其他方式将值与相同的键组合,则可以使用groupingBy
另一个收集器的重载.此示例使用分隔符连接字符串:
Map<Integer, String> index = badGuys.stream()
.collect(Collectors.groupingBy(String::length, Collectors.joining(" and ")));
Run Code Online (Sandbox Code Playgroud)
这将打印
{4=Inky, 5=Pinky and Pinky and Clyde, 6=Blinky}
Run Code Online (Sandbox Code Playgroud)
如果您有一个大型列表或您的分组功能很昂贵,您可以并行使用parallelStream
和并发收集器.
Map<Integer, List<String>> index = badGuys.parallelStream()
.collect(Collectors.groupingByConcurrent(String::length));
Run Code Online (Sandbox Code Playgroud)
这可能会打印(订单不再是确定性的)
{4=[Inky], 5=[Pinky, Clyde, Pinky], 6=[Blinky]}
Run Code Online (Sandbox Code Playgroud)