compareTo &&comparing
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class ComparingDemo {
public static void main(String[] args) {
List<Track> tracks= Arrays.asList(new Track("Bakai",5234),new Track("Violets for your fur",365),
new Track("Time was",453));
Track shortestTack=tracks.stream().min(Comparator.comparing(Track::getName)).get();
Track shortestTack1= tracks.stream().min(Comparator.comparing(track -> track.getName())).get();
System.out.println(shortestTack1);
// tracks.sort(Comparator.comparing(Track::getName));
}
}
stream
的max
方法返回Optional
对象,Optional
对象代表可能存在也可能不存在的值。如果stream为空,该值不存在,如果不为空该值存在,通过调用get方法可以取出Optional对象的值。
网友评论