JAVA8 操作List<Map<String,Object>> 按照某个KEY进行分组,然后合并Map数据案例
public static List<Map<String, Object>> mergeDataList(List<Map<String, Object>> mergeList,String mergeKey){
Set<String> set = new HashSet<>();
return mergeList.stream().filter(map->map.get(mergeKey)!=null).collect(Collectors.groupingBy(o->{
//暂存所有key
set.addAll(o.keySet());
//按mergeKey分组
return o.get(mergeKey).toString();
})).entrySet().stream().map(o->{
//合并
Map<String, Object> map = o.getValue().stream().flatMap(m->{
return m.entrySet().stream();
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a,b)->b));
//为没有的key赋值0
set.stream().forEach(k->{
if(!map.containsKey(k)) map.put(k, 0);
});
return map;
}).collect(Collectors.toList());
}
public static void main(String[] args) {
List<Map<String,Object>> result1 = new ArrayList<>();
Map map1 = new HashMap();
map1.put("instance", "192.168.25.130");
map1.put("rate", 20);
Map map2 = new HashMap();
map2.put("instance", "192.168.25.131");
map2.put("rate", 21);
Map map3 = new HashMap();
map3.put("instance", "192.168.25.132");
map3.put("rate", 22);
result1.add(map1);
result1.add(map2);
result1.add(map3);
Map map4 = new HashMap();
map4.put("instance", "192.168.25.130");
map4.put("cpu", 30);
Map map5 = new HashMap();
map5.put("instance", "192.168.25.131");
map5.put("cpu", 31);
Map map6 = new HashMap();
map6.put("instance", "192.168.25.132");
map6.put("cpu", 32);
result1.add(map4);
result1.add(map5);
result1.add(map6);
Map map7 = new HashMap();
map7.put("instance", "192.168.25.130");
map7.put("total", 40);
Map map8 = new HashMap();
map8.put("instance", "192.168.25.131");
map8.put("total", 41);
Map map9 = new HashMap();
map9.put("instance", "192.168.25.132");
map9.put("total", 42);
result1.add(map7);
result1.add(map8);
result1.add(map9);
List<Map<String,Object>> resultList = new ArrayList<>();
resultList.addAll(result1);
mergeDataList(resultList, "instance").forEach(map -> System.out.println(JSONObject.toJSONString(map)));
}
Java多线程异步操作案例
public static void main(String[] args) {
// 模拟多个异步线程任务执行
List<String> taskList = Arrays.asList("task1", "task2","task3","task4");
List<CompletableFuture> allRunTask = taskList.stream().map(taskId -> taskRun(taskId)).collect(Collectors.toList());
//new CompletableFuture[allRunTask.size()]
System.out.println("我执行到了......");
CompletableFuture.allOf(
allRunTask.toArray(new CompletableFuture[allRunTask.size()])
).thenRun(() -> {
allRunTask.stream()
.map(taskIdFuture -> {
System.out.println(111);
return taskIdFuture.join();
})
.collect(Collectors.toList());
});
Thread.sleep(3000);
}
}
public static CompletableFuture taskRun(String taskId) {
// 构建无返回值的异步任务
return CompletableFuture.runAsync(() -> {
if ("task1".equals(taskId)) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("采集task1任务执行");
}
if ("task2".equals(taskId)) {
System.out.println("采集task2任务执行");
}
if ("task3".equals(taskId)) {
System.out.println("采集task3任务执行");
}
if ("task4".equals(taskId)) {
System.out.println("采集task4任务执行");
}
}).thenRun(() -> {
// 采集回调结果
System.out.println(taskId);
}).exceptionally((e) ->{
e.printStackTrace();
return null;
});
}
借鉴一下 : https://blog.csdn.net/themagickeyjianan/article/details/106616733
学习一下: https://juejin.cn/post/6902655550031413262
JWT 令牌Token案例
public static void main(String[] args) throws Exception {
test1();
}
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
public static void test1() throws InterruptedException {// 生成JWT
Map<String, Object> claims = new HashMap<String, Object>();
claims.put("username", "zss");
claims.put("age", 18);
//生成token
String token = Jwts.builder()
.setClaims(claims)
.setId("666") //登录用户的id
.setSubject("小马") //登录用户的名称
.setExpiration(new Date(System.currentTimeMillis() + 4*1000))//过期时间
.setIssuedAt(new Date())
.signWith(SignatureAlgorithm.HS256, "abc1234")
.compact();
System.out.println("token令牌是:"+token);
//Thread.sleep(1000);
//
System.out.println("睡了一下.....");
Claims claims1 = Jwts.parser()
.setSigningKey("abbbnn")
.parseClaimsJws(token)
.getBody();
if (ObjectUtil.isEmpty(claims1)) {
System.out.println("空...");
return;
}
Date d1 = claims1.getIssuedAt();
Date d2 = claims1.getExpiration();
System.out.println("username参数值:" + claims1.get("username"));
System.out.println("登录用户的id:" + claims1.getId());
System.out.println("登录用户的id:" + claims1.getExpiration());
System.out.println("登录用户的名称:" + claims1.getSubject());
System.out.println("令牌签发时间:" + sdf.format(d1));
System.out.println("令牌过期时间:" + sdf.format(d2));
System.out.println(d2.getTime() - d1.getTime());
System.out.println(d2.getTime() - new Date().getTime());
}
借鉴一下 : https://www.cnblogs.com/cao-lei/p/13300955.html
CompletableFuture:异步线程执行
https://juejin.cn/post/6902655550031413262
https://juejin.cn/post/6844903594165026829
stream 常用API 操作:
- 转换流操作 https://juejin.cn/post/6983835171145383967
- 终结流操作 https://juejin.cn/post/6986805369540444191?utm_source=gold_browser_extension
- https://juejin.cn/post/6844903761547100168
swgger 注解使用: https://juejin.cn/post/6844903607414816775
https://segmentfault.com/a/1190000039678284