简单的介绍一下 stream
。
stream
就是一个传送带,用来对传送带上的元素进行处理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 package com.redisc; import java.util.ArrayList;import java.util.List;public class Test { public static void main (String [] args) { List<String > list = new ArrayList<>(); list .add("1" ); list .add("2" ); list .stream().filter(s -> s.startsWith("1" )).forEach(System.out::println ); } }
获取流产生 collection 获取 1 2 Collection<String > c = new ArrayList<>(); Stream <String > ss = c.stream();
Map 获取 1 2 3 4 5 6 7 Map<String , Integer> map = new HashMap<>(); map .put ("test1" , 1 );map .put ("test2" , 2 );Stream <String > keys = map .keySet().stream();Stream <Integer> values = map .values().stream();Stream <Map.Entry<String , Integer>> keyAndValue = map .entrySet().stream();
数组获取「两种方法」 1 2 3 String [] arrs = new String [] {"1" , "2" };Stream <String > arrss = Arrays.stream(arrs);Stream <String > arrss2 = Stream .of(arrs);
常用 API
forEach
count
filter
limit
skip
map
concat
代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 package com.redisc; import java.util.ArrayList;import java.util.List;import java.util.stream.Stream;class Item { public String name ; public Item(String name ) { this.name = name ; } public void setName(String name ) { this.name = name ; } } public class Test { public static void main(String[] args) { List<String> c = new ArrayList<>(); c.add ("123"); c.add ("223"); c.add ("325"); c.add ("424"); c.stream().filter (s -> s.length() > 2 ).forEach (System .out ::println); long count = c.stream().filter (s -> s.length() > 2 ).count(); c.stream().filter (s -> s.length() > 2 ).limit (2 ).forEach (System .out ::println); c.stream().map(s -> "cc" + s).forEach (System .out ::println); c.stream().map(s -> new Item(s)).forEach (System .out ::println); // 可以继续简化成 c.stream().map(Item::new ).forEach (System .out ::println); Stream<Integer > s1 = Stream.of (10 , 20 , 30 ); Stream<String> s2 = Stream.of ("10", "20", "30"); Stream<Object > allStream = Stream.concat(s1, s2); allStream.forEach (System .out ::println); } }
stream 转换 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class Test { public static void main(String [] args) { List<String > c = new ArrayList<>(); c.add ("123" ); c.add ("223" ); c.add ("325" ); c.add ("424" ); Stream<String > streamc = c.stream().filter (s -> s.length() > 2 ); Set<String > set1 = streamc.collect(Collectors.toSet()); List<String > list1 = streamc.collect(Collectors.toList()); String [] array1 = streamc.toArray(String []::new ); } }
上面的语法并没有错,但是,执行会报错,这是因为 stream
就好像流水一样,只能用一次。
案例 案例一 一个对象中,有一个 List
属性,想把 List
里面的值取出来,然后组成 Map
,以 list
里面的值为 key
,然后,对象作为 value
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 package com.redisc; import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;class Dev { public String getName () { return name; } public void setName (String name) { this .name = name; } public List<String > getDevNames () { return devNames; } public void setDevNames (List<String > devNames) { this .devNames = devNames; } public String name; public List<String > devNames; public Dev (String name, List<String > dev) { this .name = name; this .devNames = dev; } } public class Test { public static void main (String [] args) { List<Dev> devs = new ArrayList<>(); Dev dev1 = new Dev("1" , List.of("4" , "5" )); Dev dev2 = new Dev("1" , null); devs.add(dev1); devs.add(dev2); Map<String , Dev> devMap = new HashMap<>(); devs.stream().filter(s -> s.getDevNames() != null && s.getDevNames().size () > 0 ) .forEach(s -> s.getDevNames().forEach(c -> devMap.put (c, s))); System.out.println (devMap); } }
案例二 在上面的基础上,增加,如果 list
里面的元素有重复的,就以第一个为主。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 package com.redisc; import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;class Dev { public String getName () { return name; } public void setName (String name) { this .name = name; } public List<String > getDevNames () { return devNames; } public void setDevNames (List<String > devNames) { this .devNames = devNames; } public String name; public List<String > devNames; public Dev (String name, List<String > dev) { this .name = name; this .devNames = dev; } } public class Test { public static void main (String [] args) { List<Dev> devs = new ArrayList<>(); Dev dev1 = new Dev("1" , List.of("4" , "5" )); Dev dev2 = new Dev("2" , List.of("5" , "6" )); devs.add(dev1); devs.add(dev2); Map<String , Dev> devMap = new HashMap<>(); devs.stream().filter(s -> s.getDevNames() != null && s.getDevNames().size () > 0 ) .forEach(s -> s.getDevNames().forEach( c -> { if (!devMap.keySet().contains(c)) { devMap.put (c, s); } } )); System.out.println (devMap); } }