1 package org.study2.java8.stream; 2 3 import org.junit.Test; 4 5 import java.util.*; 6 import java.util.stream.Collectors; 7 import java.util.stream.Stream; 8 9 /** 10 * Java 8 – Streams 11 * url:http://www.studytrails.com/java/java8/java8_streams/ 12 * url:https://www.cnblogs.com/andywithu/p/7404101.html 13 */ 14 public class StreamDemo { 15 @Test 16 public void test() { 17 List<String> genre = new ArrayList<String>(Arrays.asList("rock", "pop", "jazz", "reggae")); 18 long a = genre.stream().filter(s -> s.startsWith("r")).count(); 19 System.out.println(a); 20 21 boolean b = genre.stream().allMatch(s -> !s.isEmpty()); 22 System.out.println(b); 23 24 boolean b2 = genre.stream().anyMatch(s -> s.indexOf("r") == 0); 25 System.out.println(b2); 26 27 System.out.println(genre.stream().peek(s -> System.out.println(s)).anyMatch(s -> s.indexOf("r") == 0)); 28 System.out.println(genre.stream().peek(s -> System.out.println(s)).count()); 29 30 } 31 32 // 现在有一个List<User> 的集合,如何把这个list转换成Map<Integer, User> 其中,key是user id,value是User对象 33 @Test 34 public void test1() { 35 List<User> users = Arrays.asList(new User(1, "Tomcat"), new User(2, "Apache"), new User(3, "Nginx")); 36 Map<Integer, User> map = users.stream().collect(Collectors.toMap(obj -> obj.getId(), obj -> obj)); 37 System.out.println(map); 38 39 // 或者使用方法的引用 40 Map<Integer, User> map2 = users.stream().collect(Collectors.toMap(User::getId, obj -> obj)); 41 System.out.println(map2); 42 } 43 44 // 把list中的每个map中的id取出来,转换成list<String> 45 @Test 46 public void test2() { 47 List<Map<String, String>> list = new ArrayList<>(); 48 49 Map<String, String> map1 = new HashMap<>(); 50 map1.put("id", "101"); 51 map1.put("name", "Tomcat"); 52 53 Map<String, String> map2 = new HashMap<>(); 54 map2.put("id", "102"); 55 map2.put("name", "Apache"); 56 57 Map<String, String> map3 = new HashMap<>(); 58 map3.put("id", "103"); 59 map3.put("name", "Nginx"); 60 61 list.add(map1); 62 list.add(map2); 63 list.add(map3); 64 65 List<String> ids = list.stream().map(entity -> entity.get("id")).collect(Collectors.toList()); 66 System.out.println(ids); 67 68 // 或者 69 List<Object> ids2 = Arrays.asList(list.stream().map(entity -> entity.get("id")).toArray()); 70 } 71 72 // 把url的请求参数(如:type=1&from=APP&source=homePage)转换成Map<String, String> 73 @Test 74 public void test3() { 75 String queryString = "type=1&from=APP&source=homePage"; 76 Map<String, String> map = Stream.of(queryString.split("&")).map(obj -> obj.split("=")).collect(Collectors.toMap(entry -> entry[0], entry -> entry[1])); 77 System.out.println(map); 78 } 79 80 // 把List<String>转换成List<Integer> 81 @Test 82 public void test4() { 83 List<String> strs = Arrays.asList("1", "2", "3"); 84 List<Integer> ints = strs.stream().map(obj -> Integer.valueOf(obj)).collect(Collectors.toList()); 85 } 86 87 88 }
User.Class
1 package org.study2.java8.stream; 2 3 4 public class User { 5 6 private int id; 7 private String name; 8 9 public User(int id, String name) { 10 this.id = id; 11 this.name = name; 12 } 13 14 public int getId() { 15 return id; 16 } 17 18 public String toString() { 19 return "User [id=" + id + ", name=" + name + "]"; 20 } 21 22 }
详细讲解参考:
https://www.cnblogs.com/andywithu/p/7404101.html