1、Collectors.toList()的使用
Collectors.toList()
是将Stream转换成List,List为ArrayList
,需要转换成其它,则需要使用Collectors.toCollection()
,例如,
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class Main { public static class Person { int age; Person (int age) { this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public static void main(String[] args) { List<Person> pList = Arrays.asList(new Person(11),new Person(22), new Person(33), new Person(44), new Person(55)); List<Person> result = pList.stream() .map(o -> { o.setAge(18); return o; }).collect(Collectors.toList()); System.out.println("stream() result :"+result.getClass()); System.exit(0); //success } }
2、Collectors.toSet()的使用
Collectors.toSet()
是将Stream转换成Set,Set为HashSet
,需要转换成其它,则也需要使用Collectors.toCollection()
,例如,
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class Main { public static class Person { int age; Person (int age) { this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public static void main(String[] args) { List<Person> pList = Arrays.asList(new Person(11),new Person(22), new Person(33), new Person(44), new Person(55)); Set<Person> setResult = pList.stream() .map(o -> { o.setAge(18); return o; }).collect(Collectors.toSet()); System.out.println("stream() result :"+setResult.getClass()); System.exit(0); //success } }
3、Collectors.toCollection()的使用
toList()
和toSet()
使用时,需要转换成指定的集合类型,则可以使用Collectors.toCollection()
,如下,
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class Main { public static class Person { int age; String name; Person (int age,String name) { this.age = age; this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public static void main(String[] args) { List<Person> pList = Arrays.asList(new Person(11,"mike"),new Person(22,"lynn"), new Person(33,"john"), new Person(44,"mickey"), new Person(55,"fiona")); List<Person> listResult = pList.stream().collect(Collectors.toCollection(LinkedList::new)); System.out.println("stream() result :"+listResult.getClass()); System.exit(0); //success } }
4、Collectors.toMap()的使用
Collectors.toMap()
是将Stream转换成Map,Map为HashMap
,需要转换成其它,则也需要使用Collectors.toCollection()
,
例如,
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class Main { public static class Person { int age; String name; Person (int age,String name) { this.age = age; this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public static void main(String[] args) { List<Person> pList = Arrays.asList(new Person(11,"mike"),new Person(22,"lynn"), new Person(33,"john"), new Person(44,"mickey"), new Person(55,"fiona"), new Person(31,"fiona")); //(oldValue, newValue) -> newValue是重复key时,使用的值,可以指定 Map<String, Integer> map = pList.stream().collect(Collectors.toMap(Person::getName, Person::getAge,(oldValue, newValue) -> newValue)); System.out.println("stream() result :"+map); System.out.println("stream() result :"+map.getClass()); //指定转换成集合类型LinkedHashMap Map<String, Integer> map1 = pList.stream().collect(Collectors.toMap(Person::getName, Person::getAge,(oldValue, newValue) -> newValue,LinkedHashMap::new)); System.out.println("stream() result :"+map1); System.out.println("stream() result :"+map1.getClass()); System.exit(0); //success } }