使用Sream流: 一行搞定
public class StreamDemo { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("Collin"); list.add("Leo"); list.add("Leon"); list.add("Liy"); list.add("Champ"); list.add("Tristan"); list.stream().filter(s -> s.startsWith("L")).filter(s -> s.length() == 3).forEach(System.out::println); } } 结果: Leo LiyStream流的使用
生成流 通过数据源(集合,数组等)生成流 list.stream()中间操作 一个流后面可以跟随零个或多个中间操作,其目的主要是打开流,做出某种程度的数据过滤/映射,然后返回一个新的流, 交给下一个操作使用 filter()终结操作 一个流只能有一个终结操作,当这个操作执行后,流就被使用“光"了,无法再被操作。所以这必定是流的最后一个操作 forEach()Stream流的常见生成方式
Collection体系的集 合可以使用默认方法stream(生成流 default Stream< E > stream0Map体系的集合间接的生成流数组可以通过Stream接口的静态方法of(T… values)生成流 public class StreamDemo { public static void main(String[] args) { //Collection体系的集 合可以使用默认方法stream(生成流 List<String> list = new ArrayList<String>(); Stream<String> listStream = list.stream(); Set<String> set = new HashSet<String>(); Stream<String> setStream = set.stream(); //Map体系的集合间接生成流 Map<String,Integer> map = new HashMap<String, Integer>(); Stream<String> keyStream = map.keySet().stream(); //Map键的Stream流 Stream<Integer> valueStream = map.values().stream(); //Map值的Stream流 Stream<Entry<String, Integer>> entryStream = map.entrySet().stream(); //Map键值对的Stream流 //数组可以通过Stream接口的静态方法of(T... values)生成流 String[] strArray = {"hello","world","java"}; Stream<String> strArrayStream = Stream.of(strArray); Stream<String> strArrayStream2 = Stream.of("hello","world","java"); Stream<Integer> intStream = Stream.of(10,20,30); } }Stream< T > filter(predicate predicate): 用于对流中的数据进行过滤 Predicate接口中的方法: boolean test(T t): 对给定的参数进行判断,返回一个布尔值 例子
Stream limit(long maxSize): 返回此流汇总到元素组成的流,截取前指定参数个数的数据
Stream skip(long n):跳过指定参数个数的数据,返回由該流的剩余元素組成的流
public class Stream_limit_skip { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("Collin"); list.add("Leo"); list.add("Leon"); list.add("Liy"); list.add("Champ"); list.add("Tristan"); list.forEach(System.out::println); System.out.println("--------------------"); //取前三个元素输出 list.stream().limit(3).forEach(System.out::println); System.out.println("--------------------"); //跳过三个元素,剩下输出 list.stream().skip(3).forEach(System.out::println); System.out.println("--------------------"); //跳过俩个元素,把剩下的元素前俩个输出 list.stream().skip(2).limit(2).forEach(System.out::println); } } 结果: Collin Leo Leon Liy Champ Tristan -------------------- Collin Leo Leon -------------------- Liy Champ Tristan -------------------- Leon Liy static <Т> Stream concat(Stream a, Streamb): 合并a和b俩个流为一个流 (静态方法)Stream distinct0:返回由该流的不同元素(根据0bjectequals(Object) 来判断)组成的流 public class Stream_concat_distinct { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("1.Collin"); list.add("2.Leo"); list.add("3.Leon"); list.add("4.Liy"); list.add("5.Champ"); list.add("6.Tristan"); //1.取前4个元素组成一个流 Stream<String> s1 = list.stream().limit(4); //2.跳过俩个数据组成一个流 Stream<String> s2 = list.stream().skip(2); //合并1.2.得到的流 输出 Stream.concat(s1, s2).forEach(System.out::println); 结果: 1.Collin 2.Leo 3.Leon 4.Liy 3.Leon 4.Liy 5.Champ 6.Tristan 去重复: //合并1.2.得到的流,输出结果,但是字符串元素不重复 Stream.concat(s1, s2).distinct().forEach(System.out::println); 结果: 1.Collin 2.Leo 3.Leon 4.Liy 5.Champ 6.Tristan Stream sorted(): 返回此流的元素组成的流,根据自然顺序排序Stream sorted(Comparator comparator): 返回该流的元素组成的流,根据提供的Comparator进行排序 Comparator接口中的方法 int compare(T o1, T o2) public class Stream_sorted { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("Collin"); list.add("Leo"); list.add("Leon"); list.add("Lay"); list.add("Champ"); list.add("Tristan"); //1.按字母顺序排序 list.stream().sorted().forEach(System.out::println); System.out.println("----------------"); //2.按长度排序 list.stream().sorted((s1,s2) -> s1.length() - s2.length()).forEach(System.out::println); //这里按长度排序后,长度相同的元素会按照添加顺序来排序 //所以会有 Leo 在 Lay 前的情况 System.out.println("--------------优化--------------"); //优化 list.stream().sorted((s1,s2) -> { int num = s1.length()-s2.length(); int num2 = num == 0? s1.compareTo(s2):num; return num2; }).forEach(System.out::println); } } 结果: 无参sorted方法,自然排序(A-Z): Champ Collin Lay Leo Leon Tristan ---------------- 带参sorted方法: Leo Lay Leon Champ Collin Tristan --------------优化-------------- Lay Leo Leon Champ Collin Tristan < R > Stream< R > map(Function mapper):返回由给定函数应用于此流的元素的结果组成的流 Function接口中的方法 R apply(Tt)IntStream mapTolnt(ToIntFunction mapper): 返回一个IntStream其中包含将给定函数应用与此流的元素的结果 IntStream:表示原始int流 TolntFunction接口中的方法 int applyAsInt(T value) public class Stream_map_mapToInt { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("10"); list.add("20"); list.add("30"); list.add("40"); list.add("50"); //将集合中的字符串数据转化为整数之后在控制台输出 //list.stream().map(s -> Integer.parseInt(s)).forEach(System.out::println); //优化 list.stream().map(Integer::parseInt).forEach(System.out::println); System.out.println("---------"); list.stream().mapToInt(Integer::parseInt).forEach(System.out::println); System.out.println("---------"); //int sun() 返回此流中所有元素的和 IntStream中的方法 int result = list.stream().mapToInt(Integer::parseInt).sum(); System.out.println(result); } } 结果: 10 20 30 40 50 --------- 10 20 30 40 50 --------- 150Stream流的常见终结操作方法
void forEach(C onsumer action):对此流的每个元素执行操作 Consumer接口中的方法 void accept(T t):对给定的参数执行此操作long count():返回此流中的元素数 public class Stream_foreach_count { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("Collin"); list.add("Leo"); list.add("Leon"); list.add("Liy"); list.add("Champ"); list.add("Tristan"); //1.输出每一个元素 list.stream().forEach(System.out::println); //2.输出元素个数 long count = list.stream().count(); System.out.println(count); } } Collin Leo Leon Liy Champ Tristan 6对数据使用Stream流的方式操作完毕后,想把数据收集到集合中时,Stream提供了这样的方法:
R collect(Collector collector) 这个收集方法的参数是一个Collector接口工具类Collectors提供了具体的收集方式
public static < T > Collector toList(): 把元素收集到List集合中public static < T > Collector toSet(): 把元索收集到Set集合中 public static Collector toMap(Function keyMapper,Function valueMapper):把元素收集到Map集合中 public class CollectDemo { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("Collin"); list.add("Leo"); list.add("Leon"); list.add("Liy"); list.add("Champ"); list.add("Tristan"); //得到3个字 流 Stream<String> listStream = list.stream().filter(s -> s.length() ==3); //用Stream流操作完毕的数据收集到List集合中并遍历 List<String> names = listStream.collect(Collectors.toList()); for (String name : names) { System.out.println(name); } //创建Set集合对象 Set<Integer> set = new HashSet<Integer>(); set.add(10); set.add(20); set.add(30); set.add(33); set.add(50); //大于25的 Stream<Integer> setStream = set.stream().filter(s -> s > 25); //用Stream流操作完毕的数据收集到Set集合中并遍历 Set<Integer> ages = setStream.collect(Collectors.toSet()); for (Integer age : ages) { System.out.println(age); } //定义字符串数组,姓名,年龄 String[] strArray = {"Collin,21","Leo,32","Alan,29","Iven,22"}; //年龄大于28的 Stream<String> arrayStream = Stream.of(strArray).filter(s -> Integer.parseInt(s.split(",")[1]) > 28); //收集到Map集合中并遍历,姓名为键,年龄为值 Map<String, Integer> map = arrayStream.collect(Collectors.toMap(s -> s.split(",")[0], s -> Integer.parseInt(s.split(",")[1]))); Set<String> keySet = map.keySet(); for (String key : keySet) { Integer value = map.get(key); System.out.println(key+ " - " + value); } } } 结果: Leo Liy 33 50 30 Leo - 32 Alan - 29