stream流相当于一个流水线,一共可以分成三大类的方法。
- 获取stream流
- 中间方法
- 终结方法(可以得到一个返回结果的)
获取stream流
单列集合获取Stream流
public class Demo01 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); Collections.addAll(list, "a", "b", "c", "d", "e", "f");
list.stream().forEach(x -> System.out.println(x)); } }
|
双列集合获取stream流
public class Demo02 { public static void main(String[] args) { HashMap<String, Integer> map = new HashMap<>(); map.put("a", 1); map.put("b", 2); map.put("c", 3); map.put("d", 4); map.keySet().stream().forEach(x -> System.out.println(x)); map.entrySet().stream().forEach(x -> System.out.println(x)); } }
|
数组获取stream流
public class Demo03 { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; Arrays.stream(arr).forEach(x -> System.out.println(x)); } }
|
一堆零散的数据
public class Demo04 { public static void main(String[] args) { Stream.of("a", 1, 2, 3).forEach(x -> System.out.println(x)); } }
|
流只能用一次,就算保存了流对象,后边也无法使用了。
中间方法
filter过滤
public class Demo01 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张三丰", "张翠山", "张良", "王二麻子", "谢广坤"); list.stream() .filter(x -> x.startsWith("张")) .filter(x -> x.length() == 3) .forEach(x -> System.out.println(x)); } }
|
skip跳过前n个
public class Demo02 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张三丰", "张翠山", "张良", "王二麻子", "谢广坤"); list.stream() .limit(2) .forEach(x -> System.out.println(x));
System.out.println("=================");
list.stream() .skip(2) .forEach(x -> System.out.println(x));
System.out.println("=================");
list.stream() .skip(3) .limit(3) .forEach(x -> System.out.println(x)); } }
|
distinct去除集合中重复的元素
public class Demo03 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); Collections.addAll(list, "张无忌","张无忌","张无忌","张无忌","张无忌", "周芷若","周芷若","周芷若","周芷若", "赵敏", "张三丰", "张翠山", "张良", "王二麻子", "谢广坤"); list.stream() .distinct() .forEach(x -> System.out.println(x)); } }
|
concat连接两个集合
public class Demo04 { public static void main(String[] args) { ArrayList<String> list1 = new ArrayList<>(); Collections.addAll(list1, "张无忌", "周芷若", "赵敏", "张三丰", "张翠山", "张良"); ArrayList<String> list2 = new ArrayList<>(); Collections.addAll(list2, "张无忌", "周芷若" , "王二麻子", "谢广坤"); Stream.concat(list1.stream(), list2.stream()) .forEach(x -> System.out.println(x)); } }
|
map类型转换
public class Demo05 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); Collections.addAll(list, "张无忌-1", "周芷若-2", "赵敏-3", "张三丰-4", "张翠山-5", "张良-6", "王二麻子-7", "谢广坤-8"); list.stream() .map(x -> { String[] arr = x.split("-"); return Integer.parseInt(arr[1]); }) .forEach(x -> System.out.println(x)); } }
|
flatMap将流中每个元素转成另一个流
public class Demo06 { public static void main(String[] args) { List<String> familyNameList = Arrays.asList("赵", "钱", "孙", "李"); List<String> boyNameList = Arrays.asList("男", "子", "孩");
List<String> cartesianProductList = familyNameList.stream() .flatMap(familyName -> boyNameList.stream() .map(boyName -> familyName + boyName)) .forEach(x -> System.out.println(x)); } }
|
终结方法
forEach遍历集合
public class Demo01 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张三丰", "张翠山", "张良", "王二麻子", "谢广坤"); list.stream().forEach(x->System.out.println(x)); } }
|
count计算集合中的元素
public class Demo02 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张三丰", "张翠山", "张良", "王二麻子", "谢广坤"); long count = list.stream().count(); System.out.println(count); } }
|
toArray收集流中的数据放到数组中
public class Demo03 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张三丰", "张翠山", "张良", "王二麻子", "谢广坤"); Object[] arr1 = list.stream().toArray(); System.out.println(Arrays.toString(arr1)); System.out.println("=========="); String[] arr2 = list.stream().toArray(n -> new String[n]); System.out.println(Arrays.toString(arr2)); } }
|
collect收集流中的数据,放到集合中(List、Set、Map)
public class Demo04 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); Collections.addAll(list, "张无忌-男-15", "周芷若-女-14", "赵敏-女-14", "张三丰-男-100", "张翠山-男-40", "张良-男-35", "王二麻子-男-37", "谢广坤-男-41"); List<String> list1 = list.stream() .filter(x -> "男".equals(x.split("-")[1])) .collect(Collectors.toList()); Set<String> set1 = list.stream() .filter(x -> "男".equals(x.split("-")[1])) .collect(Collectors.toSet());
HashMap<String, String> mp1 = list.stream() .filter(x -> "男".equals(x.split("-")[1])) .collect(Collectors.toMap(x -> x.split("-")[0], x -> x.split("-")[1])); } }
|