stream 中的groupingBy 和partitioningBy

package test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class Test {
	public static void main(String[] args) {

		List<Person> list = new ArrayList();
		list.add(new Person(1, "haha"));
		list.add(new Person(1, "rere"));
		list.add(new Person(2, "fefe"));

		Map<Integer,List<Person>> ss=list.stream()
	         .collect(Collectors.groupingBy(Person::getId));

		System.out.println(ss);
		
		List<Person> aa=ss.get(1);
		System.out.println(aa);
		
		//方式二:
		Map<Boolean,List<Person>> gg=list.stream()
				.collect(Collectors.partitioningBy(p->p.getId()==1));
		
		System.out.println(gg);
		
		List<Person> ff=gg.get(true);
		System.out.println(ff);
		
		
	}

}

得到结果:

{1=[test.Person@14c265e, test.Person@8139f0], 2=[test.Person@ca494b]}
[test.Person@14c265e, test.Person@8139f0]
{false=[test.Person@ca494b], true=[test.Person@14c265e, test.Person@8139f0]}
[test.Person@14c265e, test.Person@8139f0]

    原文作者:java编程-IT
    原文地址: https://blog.csdn.net/jiangpingjiangping/article/details/76474207
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞