Java8中的Lambda+Stream简化了我们对集合类的操作。
通过Lambda+Stream,可以很简洁地实现集合转换、去重、过滤、分组等操作。
以下为封装好的工具类:
package utils;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class CollectionKit {
private CollectionKit() {
}
public static List convertToList(Collection collection,
Function super T, ? extends R> mapper) {
if (collection == null || collection.isEmpty()) {
return new ArrayList<>();
}
return collection.stream().map(mapper).collect(Collectors.toList());
}
public static Set convertToSet(Collection collection,
Function super T, ? extends R> mapper) {
if (collection == null || collection.isEmpty()) {
return new HashSet<>();
}
return collection.stream().map(mapper).collect(Collectors.toSet());
}
public static Map convertToMap(Collection collection,
Function super T, ? extends K> keyMapper) {
if (collection == null || collection.isEmpty()) {
return new HashMap<>();
}
return collection.stream().collect(Collectors.toMap(keyMapper, Function.identity(), (key1, key2) -> key2));
}
public static Map convertToMap(Collection collection,
Function super T, ? extends K> keyMapper,
Function super T, ? extends V> valueMapper) {
if (collection == null || collection.isEmpty()) {
return new HashMap<>();
}
return collection.stream().collect(Collectors.toMap(keyMapper, valueMapper, (key1, key2) -> key2));
}
public static List filter(Collection collection,
Predicate super T> filterPredicate) {
if (collection == null || collection.isEmpty()) {
return new ArrayList<>();
}
return collection.stream().filter(filterPredicate).collect(Collectors.toList());
}
public static Map> group(Collection collection,
Function super T, ? extends K> classifier) {
if (collection == null || collection.isEmpty()) {
return new HashMap<>();
}
return collection.stream().collect(Collectors.groupingBy(classifier));
}
public static List distinct(Collection collection,
Comparator comparing) {
return collection.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(comparing)), ArrayList::new));
}
}
使用详解:
数据准备:
class User {
String name;
int age;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public User(String name, int age) {
this.name = name;
this.age = age;
}
}
List users = new ArrayList<>();
users.add(new User("张三", 10));
users.add(new User("李四", 10));
users.add(new User("王五", 20));
users.add(new User("张三", 25));
1、转换为新的list,每个用户转换为名字+年龄格式的字符串
Listlist = CollectionKit.convertToList(users, item -> item.name + "_" + item.age);
运行结果:[张三_10, 李四_10, 王五_20, 张三_25]
2、使用convertToSet得到所有用户的年龄(去除重复项)
Setset = CollectionKit.convertToSet(users, User::getAge);
运行结果:[20, 25, 10]
3、使用convertToMap转换为key=name,value=user格式的map
Mapmap = CollectionKit.convertToMap(users, User::getName);
运行结果:
{
李四=User{name='李四', age=10},
张三=User{name='张三', age=25},
王五=User{name='王五', age=20}
}
4、使用convertToMap转换为key=name,value=age格式的map
Mapmap1 = CollectionKit.convertToMap(users, User::getName, User::getAge);
运行结果:
{
李四=10,
张三=25,
王五=20
}
5、使用filter筛选年龄为10的用户
Listfilter = CollectionKit.filter(users, item -> item.age == 10);
运行结果:[User{name='张三', age=10}, User{name='李四', age=10}]
6、使用distinct根据名字或年龄去重
Listdistinct = CollectionKit.distinct(users, Comparator.comparing(o -> o.name)); List distinct1 = CollectionKit.distinct(users, Comparator.comparing(o -> o.age));
运行结果:
[User{name='张三', age=10}, User{name='李四', age=10}, User{name='王五', age=20}]
[User{name='张三', age=10}, User{name='王五', age=20}, User{name='张三', age=25}]
7、使用group根据年龄分组
Map> group = CollectionKit.group(users, User::getAge);
运行结果:
{
20=[User{name='王五', age=20}],
25=[User{name='张三', age=25}],
10=[User{name='张三', age=10}, User{name='李四', age=10}]
}


