利用Java Stream排序 ,以及倒序,获取排序的最大值,最小值,建议使用排序12345和9,10,11
public static void main(String[] args) {
FormOutcome formOutcome = new FormOutcome();
formOutcome.setId("sdfsdf");
formOutcome.setName("sdfsdf");
FormOutcome formOutcome1 = new FormOutcome();
formOutcome1.setId("sdfsdf1");
formOutcome1.setName("sdfsdf1");
FormOutcome formOutcome3 = new FormOutcome();
formOutcome3.setId("sdfsdf3");
formOutcome3.setName("sdfsdf3");
FormOutcome formOutcome2 = new FormOutcome();
formOutcome2.setId("sdfsdf2");
formOutcome2.setName("sdfsdf2");
List formOutcomeDTOList = new ArrayList<>(Arrays.asList(formOutcome,formOutcome1,formOutcome2,formOutcome3));
//排序1
Collections.sort(formOutcomeDTOList, new Comparator() {
@Override
public int compare(FormOutcome o1, FormOutcome o2) {
return o1.getName().compareTo(o2.getName());
}
});
System.out.println("排序1:"+formOutcomeDTOList);
//排序2
List historyDto2 = formOutcomeDTOList.stream().sorted(Comparator.comparing(FormOutcome::getName)).collect(Collectors.toList());
System.out.println("排序2,正序:"+historyDto2);
//排序3,倒序
List historyDto3 = formOutcomeDTOList.stream().sorted(Comparator.comparing(FormOutcome::getName).reversed()).collect(Collectors.toList());
System.out.println("排序3,倒序:"+historyDto3);
//排序4,正序 第一个
List historyDto4 = formOutcomeDTOList.stream().sorted(Comparator.comparing(FormOutcome::getName)).limit(1).collect(Collectors.toList());
System.out.println("排序4,正序第一个:"+historyDto4);
//排序5 ,倒序 第一个
List historyDto5 = formOutcomeDTOList.stream().sorted(Comparator.comparing(FormOutcome::getName).reversed()).limit(1).collect(Collectors.toList());
System.out.println("排序5,倒序第一个:"+historyDto5);
//排序6
List historyDto6 = formOutcomeDTOList.stream().sorted((FormOutcome o1, FormOutcome o2) -> {return o1.getName().compareTo(o2.getName());}).collect(Collectors.toList());
System.out.println("排序6:"+historyDto6);
//排序7
List historyDto7 = formOutcomeDTOList.stream().sorted((o1, o2) -> {return o1.getName().compareTo(o2.getName());}).collect(Collectors.toList());
System.out.println("排序7:"+historyDto7);
//排序8
List historyDto8 = formOutcomeDTOList.stream().sorted((o1, o2) -> o1.getName().compareTo(o2.getName())).collect(Collectors.toList());
System.out.println("排序8:"+historyDto8);
//排序9, 最大值
Optional historyDto9 = formOutcomeDTOList.stream().max((o1, o2) -> o1.getName().compareTo(o2.getName()));
System.out.println("排序9,max:"+historyDto9);
//排序10, 最大值
Optional historyDto10 = formOutcomeDTOList.stream().max(Comparator.comparing(FormOutcome::getName));
System.out.println("排序10,max:"+historyDto10);
//排序11,最小值
Optional historyDto11 = formOutcomeDTOList.stream().min(Comparator.comparing(FormOutcome::getName));
System.out.println("排序11,min:"+historyDto11);
}
其中,FormOutcome.java 如下:
@Data
public class FormOutcome {
private String id;
private String name;
}
执行结果如下:
排序1:[FormOutcome(id=sdfsdf, name=sdfsdf), FormOutcome(id=sdfsdf1, name=sdfsdf1), FormOutcome(id=sdfsdf2, name=sdfsdf2), FormOutcome(id=sdfsdf3, name=sdfsdf3)]
排序2,正序:[FormOutcome(id=sdfsdf, name=sdfsdf), FormOutcome(id=sdfsdf1, name=sdfsdf1), FormOutcome(id=sdfsdf2, name=sdfsdf2), FormOutcome(id=sdfsdf3, name=sdfsdf3)]
排序3,倒序:[FormOutcome(id=sdfsdf3, name=sdfsdf3), FormOutcome(id=sdfsdf2, name=sdfsdf2), FormOutcome(id=sdfsdf1, name=sdfsdf1), FormOutcome(id=sdfsdf, name=sdfsdf)]
排序4,正序第一个:[FormOutcome(id=sdfsdf, name=sdfsdf)]
排序5,倒序第一个:[FormOutcome(id=sdfsdf3, name=sdfsdf3)]
排序6:[FormOutcome(id=sdfsdf, name=sdfsdf), FormOutcome(id=sdfsdf1, name=sdfsdf1), FormOutcome(id=sdfsdf2, name=sdfsdf2), FormOutcome(id=sdfsdf3, name=sdfsdf3)]
排序7:[FormOutcome(id=sdfsdf, name=sdfsdf), FormOutcome(id=sdfsdf1, name=sdfsdf1), FormOutcome(id=sdfsdf2, name=sdfsdf2), FormOutcome(id=sdfsdf3, name=sdfsdf3)]
排序8:[FormOutcome(id=sdfsdf, name=sdfsdf), FormOutcome(id=sdfsdf1, name=sdfsdf1), FormOutcome(id=sdfsdf2, name=sdfsdf2), FormOutcome(id=sdfsdf3, name=sdfsdf3)]
排序9,max:Optional[FormOutcome(id=sdfsdf3, name=sdfsdf3)]
排序10,max:Optional[FormOutcome(id=sdfsdf3, name=sdfsdf3)]
排序11,min:Optional[FormOutcome(id=sdfsdf, name=sdfsdf)]