Flask接口签名sign原理与实例代码浅析
415
2022-12-28
Java JDK 1.8 lambda的用法详解
具体代码如下所示:
public class Student {
private String id;
private String name;
private String age;
private String address;
public Student(String id, String name, String age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public http://String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age='" + age + '\'' +
", address='" + address + '\'' +
'}';
}
}
public class TestStrean {
public static void main(String[] args){
Student stuA = new Student("1", "A", "M", "184");
Student stuB = new Student("2", "B", "G", "163");
Student stuC = new Student("3", "C", "M", "175");
Student stuD = new Student("4", "D", "G", "158");
Student stuE = new Student("5", "E", "M", "175");
List
list.add(stuA);
list.add(stuB);
list.add(stuC);
list.add(stuD);
list.add(stuE);
/*-----------forEach-----------------------*/
// list.forEach(item -> item.setId(item.getAddress()+"test") );
/*-----------filter-----------------------*/
// List
/*-----------filter的复杂应用-----------------------*/
// list.stream().filter(new Predicate
//
// @Override
// public boolean test(Student student) {
// if(student.getName() !=null){
// return student.getName().contains("30");
// }else{
// return true;
// }
// }
// }).collect(Collectors.toList());
/*-----------分页-----------------------*/
// List
/*-----------map 返回一个新的list
// List
// return new Student(
// item.getAddress(),
// item.getAge(),
// item.getName(),
// item.getId()
// );
// }).collect(Collectors.toList());
/*-----------map 返回一个原来的的list
// List
// item.setId("1");
// return item;
// }).collect(Collectors.toList());
/*-----------flatMap-----------------------*/
// List
// String[] split =item.getAddress().split("");
// String[] s2 = (String[]) Arrays.stream(split).filter(s -> s.contains("1")).toArray();
// return s2;
// });
/*-----------sorted compareTo排序-----------------------*/
// List
// if(o1.getAddress().equals(o2.getAddress())){
// return Integer.parseInt(o1.getId())-Integer.parseInt(o2.getId());
// }else{
// return o1.getAddress().compareTo(o2.getAddress());
// }
// }).collect(Collectors.toList());
/*-----------sorted Comparator排序-----------------------*/
// List
// Comparator.comparing(Student::getAddress).reversed()
// ).collect(Collectors.toList());
/*-----------sorted3 多条件排序-----------------------*/
// List
// Comparator.comparing(Student::getAddress).thenComparing(Comparator.comparing(Student::getId).reversed())
// ).collect(Collectors.toList());
// boolean b=list.stream().anyMatch(item -> item.getAge().contains("G"));
// System.out.println(b);
/*-----------Collector tomap----------------------*/
// Map
// Comparator.comparing(Student::getAddress).thenComparing(Comparator.comparing(Student::getId).reversed())
// ).collect(Collectors.toMap(Student::getId,Student::getAddress));
/*-----------Collector groupingBy----------------------*/
Map
System.out.println(ageMap );
}
}
知识点扩展:
jdk 1.8 Lambda 表达式 遍历数组
Lambda 表达式可以把回调函数作为参数传递
常用的遍历数组的方式如下
List
//遍历
datas.forEach(item->{
System.out.println(item);
});
说明:
item就是每次遍历的当前元素
总结
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~