对集合分组
Person p1 = new Person(1L, "a", 1, "aaaa");
Person p2 = new Person(2L, "a", 1, "bbbb");
Person p3 = new Person(3L, "b", 1, "bbbb");
Person p4 = new Person(4L, "b", 1, "aaaa");
Person p5 = new Person(5L, "c", 1, "bbbb");
List<Person> personList = Arrays.asList(p1, p2, p3, p4, p5);
//List<Person> personList = Arrays.asList();
Map<String, List<Person>> collect_adds = personList.stream().filter(p->p.getId()>3L).collect(Collectors.groupingBy(Person::getAdds));
collect_adds.forEach((adds, persons) -> {
System.out.println(MessageFormat.format("adds:{0},person:{1}", adds, persons));
});
结果:
adds:aaaa,person:[Person{id=4, name='b', age=1, adds='aaaa'}]
adds:bbbb,person:[Person{id=5, name='c', age=1, adds='bbbb'}]
(List-> Map)
Map<Long, Person> collect = personList.stream().collect(Collectors.toMap(Person::getId, person -> person));
collect.forEach((id, persons) -> {
System.out.println(MessageFormat.format("id:{0},person:{1}", id, persons));
});
结果:
id:1,person:Person{id=1, name='a', age=1, adds='aaaa'}
id:2,person:Person{id=2, name='a', age=1, adds='bbbb'}
id:3,person:Person{id=3, name='b', age=1, adds='bbbb'}
id:4,person:Person{id=4, name='b', age=1, adds='aaaa'}
id:5,person:Person{id=5, name='c', age=1, adds='bbbb'}
(Map -> List)
List<Person> pList = collect.values().stream().collect(Collectors.toList());
pList.forEach(System.out::println);
List<Long> idList = collect.keySet().stream().collect(Collectors.toList());
idList.forEach(System.out::println);
(List-> List)
List<String> collect1 = personList.stream().map(p -> p.getName()).collect(Collectors.toList());
collect1.forEach(System.out::println);
結果:
a
a
b
b
c
排序
Map
Map<String, Integer> unsortMap = new HashMap<>();
unsortMap.put("z", 10);
unsortMap.put("b", 5);
unsortMap.put("a", 6);
unsortMap.put("c", 20);
unsortMap.put("d", 1);
unsortMap.put("e", 7);
unsortMap.put("y", 8);
unsortMap.put("n", 99);
unsortMap.put("g", 50);
unsortMap.put("m", 2);
unsortMap.put("f", 9);
System.out.println("Original...");
System.out.println(unsortMap);
LinkedHashMap<String, Integer> sortMap = unsortMap.entrySet().stream().sorted(Map.Entry.comparingByKey()).
collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));
System.out.println("sort...");
System.out.println(sortMap);
//倒序
LinkedHashMap<String, Integer> sortMap2 = unsortMap.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())).
collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));
System.out.println(sortMap2);
结果:
Original...
{a=6, b=5, c=20, d=1, e=7, f=9, g=50, y=8, z=10, m=2, n=99}
sort...
{a=6, b=5, c=20, d=1, e=7, f=9, g=50, m=2, n=99, y=8, z=10}
{z=10, y=8, n=99, m=2, g=50, f=9, e=7, d=1, c=20, b=5, a=6}
List
List<Person> personList = new ArrayList();
personList = Arrays.asList(
new Person("zs", 12),
new Person("ls", 5),
new Person("ww", 33)
);
//method1
System.out.println("method1...");
personList.sort((p1, p2) -> p1.getName().compareTo(p2.getName()));
personList.forEach(person -> System.out.println(person.getName()));
//method2
System.out.println("method2...");
personList.sort(Comparator.comparing(Person::getName));
personList.forEach(person -> System.out.println(person.getName()));
结果:
method1...
ls
ww
zs
method2...
ls
ww
zs
过滤
List<Person> personList = new ArrayList();
personList = Arrays.asList(
new Person("zs", 12),
new Person("ls", 5),
new Person("ww", 33)
);
List<Person> collect = personList.stream().filter(person -> person.getAge() > 10).collect(Collectors.toList());
collect.forEach(System.out::println);
System.out.println("========================");
Person person1 = personList.stream().filter(person -> person.getAge() > 10)//过滤
.findAny()//找到一个就返回
.orElse(null);//没有就返回null
System.out.println(person1);
结果:
Person{id=null, name='zs', age=12, adds='null'}
Person{id=null, name='ww', age=33, adds='null'}
========================
Person{id=null, name='zs', age=12, adds='null'}
修改集合元素
List<Person> personList = new ArrayList();
personList = Arrays.asList(
new Person("zs", 12),
new Person("ls", 5),
new Person("ww", 33)
);
List<Person> collect = personList.stream().map(person -> {
person.setName(person.getName()+">>>");
return person;
}).collect(Collectors.toList());
collect.forEach(System.out::println);
List<String> collect2 = personList.stream().map(Person::getName).collect(Collectors.toList());
collect2.forEach(System.out::println);
结果:
Person{id=null, name='zs>>>', age=12, adds='null'}
Person{id=null, name='ls>>>', age=5, adds='null'}
Person{id=null, name='ww>>>', age=33, adds='null'}
zs>>>
ls>>>
ww>>>
flatMap
example:
List<Person> list= new ArrayList<>();
Person p1 =new Person("zhang",Arrays.asList("wqwq","qqqeq","dfdfs"));
Person p2 =new Person("li",Arrays.asList("hyhy","vffv","vzvzdv"));
Person p3 =new Person("wang",Arrays.asList("43","4dfds","dttdsvsvfdfs"));
list.add(p1);
list.add(p2);
list.add(p3);
list.forEach(System.out::println);
System.out.println("-------------------------");
List<String> collect = list.stream()
.map(x -> x.getAddrs()) //Stream<Set<String>>
.flatMap(x -> x.stream()) //Stream<String>
.distinct()
.collect(Collectors.toList());
collect.forEach(System.out::println);
}
結果:
Person{id=null, name='zhang', age=0, addrs=[wqwq, qqqeq, dfdfs]}
Person{id=null, name='li', age=0, addrs=[hyhy, vffv, vzvzdv]}
Person{id=null, name='wang', age=0, addrs=[43, 4dfds, dttdsvsvfdfs]}
-------------------------
wqwq
qqqeq
dfdfs
hyhy
vffv
vzvzdv
43
4dfds
dttdsvsvfdfs
example:
List<List<String>> list= new ArrayList<>();
List<String> list1= Arrays.asList("123","234","345");
List<String> list2= Arrays.asList("asd","sdf","dfg");
List<String> list3= Arrays.asList("zxc","xcv","cvb");
list.add(list1);
list.add(list2);
list.add(list3);
list.forEach(System.out::println);
System.out.println("-------------------------");
List<String> collect = list.stream().flatMap(x -> x.stream()).collect(Collectors.toList());
collect.forEach(System.out::println);
結果:
[123, 234, 345]
[asd, sdf, dfg]
[zxc, xcv, cvb]
-------------------------
123
234
345
asd
sdf
dfg
zxc
xcv
cvb
mapToObj
String str = "hello";
str.chars().mapToObj(x -> (char) x).forEach(System.out::println);
结果:
h
e
l
l
o
裴波那契数列
Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1], t[0] + t[1]})
.limit(10)
.map(t -> t[0])
.forEach(System.out::println);
//结果
0
1
1
2
3
5
8
13
21
34