首页 > 其他分享 >Map自定义key,然后把value的集合List进行指定字段排序

Map自定义key,然后把value的集合List进行指定字段排序

时间:2023-04-06 13:56:06浏览次数:47  
标签:Map String 自定义 list List add Student new public

package com.zdft.purchase;

import com.google.common.collect.Lists;

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

public class StudentMethod {
    // 需求:Map自定义key,然后把value的集合List进行指定字段排序;例如:多次考试,取最高分的集合展示
    public static void main(String[] args) {
        Student student1 = new Student(1, "张三", "1991-01", "英语", "27");
        Student student2 = new Student(2, "李四", "1992-01", "英语", "15");
        Student student3 = new Student(3, "王五", "1993-01", "英语", "24");
        Student student4 = new Student(4, "赵六", "1994-01", "英语", "15");
        Student student5 = new Student(5, "李七", "1995-01", "英语", "29");
        Student student6 = new Student(6, "钱八", "1996-01", "英语", "18");
        Student student7 = new Student(7, "孙九", "1997-01", "英语", "25");
        Student student8 = new Student(8, "周八", "1998-01", "英语", "80");
        Student student9 = new Student(8, "周八", "1998-01", "英语", "90");
        Student student10 = new Student(8, "周八", "1998-01", "英语", "65");
        Student student11 = new Student(8, "周八", "1998-01", "英语", "60");

        List<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        list.add(student5);
        list.add(student6);
        list.add(student7);
        list.add(student8);
        list.add(student9);
        list.add(student10);
        list.add(student11);

        // 第一种方式
        Map<String, List<Student>> map2 = list.stream().collect(Collectors.toMap(m -> m.getId() + m.getName() + m.getBirth(), item -> Lists.newArrayList(item),
                (List<Student> newValueList, List<Student> oldValueList) -> {
                    oldValueList.addAll(newValueList);
                    return oldValueList;
                })
        );

        List<Student> collect = new ArrayList<>();

        for (String key : map2.keySet()) {
            List<Student> studentList = map2.get(key);
            Student student = studentList.stream().sorted(Comparator.comparing(Student::getScore).reversed()).findFirst().orElse(null);
            collect.add(student);
        }

        // 第二种方式
        /*Map<String, Student> map = list.stream().collect(
                Collectors.groupingBy(m -> m.getId() + m.getName() + m.getBirth(), Collectors.collectingAndThen(
                        Collectors.reducing((t1, t2) -> t1.getScore().compareTo(t2.getScore()) > 0 ? t1 : t2),
                        Optional::get
                ))
        );

        List<Student> collect = map.values().stream().collect(Collectors.toList());*/


        // 按ID排序
        Collections.sort(collect, Comparator.comparing(Student::getId));
        collect.forEach(m1 -> {
            System.out.println(m1.toString());
        });
    }
}

class Student {
    private int id;
    private String name;
    private String birth;
    private String curriculum;
    private String score;

    public Student() {

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }

    public String getCurriculum() {
        return curriculum;
    }

    public void setCurriculum(String curriculum) {
        this.curriculum = curriculum;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    public Student(int id, String name, String birth, String curriculum, String score) {
        this.id = id;
        this.name = name;
        this.birth = birth;
        this.curriculum = curriculum;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", birth='" + birth + '\'' +
                ", curriculum='" + curriculum + '\'' +
                ", score=" + score +
                '}';
    }
}

 

标签:Map,String,自定义,list,List,add,Student,new,public
From: https://www.cnblogs.com/sunAnqing/p/17292526.html

相关文章

  • 探测工具nmap简介及使用说明
    1.前言:当我们在构建环境或排查问题时,常常是先确定环境是否正常,首要确定的就是当前ip是否可用,或是是否在使用,将要使用的端口是否已配置等进行,除了我们常用的ping或是telnet工具外,还有别一种工具nmap,可以说是扫描神器。接下来就让我简单的给大家介绍一下nmap吧。2.nmap简介:本人使用......
  • 深度学习基础入门篇[二]:机器学习常用评估指标:AUC、mAP、IS、FID、Perplexity、BLEU、
    A.深度学习基础入门篇[二]:机器学习常用评估指标:AUC、mAP、IS、FID、Perplexity、BLEU、ROUGE等详解1.基础指标简介机器学习的评价指标有精度、精确率、召回率、P-R曲线、F1值、TPR、FPR、ROC、AUC等指标,还有在生物领域常用的敏感性、特异性等指标。在分类任务中,各指标的计算......
  • map的两种遍历方式是什么
    学了Map后,我们都知道Map有两种遍历方式,keySet遍历个entrySet遍历,这里简单介绍一下这两种遍历方式。首先对于一个Map来说,右key列和value列组成,想遍历这个Map,有两种选择第一种keyset的想法是先得到其key列,使用Map的get(key)方法来获取其对应的值,如下图:对应的代码是:第二种思想是这样的......
  • ES6 => map、filter方法的区别
    letdataArr=[{name:450200000,code:1},{name:450300000,code:2}....] map:会返回执行map方法的数组(dataArr),的所有项(条件不成立也会返回undefined),可以只返回项中的某一参数 filter:会返回执行filter方法的数组(dataArr),条件成立的项,会返回整个遍历的项(不能只返回项中的某一......
  • css自定义复选框和单选框
    <!DOCTYPEhtml><html><head><metacharset="UTF-8"><title></title></head><styletype="text/css">*{margin:0;padding:0;box-sizing:border-box;}......
  • 8.1 js addEventListener js给div添加事件
    <!DOCTYPEhtml><html><head><metacharset="utf-8"><title>testaddEventListener</title></head><body><buttonid="myBtn">clickme</button><pid="demo">&......
  • 界面控件DevExtreme v23.1抢先体验,增强的UI/UX自定义功能!
    DevExtreme拥有高性能的HTML5/JavaScript小部件集合,使您可以利用现代Web开发堆栈(包括React,Angular,ASP.NETCore,jQuery,Knockout等)构建交互式的Web应用程序,该套件附带功能齐全的数据网格、交互式图表小部件、数据编辑器等。本文的目的就是为了让开发者预览即将发布的DevExtreme功......
  • concurrentHashMap为什么是线程安全的?
    ConcurrentHashMap是线程安全的。它可以被多个线程同时使用而不需要额外的同步措施(比如使用synchronized)来保证线程安全。这是因为ConcurrentHashMap内部使用了一些非常高效的机制来保证线程安全,包括:分段锁:ConcurrentHashMap将数据分成多个段,每个段都有自己的锁。这样,在多线程......
  • Cesium 案例(三) Web Map Service(WMS) Washington DC 2017
    WMSCesium.Ion.defaultAccessToken="token";   constviewer=newCesium.Viewer("cesiumContainer");   //AddaWMSimagerylayer   constlayer=newCesium.ImageryLayer(    newCesium.WebMapServiceImageryProvider({ ......
  • uni-app:ios/android中的nvue和vue页面加载自定义字体(hbuilderx 3.7.3)
    一,官方文档地址:https://uniapp.dcloud.net.cn/tutorial/nvue-api.html#addrule二,代码1,nvue页面:模板<viewclass="listTitle">{{item.title}}</view>......