一.判断List对象中是否包含对象的值
1.第一种方法:
boolean present1 = appCountStudentActionResults.stream().filter(m -> m.getActiveTypes().equals("3")).findAny().isPresent();
2.第二种方法:
boolean b = appCountStudentActionResults.stream().anyMatch(m -> m.getActiveTypes().equals("1"))
二.List按某个字段排序
1.第一种方法<Bean>:
List<User> newList = list.stream().sorted(Comparator.comparing(User::getAge))
.collect(Collectors.toList());
List<类> list; 代表某集合
//返回 对象集合以类属性一升序排序
list.stream().sorted(Comparator.comparing(类::属性一));
//返回 对象集合以类属性一降序排序 注意两种写法
list.stream().sorted(Comparator.comparing(类::属性一).reversed());//先以属性一升序,结果进行属性一降序
list.stream().sorted(Comparator.comparing(类::属性一,Comparator.reverseOrder()));//以属性一降序
(多个属性排序)
原文链接:https://blog.csdn.net/qq_41999771/article/details/123045488
2.第二种方法<Map<String,Object>>:
常见排序
public static void main(String args[]) throws IOException, ClassNotFoundException {
testSorted();
}
public static void testSorted() {
List<Map<String, Object>> cpuRateList = setCpuRateValue();
List<Map<String, Object>> valueList = cpuRateList.stream()
.sorted(Comparator.comparing(e -> MapUtils.getDouble(e, "value"))).collect(Collectors.toList());
System.out.println("============排序 ===========");
valueList.forEach(System.out::println);
// 倒序
// Collections.reverse(valueList);
List<Map<String, Object>> reverseValueList = cpuRateList.stream()
.sorted((c1, c2) -> MapUtils.getDouble(c2, "value").compareTo(MapUtils.getDouble(c1, "value"))).collect(Collectors.toList());
System.out.println("============倒序===========");
reverseValueList.forEach(System.out::println);
}
处理null的情况(如果排序中,值为null了,要怎么处理呢?)
public static void main(String args[]) throws IOException, ClassNotFoundException { testMapWithNullSorted();
}
public static void testMapWithNullSorted() {
List<Map<String, Object>> cpuRateList = setCpuRateNullValue();
List<Map<String, Object>> filterValueList = cpuRateList.stream().filter(e -> MapUtils.getDouble(e, "value") != null)
.sorted(Comparator.comparing(e -> MapUtils.getDouble(e, "value"))).collect(Collectors.toList());
System.out.println("==========过滤null===========");
filterValueList.forEach(System.out::println);
List<Map<String, Object>> valueNullFirstList = cpuRateList.stream().sorted(Comparator.comparing(e -> MapUtils.getDouble(e, "value"), Comparator.nullsFirst(Double::compareTo))).collect(Collectors.toList());
System.out.println("==========null排前面===========");
valueNullFirstList.forEach(System.out::println);
List<Map<String, Object>> valueNullLastList = cpuRateList.stream().sorted(Comparator.comparing(e -> MapUtils.getDouble(e, "value"), Comparator.nullsLast(Double::compareTo))).collect(Collectors.toList());
System.out.println("==========null排后面===========");
valueNullLastList.forEach(System.out::println);
}
JSONArray的排序同理(JSONArray排序)
public static void main(String args[]){
testSorted();
testJsonWithNullSorted();
}
public static void testSorted() {
JSONArray fieldList = initFiledValue();
List<JSONObject> sortIdList = ListUtils.emptyIfNull(fieldList).stream().map(e -> (JSONObject) e)
.sorted(Comparator.comparing(e -> MapUtils.getInteger(e, "sortId"))).collect(Collectors.toList());
System.out.println("============排序 ===========");
sortIdList.forEach(System.out::println);
// 倒序
List<JSONObject> reverseSortIdList = ListUtils.emptyIfNull(fieldList).stream().map(e -> (JSONObject) e)
.sorted((c1, c2) -> MapUtils.getInteger(c2, "sortId").compareTo(MapUtils.getInteger(c1, "sortId"))).collect(Collectors.toList());
System.out.println("============倒序===========");
reverseSortIdList.forEach(System.out::println);
}
public static void testJsonWithNullSorted() {
JSONArray fieldList = initFiledNullValue();
List<JSONObject> filterValueList = ListUtils.emptyIfNull(fieldList).stream().map(e -> (JSONObject) e).filter(e -> MapUtils.getInteger(e, "sortId") != null)
.sorted(Comparator.comparing(e -> MapUtils.getInteger(e, "sortId"))).collect(Collectors.toList());
System.out.println("==========过滤null===========");
filterValueList.forEach(System.out::println);
List<JSONObject> valueNullFirstList = ListUtils.emptyIfNull(fieldList).stream().map(e -> (JSONObject) e).sorted(Comparator.comparing(e -> MapUtils.getInteger(e, "sortId"), Comparator.nullsFirst(Integer::compareTo))).collect(Collectors.toList());
System.out.println("==========null排前面===========");
valueNullFirstList.forEach(System.out::println);
List<JSONObject> valueNullLastList = ListUtils.emptyIfNull(fieldList).stream().map(e -> (JSONObject) e).sorted(Comparator.comparing(e -> MapUtils.getInteger(e, "sortId"), Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList());
System.out.println("==========null排后面===========");
valueNullLastList.forEach(System.out::println);
}
//
public static JSONArray initFiledValue() {
String data = "[{\n" +
"\t\"fieldName\": \"model_name\",\n" +
"\t\"sortId\": 2,\n" +
"\t\"elementLabel\": \"模型名\"\n" +
"}, {\n" +
"\t\"elementName\": \"model_mod\",\n" +
"\t\"sortId\": 1,\n" +
"\t\"elementLabel\": \"模型ID\"\n" +
"}, {\n" +
"\t\"fieldName\": \"remark\", \n" +
"\t\"sortId\": 3,\n" +
"\t\"elementLabel\": \"备注\"\n" +
"}, {\n" +
"\t\"fieldName\": \"model_type\", \n" +
"\t\"sortId\": 4,\n" +
"\t\"elementLabel\": \"类型\" \n" +
"}]";
return JSONArray.parseArray(data);
}
public static JSONArray initFiledNullValue() {
String data = "[{\n" +
"\t\"fieldName\": \"model_name\",\n" +
"\t\"sortId\": 2,\n" +
"\t\"elementLabel\": \"模型名\"\n" +
"}, {\n" +
"\t\"elementName\": \"model_mod\",\n" +
"\t\"sortId\": null,\n" +
"\t\"elementLabel\": \"模型ID\"\n" +
"}, {\n" +
"\t\"fieldName\": \"remark\", \n" +
"\t\"sortId\": 3,\n" +
"\t\"elementLabel\": \"备注\"\n" +
"}, {\n" +
"\t\"fieldName\": \"model_type\", \n" +
"\t\"sortId\": 4,\n" +
"\t\"elementLabel\": \"类型\" \n" +
"}]";
return JSONArray.parseArray(data);
}
三.Java对list多个字段进行去重以及过滤
1.对年龄和名字同时进行去重(多个字段)
public static void main(String[] args) {
us u=fa.fa(1);
us u2=fa.fa(1);
us u3=fa.fa(1);
List<us> list=new ArrayList<>();
u.setAge(10);
u.setName("张三");
list.add(u);
u2.setAge(13);
u2.setName("李四");
list.add(u2);
u3.setAge(13);
u3.setName("李四");
list.add(u3);
List<us> usList=list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(s->s.getName()+";"+s.getAge()))),ArrayList::new));
usList.forEach(userList->{
System.out.println(userList.getName()+userList.getAge());
});
}
2.对年龄进行过滤筛选(单个字段)
List<us> userDiscont=list.stream().filter(us -> us.getAge()==10).collect(Collectors.toList());
List<Map<String,Object>> list2 = selectChoasInfo.stream().distinct().collect(Collectors.toList());