jdk1.8表达式学习
时间:2021-12-02 作者:匿名
package 域名.client; import 域名.*; import 域名域名icate; import 域名域名ector; import 域名域名ectors; import 域名域名am; /** * @author liyongqiang * @title: Test1 * @description: TODO * @date 2019/9/209:43 */ public class Test1 { public static void main(String[] args) { List<User> list=new ArrayList<>(); 域名(new User("1","张三","一年级",95)); 域名(new User("2","李四","二年级",96)); 域名(new User("3","王五","三年级",97)); 域名(new User("4","朱留","四年级",100)); //遍历 域名ach((o)-> 域名tln(""+域名ring())); //遍历 域名ach(o-> 域名tln(""+域名ring())); //转 hashSet Set<User> set1=域名am().collect(域名t()); //转 hashSet Set<String> set2=域名am().map(User::getName).collect(域名t()); //转treeSet TreeSet<String> set3=域名am().map(User::getName).collect(域名llection(TreeSet::new)); //转 map Map<String,String> map1=域名am().collect(域名p(User::getId,User::getLevel)); //转map Map<String,String> map2=域名am().collect(域名p(User::getId,User::getName,(token, token2) -> token)); //分组 Map<String,List<User>> map3=域名am().collect(域名pingBy(User::getName)); //分组count Map<String,Long> map4=域名am().collect(域名pingBy(User::getName,域名ting())); //分组求和 Map<String,Integer>map5=域名am().collect(域名pingBy(User::getName,域名ingInt(User::getScore))); //过滤条件 List<User> temp1=域名am().filter(o->"1".equals(域名d())).collect(域名st()); //多个过滤条件 Predicate<User> scoFilter=(o)->(域名core()>25); Predicate<User> clsFilter=(o)->("三年级".equals(域名evel())); List<User> temp2=域名am().filter(scoFilter).filter(clsFilter).collect(域名st()); //取前多少个 List<User> temp3=域名am().limit(2).collect(域名st()); //升序排序 List<User>temp4=域名am().sorted((u1,u2)->(域名core()-域名core())).collect(域名st()); //降序排序 List<User>temp5=域名am().sorted((u1,u2)->(域名core()-域名core())).collect(域名st()); //升序 第一个 User user1=域名am().max((u1,u2)->(域名core()-域名core())).get(); //降序第一个 User user2=域名am().min((u1,u2)->(域名core()-域名core())).get(); //求和 int sum=域名llelStream().mapToInt(p->域名core()).sum(); IntSummaryStatistics stats=域名am().mapToInt((x)->域名core()).summaryStatistics(); 域名tf("最大的数字"+域名ax()); 域名tf("最小的数字"+域名in()); 域名tf("所有数字的综合"+域名um()); 域名tf("所有数字的平均值"+域名verage()); } } class User{ private String id; private String name; private String level; private int score; public User(String id, String name, String level, int score) { 域名 = id; 域名 = name; 域名l = level; 域名e = score; } public String getId() { return id; } public void setId(String id) { 域名 = id; } public String getName() { return name; } public void setName(String name) { 域名 = name; } public String getLevel() { return level; } public void setLevel(String level) { 域名l = level; } public int getScore() { return score; } public void setScore(int score) { 域名e = score; } @Override public String toString() { return "User{" + "id=\'" + id + \'\\'\' + ", name=\'" + name + \'\\'\' + ", level=\'" + level + \'\\'\' + ", score=" + score + \'}\'; } }