代码如下:
package CollectPack;import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;public class CollectDemo {public static void main(String[] args){List<String> list = new ArrayList<String>();list.add("林青霞");list.add("张曼玉");list.add("王祖贤");list.add("柳岩");//需求1:得到名字为3个字的流Stream<String> listStream = list.stream().filter(s -> s.length() == 3);//需求2:把使用Stream流操作完毕的数据收集到list集合中并遍历List<String> names = listStream.collect(Collectors.toList());for (String name:names){System.out.println(name);}//创建Set集合对象Set<Integer> set =new HashSet<Integer>();set.add(10);set.add(20);set.add(30);set.add(33);set.add(35);//需求3:得到年龄大于25的流Stream<Integer> setStream = set.stream().filter(s -> s > 25);//需求4:把使用Stream流操作完毕的数据收集到Set集合中并遍历Set<Integer> ages = setStream.collect(Collectors.toSet());for (Integer age:ages){System.out.println(age);}//定义一个字符串数组,每一个字符串数据由姓名数据和年龄数据组合而成String[] strArray = {"林青霞,30","张曼玉,35","王祖贤,33","柳岩,25"};//需求5:得到字符串中年龄数据大于28的流Stream<String> arrayStream = Stream.of(strArray).filter(s -> Integer.parseInt(s.split(",")[1]) > 28);//需求6:把使用Stream流操作完毕的数据收集到Map集合中并遍历,字符串中的姓名作键,年龄作值Map<String, Integer> map = arrayStream.collect(Collectors.toMap(s -> s.split(",")[0], s -> Integer.parseInt(s.split(",")[1])));Set<String> keySet = map.keySet();for (String key:keySet){Integer value = map.get(key);System.out.println(key+","+value);}}
}