- 根据对象属性收集
static class Person {
String name;
int age;
int id;
Person(String name, int age) {
this.name = name;
this.age = age;
this.id =id;
}
//get/set方法。。。。
}
public static void main(String[] args) {
List<Person> persons =
Arrays.asList(
new Person("Max", 18,1),
new Person("Peter", 23,2),
new Person("Pamela", 23,3),
new Person("David", 12,4));
**//收集集合中的id**
List<int> ids = persons.stream().map(p -> p.getId()).collect(Collectors.toList());
//[1,2,3,4]
//过滤后收集 1
List<Person> persons = persons.stream().filter(p -> p.getAge()==23).collect(Collectors.toList());
//[{"Peter", 23,2},"Pamela", 23,3]
//过滤后收集 2
List<int> ids = persons
.stream()
.filter(p -> p.getAge() >= 18)
.map(p -> p.getId()).collect(Collectors.toList());
//[1,2,3]
//过滤后收集属性拼装 3
String names = persons
.stream()
.filter(p -> p.getAge() >= 18)
.map(p -> p.getName())
.collect(Collectors.joining(",", "In china ", " are funny name."));
//In china Max , Peter , Pamela are funny name.
}
- List转换map
//list根据属性分组转map
Map<Integer, List<Person>> personsByAge = persons
.stream()
.collect(Collectors.groupingBy(p -> p.getAge()));
personsByAge
.forEach((age, p) -> System.out.format("age %s: %s\n", age, p));
// age 18: [Max]
// age 23:[Peter, Pamela]
// age 12:[David]
//list 转 map(重复的年龄key,name拼在一起)
Map<Integer, String> map = persons
.stream()
.collect(Collectors.toMap(
p -> p.getAge(),
p -> p.getName(),
(name1, name2) -> name1 + ";" + name2));//key相同时保留原始值和当前值
System.out.println(map);
// {18=Max, 23=Peter;Pamela, 12=David}
//list转map
//需要注意的是:toMap 如果集合对象有重复的key,会报错Duplicate key ....
//可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留k1对应的value
Map<Integer, String> map = persons
.stream()
.collect(Collectors.toMap(
p -> p.age(),
p -> p.getName(),
(oldVal, currVal) -> currVal));// key相同时当前值替换原始值
System.out.println(map);
//{18=Max, 23=Pamela, 12=David}
- 求和、计算、排序
(1)排序
List<String> strings =
Arrays.asList("d2", "a2", "b1", "b3", "c");
strings.stream().sorted().forEach(System.out::print);
//a2,b1,b3,c,d2,D
(2) 求和
List<Person> persons =
Arrays.asList(
new Person("Max", 18),
new Person("Peter", 23),
new Person("Pamela", 23),
new Person("David", 12));
//求和年龄
Integer ageSum = persons.stream()
.reduce(0, (sum, p) -> sum += p.age, (sum1, sum2) -> sum1 + sum2);
//76
(3) 平均值
Double averageAge = persons
.stream()
.collect(Collectors.averagingInt(p -> p.age));
//19
Arrays.stream(new int[] {1, 2, 3})
.map(n -> 2 * n + 1)
.average()
.ifPresent(System.out::println);
//计算完后,求平均值 5.0
- 高效去除一个list中包含的另一个list
List<Long> list=new ArrayList<>();
List<Long> list1=Arrays.asList(1L,2L,3L,4L,5L,6L);
List<Long> list2=Arrays.asList(4L,5L,6L);
HashSet h1 = new HashSet(list1);
HashSet h2 = new HashSet(list2);
h1.removeAll(h2);
list.addAll(h1);
//打印结果为:[1, 2, 3]
System.out.println(list);
- 去重
// 根据id去重
List<Person> persons =
Arrays.asList(
new Person("Max", 18,1),
new Person("Peter", 23,2),
new Person("Pamela", 23,3),
new Person("Pamela", 23,3),
new Person("David", 12,4));
List<Person> unique = persons.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(comparingLong(Person::getId))), ArrayList::new)
);
总结:以上是本人开发过程中最常遇到的集合需要处理的情况,所以整理出来,方便自己开发的时候直接拿来使用,分享给大家!觉得有帮助的,点个赞,以资鼓励!