JDK8集合的便捷操做

JDK8新特性,stream相關操做。把集合轉換成stream,再對其進行相關操做,加上lambada表達式。html

demo:java

List<String> list = Arrays.asList("a","b"); //全部實現了Conllection的類型均可以使用
    	String str = list.stream().map(x->{
    		try {
				return x+" and "+x;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "";
			}
    	}).collect(Collectors.joining("\n"));  // collect 收集器,把處理過的元素都手機起來,Collectors.joining("\n") 是用"\n"(換行符) 分開,也能夠用別本身須要的
System.out.println(str);

  執行結果:多線程

a and a
b and b

  集合元素分組收集,用字符串長度進行分組:函數

    	List<String> list = Arrays.asList("abc","bb","cc","a");
    	Map<Integer, List<String>> collect = list.stream().collect(Collectors.groupingBy(String::length));
    	System.out.println(collect.toString());

  執行結果:spa

{1=[a], 2=[bb, cc], 3=[abc]}

  集合的forEach:線程

List<String> list1 = Arrays.asList("abc","bb","cc","a");
    	list1.forEach(x->{
    		System.out.println(x.length()); //輸出每一個元素的長度
    	});

  執行結果:htm

3
2
2
1

max和min函數,返回最值:對象

    	List<String> list = Arrays.asList("abc","bb","cc","a");
    	Optional<String> max = list.stream().max((a,b)->{
    		return a.compareTo(b);
    	});
    	System.err.println(max.orElse(null));

  返回的max爲list最大值。max能夠調用get()函數,可是若是list爲空,則max()返回的就是null,那直接使用get就會報錯,可是JDK8改動後,orElse(null),表示若是對象爲空,就返回null,不會報錯。blog

平行流,就是把集合的個元素開多線程去處理:字符串

List<String> list = IntStream.iterate(1, i -> i + 1).limit(20).mapToObj(Integer::toString).collect(Collectors.toList()); //limit 限制stream的最多個數
        list.stream().parallel().forEach(x->{
            System.out.println(x+" " +Thread.currentThread());
        });
//能夠直接用parallelStream轉換平行流,以下

list.parallelStream().forEach(x->{
    System.out.println(x);
});

  

  執行結果:

13 Thread[main,5,main]
7 Thread[ForkJoinPool.commonPool-worker-1,5,main]
15 Thread[main,5,main]
6 Thread[ForkJoinPool.commonPool-worker-1,5,main]
9 Thread[ForkJoinPool.commonPool-worker-1,5,main]
10 Thread[ForkJoinPool.commonPool-worker-1,5,main]
8 Thread[ForkJoinPool.commonPool-worker-1,5,main]
2 Thread[ForkJoinPool.commonPool-worker-1,5,main]
1 Thread[ForkJoinPool.commonPool-worker-1,5,main]
5 Thread[ForkJoinPool.commonPool-worker-1,5,main]
4 Thread[ForkJoinPool.commonPool-worker-1,5,main]
18 Thread[ForkJoinPool.commonPool-worker-1,5,main]
20 Thread[ForkJoinPool.commonPool-worker-1,5,main]
19 Thread[ForkJoinPool.commonPool-worker-1,5,main]
17 Thread[ForkJoinPool.commonPool-worker-1,5,main]
16 Thread[ForkJoinPool.commonPool-worker-1,5,main]
12 Thread[ForkJoinPool.commonPool-worker-1,5,main]
11 Thread[ForkJoinPool.commonPool-worker-1,5,main]
14 Thread[main,5,main]
3 Thread[ForkJoinPool.commonPool-worker-3,5,main]

  parallel執行是使用 ForkJoinPool的線程池,ForkJoinPool的最大線程數是CPU數-1,若是算上當前線程,則會有CPU數量的線程數執行任務。

 

集合的合併,把多個集合合併成一個集合並進行操做:  

        List<String> al = Arrays.asList("a", "b", "c", "d","e","f");
        List<String> al2 = Arrays.asList("a1", "b1", "c1", "d1","e1","f1");
        //吧al和al2合併後按單線程輸出來
        Stream.concat(al.stream(), al2.stream()).forEach(System.out::println);
        System.out.println("******************");
        //把al和al2合併,並按平行流輸出
        Stream.concat(al.stream(),al2.stream()).parallel().forEach(System.out::println);
        System.out.println("------------------");

  執行結果:

a
b
c
d
e
f
a1
b1
c1
d1
e1
f1
******************
b1
c1
a1
d
f
e
b
c
a
d1
e1
f1
------------------

  上面一個執行語句是普通流操做,下面一個語句是平行流(parallel)操做。

     

       

 

stream還有不少操做

Optional<T> findFirst(); //返回stream的第一個

Optional<T> findAny(); //隨機返回一個

Stream<T> of(T... values) ; //傳入多個

相關文章
相關標籤/搜索