sorted(Comparator com)---定制排序
创新互联是网站建设专家,致力于互联网品牌建设与网络营销,专业领域包括网站设计制作、网站设计、电商网站制作开发、小程序制作、微信营销、系统平台开发,与其他网站设计及系统开发公司不同,我们的整合解决方案结合了恒基网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,且不断评估并优化我们的方案,为客户提供全方位的互联网品牌整合方案!public class Employee {
private String name;
private Integer age;
private Double salary;
private Status status;
public Employee() {
super();
}
public Employee(Integer age){
this.age = age;
}
public Employee(String name, Integer age, Double salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
public Employee(String name, Integer age, Double salary, Status status) {
this.name = name;
this.age = age;
this.salary = salary;
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
", status=" + status +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return age == employee.age &&
Double.compare(employee.salary, salary) == 0 &&
Objects.equals(name, employee.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age, salary);
}
public enum Status{
FREE,
BUSY,
VOCATION;
}
}
@Test
public void test1(){
List list = Arrays.asList("ccc", "aaa", "bbb", "ddd", "eee");
//自然排序
list.stream()
.sorted()
.forEach(System.out::println);
System.out.println("------------------------------");
//定制排序
employees.stream()
.sorted((e1, e2) -> {
if (e1.getAge() == e2.getAge()){
return e1.getName().compareTo(e2.getName());
}else{
return Integer.compare(e1.getAge(), e2.getAge());
}
}).forEach(System.out::println);
}
min---返回流中最小值
List employees = Arrays.asList(
new Employee("张三", 18 ,9999.99, Employee.Status.FREE),
new Employee("李四", 38, 5555.99, Employee.Status.BUSY),
new Employee("王五", 50, 6666.66, Employee.Status.VOCATION),
new Employee("赵六", 16, 3333.33, Employee.Status.FREE),
new Employee("田七", 8, 7777.77, Employee.Status.BUSY)
);
@Test
public void test2(){
//allMatch---检查是否匹配所有元素
boolean b1 = employees.stream()
.allMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
System.out.println(b1);
//anyMatch---检查是否至少匹配一个元素
boolean b2 = employees.stream()
.anyMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
System.out.println(b2);
//noneMatch---检查是否没有匹配所有元素
boolean b3 = employees.stream()
.noneMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
System.out.println(b3);
//findFirst---返回第一个元素
Optional op = employees.stream()
.sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()))
.findFirst();
System.out.println(op.get());
//findAny---返回当前流中的任意元素
Optional op2 = employees.stream()
.filter((e) -> e.getStatus().equals(Employee.Status.FREE))
.findAny();
System.out.println(op2.get());
}
@Test
public void test3(){
//count---返回流中元素的总个数
Long count = employees.stream()
.count();
System.out.println(count);
//max---返回流中大值
Optional op1 = employees.stream()
.max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
System.out.println(op1.get());
//min---返回流中最小值
Optional op2 = employees.stream()
.map(Employee::getSalary)
.min(Double::compare);
System.out.println(op2.get());
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。