首页 > 其他分享 >一个List对象,想把特定的值排在最前面进行处理

一个List对象,想把特定的值排在最前面进行处理

时间:2023-11-07 14:36:09浏览次数:25  
标签:contains 值排 getOrderNo List add studentList 特定 Student new

今天遇到一个需求,要把list中的某些特定的值排在最前面处理,所以就要对list进行排序,搜索了一下进行总结

首先对List<String> 根据特定的值进行排序

    List<String> list = Arrays.asList("apple", "banana", "cherry", "date", "sss", "fig");
        List<String> specificValues = Arrays.asList("fig", "date");
        // 指定的特定值
        list.sort((a, b) -> {
            if (specificValues.contains(a)) {
                // a在指定值列表中,应排在前面
                return -1;
            } else if (specificValues.contains(b)){
                // b在指定值列表中,应排在前面
                return 1;
            } else {
                // 其他情况保持不变
                return 0;
             }
        });
        System.out.println(list);

 然后对一个List对象根据特定的值进行排序

首先去写一个简单的类
@Data public static class Student{ private String name; private String orderNo; private int classNum; public Student(String name, String orderNo, int i) { this.name = name; this.orderNo = orderNo; this.classNum = classNum; } } 然后main方法进行验证     List<Student> studentList = new ArrayList<Student>(); studentList.add(new Student("xiaopang","SN005",1)); studentList.add(new Student("xiaopang","SN002",1)); studentList.add(new Student("xiaopang","SN004",1)); studentList.add(new Student("xiaopang","SN001",1)); studentList.add(new Student("xiaopang","AN003",1)); studentList.add(new Student("xiaopang","AN001",1)); studentList.add(new Student("xiaopang","AN002",1)); // SN开头的再前面排列 studentList.sort((student1, student2) -> { if (student1.getOrderNo().contains("SN") && student2.getOrderNo().contains("SN")) { return student1.getOrderNo().compareTo(student2.getOrderNo()); } else if (student1.getOrderNo().contains("SN") && !student2.getOrderNo().contains("SN")) { return -1; } else if (!student1.getOrderNo().contains("SN") && student2.getOrderNo().contains("SN")) { return 1; } else { return student1.getOrderNo().compareTo(student2.getOrderNo()); } }); studentList.forEach(e->{ System.out.println(e.toString()); });

  

 

标签:contains,值排,getOrderNo,List,add,studentList,特定,Student,new
From: https://www.cnblogs.com/zhangliang88/p/17814903.html

相关文章

  • springboot nacos使用yaml配置list方式
    方式一配置项:app:demo:list1:xiaohong,xiaominglist2:>xiaohong,xiaominglist1和list2看起来是2种风格,其实都是同一种写法,以逗号分隔java代码:@Data@ComponentpublicclassAppConfig1{@Value("${app.demo.list1}")privateList<Strin......
  • js 数组按指定字段转map-list结构
    js数组按指定字段转map-list结构背景介绍在开发过程中经常会出现接口返回整个数组,我们需要将数组进行二次处理,如下格式按照不同功能模块(type)进行数据拆分原始数据constlist=[{"type":"red","id":1,"name":"a","count":1}, {"type":"red","......
  • .NET(C#) LinkedList、Queue<T>和Stack<T>的使用
    本文主要介绍.NET(C#)中,LinkedList链表、Queue<T>队列和Stack<T>堆栈的使用,以及相关的示例代码。1、LinkedList(链表)链表中元素存储内存中是不连续分配,每个元素都有记录前后节点,节点值可以重复,不能通过下标访问,泛型的使用保证类型安全,可以避免装箱拆箱,找元素就只能遍历,查找不方......
  • C#学习之ListBox,ComboBox,CheckListBox
    ListBox(文本列表)常用属性:Items:描述:ListBox中的项列表。默认值:空用法:可以使用Add(),AddRange(),Insert(),Remove(),Clear(),等方法来对Items集合进行管理。SelectedIndex:描述:ListBox中当前选择项的索引。默认值:-1(表示没有选中项)用法:通过设置或读取该属......
  • 通过一个特定条件获取下面的所有数据
    例:通过客户名称获取所有的还客户名称的合同编号 在接口方法中,首先执行数据库查询,使用客户名称作为条件,在数据库中查找具有相同客户名称的合同记录 这将返回一个ZyContract对象列表,这些对象代表了具有相同客户名称的合同记录。 接下来,通过以下代码,从匹配的合同记录中提......
  • [Listen][Download] Harry Potter Audiobooks Complete Series – All Harry Potter A
    HarryPotterAndTheCursedChild(#8inSeries)WrittenbyJKRowling在线音频,英文语音,mp3https://audiobookslab.com/harry-potter-and-the-cursed-child-audiobook/ ThePhilosopher’sStone(1997)TheChamberofSecrets(1998)ThePrisonerofAzkaban(1999)TheGoble......
  • 如果它是MySQL中的特定字符,则删除最后一个字符?
    要删除最后一个字符(如果是特定字符),请使用SUBSTRING()。让我们首先创建一个表-mysql>createtableDemoTable(SubjectNamevarchar(100));使用插入命令在表中插入一些记录-mysql>insertintoDemoTablevalues('MySQL');mysql>insertintoDemoTablevalues('MongoD......
  • springboot启动报错[main] o.a.catalina.core.AprLifecycleListener
    详细信息为:---[main]o.a.catalina.core.AprLifecycleListener:Anincompatibleversion[1.2.17]oftheApacheTomcatNativelibraryisinstalled,whileTomcatrequiresversion[1.2.34]解决方法:下载对应版本的native文件,地址为https://archive.apache......
  • java开发,json转list集合,原生实现
    java是一门面象对象的语言,对象需要先定义,但是在外理网络请求时候会用到json转成java对象,虽然现代开发框架中也提供了很多工具和方法直接转换,但是作为学习者了解一下底层实现,更能灵活变通现在有一个json格式如下{"list":[{"name":"Tom","sex":"b......
  • Linux创建特定用户运行应用程序
    我们知道Linxu分为内核态和用户态,用户态和内核态交互的桥梁就是shell,用户的应用程序通常运行在用户态,也就是用户空间,默认情况下,root用户拥有系统最高权限,很多时候我们在linux部署应用程序时,程序可能需要取得某些系统权限才能正常运行,比如在所属组为root的目录里新建一个*.pid文件,......