package 基础知识.λ和Stream流.A5_Stream流收集操作;
import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream;
public class TestCollect {
public static void main(String[] args) {
listMe(); setMe(); mapMe(); Me();
}
public static void listMe() { ArrayList<String> list = new ArrayList<>(); list.add("尴尬酱"); list.add("这就尴尬了"); list.add("尴尬"); list.add("尴尬帝"); list.add("尴尬了"); list.add("不尴尬");
Stream<String> listStream = list.stream().filter(s -> s.length() == 3);
List<String> list1 = listStream.collect(Collectors.toList()); for (String str : list1) { System.out.println(str); }
System.out.println("===================================");
}
public static void setMe() { HashSet<Integer> set = new HashSet<>(); set.add(10); set.add(15); set.add(20); set.add(35); set.add(55); set.add(66);
Stream<Integer> setStream = set.stream().filter(i -> i > 30);
Set<Integer> setNew = setStream.collect(Collectors.toSet()); for (int i : setNew) { System.out.println(i); }
System.out.println("==================================="); }
public static void mapMe() { String[] array = {"尴尬酱,16", "尴尬帝,20,", "尴尬了,9", "尴尬,18", "不尴尬,13"};
Stream<String> arrayStream = Stream.of(array).filter(s -> Integer.parseInt(s.split(",")[1]) > 15);
Map<String,Integer> map =arrayStream.collect(Collectors.toMap( s -> s.split(",")[0], s -> Integer.parseInt(s.split(",")[1]))); Set<String> mapK = map.keySet(); for (String k : mapK) { int v = map.get(k); System.out.println("姓名: " + k + ", 年龄: " + v); } }
public static void Me() { }
}
|