当前位置: 首页> 财经> 访谈 > Stream 33

Stream 33

时间:2025/7/12 2:06:28来源:https://blog.csdn.net/2301_81744437/article/details/140875319 浏览次数:0次

 

        

package Array.collection;import java.util.*;
import java.util.stream.Stream;public class stream1 {public static void main(String[] args) {//、如何茯取List集合的Stream流?List<String> names = new ArrayList<>();Collections. addAll(names,"方法","过时","丰富");Stream<String> stream = names . stream();//2、如何荻取Set集合的Stream流?Set<String> set = new HashSet<>();Collections. addAll(set, "刈徳半", "張曼玉", "蜘蛛精","弓徳", "徳母西並");Stream<String> stream1 = set. stream();stream1. filter(s -> s. contains("徳")). forEach(s -> System. out. println(s));// 3、如何茯取Map集合的Stream流?Map<String, Double> map = new HashMap<>();map. put("古力娜扎",172.3);map . put("迪雨熱巴",168.3);map.put("弓尓扎哈",166.3);map. put("卞尓扎巴",168.3);Set<String> keys = map.keySet() ;Stream<String> ks = keys. stream();Collection<Double> values = map. values();Stream<Double> vS = values. stream();Set<Map. Entry<String, Double>> entries = map. entrySet();Stream<Map. Entry<String, Double>> kvs = entries. stream() ;kvs. filter(e -> e.getKey() . contains("把" )).forEach(e -> System. out.println(e.getKey()+ "-->" +e.getValue()));// 4. 如何获取数组StreamString[] names2 = {"张翠山","东方不败生","唐大山","独孤求败"};Stream<String> stream2 = Arrays. stream(names2);Stream<String> names21 = Stream. of(names2) ;}
}

 

package Array.collection;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;public class stream2 {public static void main(String[] args) {List<Integer> scores=new ArrayList<>();Collections.addAll(scores,32,45,425,55,47,747,56);//找出成绩大于56的数据,并升序后,在输出scores.stream().filter(s -> s>=56).sorted().forEach(s -> System.out.println(s));List<student> students=new ArrayList<>();student s1=new student("完全",31,123.2);student s2=new student("访问",13,123.2);student s3=new student("太她",34,123.2);student s4=new student("完全",31,123.2);Collections.addAll(students ,s1,s2,s3,s4);//找出年龄大于等于23,且年龄小于等于34的学生,并按年龄降序输出students.stream().filter(s -> s.getAge()>=23&& s.getAge()<=34 ).sorted((o1,o2) ->o2.getAge()-o1.getAge()).forEach(s ->System.out.println(s));//取出身高最高的前三名students.stream().sorted((o1,o2) ->Double.compare(o2.getHeight(),o1.getHeight())).limit(3).forEach(s -> System.out.println(s));//取出身高倒数两名学生students.stream().sorted((o1,o2) ->Double.compare(o2.getHeight(),o1.getHeight())).skip(students.size()-2).forEach(System.out::println);
//找出身高超过123.4的学生叫什么,要求去除重复名字,在输出students.stream().filter(s -> s.getHeight()>123.4).map(s -> s.getName()).distinct().forEach(System.out::println);//自定义对象(希望内容相同就重复)重写hashcode,equals方法students.stream().filter(s -> s.getHeight()>123.4).distinct().forEach(System.out::println);Stream<String> a=Stream.of("ewr","wrw");Stream<String> d=Stream.of("eef","wegt");Stream f=Stream.concat(a,d);f.forEach(System.out::println);}
}

关键字:Stream 33

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: