Straem
- 主要对集合进行更简介的操作,最主要是用到了函数时接口,可以简化书写
获取Stream流的四种方式(外加并行流)
- Collection下List/Set集合直接获取(可以获取集合并行流)
ArrayList<Object> arrayList = new ArrayList<>();
arrayList.stream();
arrayList.parallelStream()
LinkedList<Object> linkedList = new LinkedList<>();
linkedList.stream();
linkedList.parallelStream()
HashSet<Object> hashSet = new HashSet<>();
hashSet.stream();
hashSet.parallelStream()
TreeSet<Object> treeSet = new TreeSet<>();
treeSet.stream();
treeSet.parallelStream()
- Map下集合需要转为Collection下Set集合间接获取
HashMap<Object, Object> hashMap = new HashMap<>();
Set<Map.Entry<Object, Object>> HashEntrieSet = hashMap.entrySet();
HashEntrieSet.stream();
TreeMap<Object, Object> treeMap = new TreeMap<>();
Set<Map.Entry<Object, Object>> TreeEntrieSet = treeMap.entrySet();
TreeEntrieSet.stream();
- 数组array[ ] 通过Arrarys数组工具类
Object[] objects = new Object[0];
Arrays.stream(objects);
- 基本类型直接Stream静态of方法,将元素包含
Stream.of(T...t);
中间方法
返回值是stream对象的方法,可以连续stream
- concat(stream1 , stream2)两个流连接
- distincat(stream)使每个元素截然不同
- limit(end)包含取前元素
- skip(start)跳过取后元素
- flatMap:接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流。
- map(Function表达式)对每一个元素都进行操作,有返回值
- peek(Consumer表达式):对每一个元素都进行操作,有返回值没有返回值。
- filter(interface)对元素条件过滤
- sorted():自然排序,流中元素需实现Comparable接口
- sorted(Comparator com):排序器排序,自定义Comparator排序器
终结方法
返回值不是stream对象的方法,将不能连续stream