当前位置 : 主页 > 编程语言 > java >

使用lambda去重、map排序、按任意时间间隔(小时、半小时、分钟)进行结果统计

来源:互联网 收集:自由互联 发布时间:2022-09-02
1.lambda去重 public static T Predicate T distinctByKey ( Function ? super T , Object keyExtractor ) { Map Object , Boolean seen = new ConcurrentHashMap (); return object - seen . putIfAbsent ( keyExtractor . apply ( object ), Boolean . TR


1.lambda去重

public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return object -> seen.putIfAbsent(keyExtractor.apply(object), Boolean.TRUE) == null;
}//对工号重复的进行去重
ens=ens.stream().filter(distinctByKey(i->{
return i.get("code");
})).collect(Collectors.toList());

2.lambda对map排序 

package com.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.annotation.Order;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.*;
import java.util.stream.Collectors;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UeditorTestApplicationTests {

@Test
public void contextLoads() {
Map<String,Object> map = new HashMap<>();
map.put("1","oo1");
map.put("2","oo1");
map.put("3","oo1");

Map<String,Object> map1 = new HashMap<>();
map1.put("1","oo1");
map1.put("2","oo2");
map1.put("3","oo2");

Map<String,Object> map2 = new HashMap<>();
map2.put("1","oo2");
map2.put("2","oo1");
map2.put("3","oo1");

List<Map> list = new ArrayList<>();
list.add(map);
list.add(map1);
list.add(map2);

// ArrayList<Map> collect = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
// new TreeSet<>(Comparator.comparing(o -> (String) o.get("1")))), ArrayList::new));
//
// System.out.println(collect);


// ArrayList<Map> collect1 = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
// new TreeSet<>((o1, o2) -> {
// String i = (String) o1.get("2");
// String j = (String) o2.get("2");
// return i.compareTo(j);
// })), ArrayList::new));
//
// System.out.println(collect1);

TreeSet<Map<String,Object>> t = new TreeSet<>((o1, o2) -> {
String i = (String) o1.get("2");
String j = (String) o2.get("2");
return i.compareTo(j);
});
t.add(map);
t.add(map1);
t.add(map2);
System.out.println(t);


}

class A {

}

class B implements Comparable {
@Override
public int compareTo(Object o) {
return 0;
}
}

}

3.lambda对List排序

list=list.stream().sorted(Comparator.comparing(m -> m.get("排序的字段").toString())).collect(Collectors.toList());


//按日期排序
transferList = transferList.stream().sorted((d1, d2) -> {
if(d1.get("inTime")!=null&& d2.get("inTime")!=null){
Date date1 = DateUtils.parse((String) d1.get("inTime"), DateUtils.DATE_FORMAT_YYYY_MM_DD_HH_MM_SS);
Date date2 = DateUtils.parse((String) d2.get("inTime"), DateUtils.DATE_FORMAT_YYYY_MM_DD_HH_MM_SS);
return date1.compareTo(date2);
}
return 0;
}).collect(Collectors.toList());

4.lambda操作map根据key或者value排序,reversed()是倒序,正序就去掉.reversed()

public static <K extends Comparable<? super K>, V > Map<K, V> sortByKey(Map<K, V> map) {
Map<K, V> result = new LinkedHashMap<>();

map.entrySet().stream()
.sorted(Map.Entry.<K, V>comparingByKey()
.reversed()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
return result;
}public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
Map<K, V> result = new LinkedHashMap<>();

map.entrySet().stream()
.sorted(Map.Entry.<K, V>comparingByValue()
.reversed()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
return result;
}

5.按半小时统计结果值

public static void main(String[] args)throws Exception {
List<Chart> charts=new ArrayList<>();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
charts.add(new Chart(format.parse("2020-01-01 01:10:00"),2d));
charts.add(new Chart(format.parse("2020-01-01 01:20:00"),3d));
charts.add(new Chart(format.parse("2020-01-01 01:40:00"),4d));
charts.add(new Chart(format.parse("2020-02-01 01:50:00"),3d));
charts.add(new Chart(format.parse("2020-01-01 02:20:00"),26d));

Map<Long, Chart> collect = charts.stream().collect(Collectors.toMap(chart -> {
System.out.println(chart.date.getTime()/(30*60*1000));
return chart.date.getTime()/(30*60*1000);
}, Function.identity(), (k1, k2) -> {
return k1.date.getTime() > k2.date.getTime() ? k1 : k2;
}));



Map<String, Double> map=new HashMap<>();
format = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat finalFormat = format;
collect.forEach((k, v)->{
//此处把v.date转换为半小时的标准格式
String format1 = finalFormat.format(v.date);

String hours = v.date.getHours()+"";
int minutes = v.date.getMinutes();
if (minutes>=0&&minutes<30){
hours=format1+" "+hours+":00";
}else {
hours=format1+" "+hours+":30";
}
map.put(hours,v.value);
});

Map<String, Double> stringDoubleMap = sortByKey(map);
System.out.println(stringDoubleMap);



}
public static <K extends Comparable<? super K>, V > Map<K, V> sortByKey(Map<K, V> map) {
Map<K, V> result = new LinkedHashMap<>();

map.entrySet().stream()
.sorted(Map.Entry.<K, V>comparingByKey()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
return result;
}

static class Chart{
public Date date;
public Double value;

public Chart(Date date, Double value) {
this.date = date;
this.value = value;
}
}

6.lambda去重,支持根据条件有选择进行过滤去重

static class User{
String id;
String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id=id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static void main(String[] args) throws Exception {
List<User> distinctList = new ArrayList();
User user1 = new User();
user1.setId("1");
user1.setName("11");
distinctList.add(user1);
User user2 = new User();
user2.setId("2");
user2.setName("21");
distinctList.add(user2);

User user3 = new User();
//user3.setId("3");
user3.setName("31");
distinctList.add(user3);
User user4 = new User();
//user4.setId("4");
user4.setName("31");
distinctList.add(user4);
//list是需要去重的list,返回值是去重后的list
List<User> res = distinctList.stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> {
if (StringUtils.isNotBlank(o.getId())) {
return o.getName();
}
return o.getName();
}))), ArrayList::new));
//筛选

List<User> users=distinctList.stream().collect(Collectors.toMap(key->key.getName(),value->value,(v1,v2)->{
if(v1.getId()!=null){
return v1;
}else if(v2.getId()!=null){
return v2;
}
return v1;
})).values().stream().collect(Collectors.toList());
//res = distinctList.stream().filter(ls->ls.getId().equals("222")).collect(Collectors.toList());

System.out.println(users);


}

7.Java一个集合中根据,获取某一个日期对象最大值得对象

Optional<Person> max = personList.stream().max(Comparator.comparing(Person::getCreateDate));
Person person= max.get();
上一篇:XStream应用实例
下一篇:没有了
网友评论