java代码排序去重

去重第一种方法

	//根据报警Id去重
		ArrayList<Map> alarmid = overrunRedAlarm.stream().collect(
			Collectors.collectingAndThen(
				Collectors.toCollection(
					() -> new TreeSet<>(Comparator.comparing(m -> m.get("alarmid").toString()))
				), ArrayList::new
			)
		);

去重第二种方法

	//去重方法

	private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
		Map<Object, Boolean> seen = new ConcurrentHashMap<>();
		return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
	}




	List<Map> dataList = overrunRedAlarm1 .stream()
			.filter(distinctByKey(o -> o.get("alarmid") ))
			.collect(Collectors.toList());


排序

正序
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Collections.sort(curveByNodeId, (o1, o2) -> {

					LocalDateTime cj = LocalDateTime.parse(o1.get("DataTime").toString(), dateTimeFormatter);

					LocalDateTime dataTime = LocalDateTime.parse(o2.get("DataTime").toString(), dateTimeFormatter);

					return cj.compareTo(dataTime);
				}
			);
倒序
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Collections.sort(curveByNodeId, (o1, o2) -> {

					LocalDateTime cj = LocalDateTime.parse(o1.get("DataTime").toString(), dateTimeFormatter);

					LocalDateTime dataTime = LocalDateTime.parse(o2.get("DataTime").toString(), dateTimeFormatter);

					return dataTime .compareTo(cj );
				}
			);

对象的排序去重

	List<AlarmDetailsDTO> unique = mapList.getRecords().stream()
				.sorted(Comparator.comparing(AlarmDetailsDTO::getDataTime).reversed()
					.thenComparing(AlarmDetailsDTO::getEndordertime).reversed())
				.collect(Collectors.toMap(
					AlarmDetailsDTO::getAlarmId, // keyMapper
					Function.identity(),         // valueMapper
					(existing, replacement) -> replacement // mergeFunction
				))
				.values()
				.stream()
				.collect(Collectors.toList());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值