javalistfind⽅法_Java8Stream-FindMaxandMinfromL。。。学习使⽤查最⼩和最⼤⽇期,数字,字符,字符串或对象。
Find Min or Max Date
要从⽇期流中获取最⼤或最⼩⽇期 ,可以使⽤Comparatorparing( LocalDate::toEpochDay )⽐较器。 1970-01-01 。LocalDate start = w();
LocalDate end = w().plusMonths(1).with(TemporalAdjusters.lastDayOfMonth());
//Create stream of dates
List dates = Stream.iterate(start, date -> date.plusDays(1))
.limit(ChronoUnit.DAYS.between(start, end))
.List());
// Get Min or Max Date
LocalDate maxDate = dates.stream()
.max( Comparatorparing( LocalDate::toEpochDay ) )
.get();
LocalDate minDate = dates.stream()
.min( Comparatorparing( LocalDate::toEpochDay ) )
.get();
System.out.println("maxDate = " + maxDate);
System.out.println("minDate = " + minDate);
Program Output.maxDate = 2017-10-30
minDate = 2017-09-13
使⽤上述程序从⽇期列表中查最早的⽇期或最晚的⽇期。
Find Min or Max Number
要从数字流中到最⼩和最⼤数字,请使⽤Comparatorparing( Integer::valueOf )像⽐较器⼀样。 以下⽰例是整数流的⽰例。// Get Min or Max Number
Integer maxNumber = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9)
.max(Comparatorparing(Integer::valueOf))
.get();
Integer minNumber = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9)
.min(Comparatorparing(Integer::valueOf))
.get();
System.out.println("maxNumber = " + maxNumber);
System.out.println("minNumber = " + minNumber);
Program Output.
maxNumber = 9
minNumber = 1
Find Min or Max char or String
要从字符流中查最⼩和最⼤字符串或字符 ,请像⽐较器⼀样使⽤Comparatorparing( String::valueOf ) 。// Get Min or Max String/Char
String maxChar = Stream.of("H", "T", "D", "I", "J")
.max(Comparatorparing(String::valueOf))
.get();
String minChar = Stream.of("H", "T", "D", "I", "J")
.min(Comparatorparing(String::valueOf))
.get();
System.out.println("maxChar = " + maxChar);
System.out.println("minChar = " + minChar);
Program Output.maxChar = T
minChar = D
Find Min or Max Object by key
Object comparison创建⾃⼰的custom comparator 。 例如,如果我想从Employee对象流中获取最年轻的Employee ,那么我的⽐较器将类似于Comparatorparing(Employee::getAge) 。 现在使⽤此⽐较器获取最⼤或最⼩员⼯对象。
Java程序可根据年龄查最⼤或最⼩员⼯对象。List employees = new ArrayList();
employees.add(new Employee(1, "Lokesh", 36));
employees.add(new Employee(2, "Alex", 46));
employees.add(new Employee(3, "Brian", 52));
Comparator comparator = Comparatorparing( Employee::getAge );
// Get Min or Max Object
Employee minObject = employees.stream().min(comparator).get();
Employee maxObject = employees.stream().max(comparator).get();
System.out.println("minObject = " + minObject);
System.out.println("maxObject = " + maxObject);
Program Output.minObject = Id:- 1 Name:- Lokesh Age:- 36
maxObject = Id:- 3 Name:- Brian Age:- 52
Employee对象是:class Employee {
private int id;
private String name;
private int age;
public Employee(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
StringBuilder str = null;
str = new StringBuilder();
str.append("Id:- " + getId() + " Name:- " + getName() + " Age:- " + getAge());
String();
}
}java stream
在本教程中,我们学习了使⽤Java 8 lambda expression 从原始值流中查最⼤值或最⼩值 。 我们还学会了查最⼤或最⼩对象,例如max Date或字符串。
我们还学会了从对象流中按对象属性到最⼤对象 。
在评论中把您的问题交给我。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。