ArrayList等常见集合的排序问题

对于ArrayList等常用的集合具体业务类,基本上都实现了Comparable接口,即可以用来比较装载的对象实体。

主要用Collections.sort方法对集合类中的对象进行排序

Collections.sort的两种重载方法

1.Collections.sort(list, comparator)方法,通过comparator规则,实现对list的特定排序。

2.Collections.sort(list),list中的对象自身实现comparator接口

Java集合框架:

代码示例(演示Collections.sort(list, comparator)方法):

注意:本代码均已在`jdk1.6`版本下通过测试

model,Student类

public class Student {
    private int id;
    private String name;
    private int age;

    /**
     * @Title: Student
     * @Description: TODO
     * @param:
     * @throws
     */
    public Student(int id, String name, int age) {
        // TODO Auto-generated constructor stub
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return String.format("Student [age=%s, name=%s, id=%s]", age, name, id);
    }
}
测试类
public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<Student> arrayList = new ArrayList<Student>();
        Student s1 = new Student(1, "jack", 20);
        Student s2 = new Student(2, "jack", 20);
        Student s3 = new Student(3, "lily", 29);
        Student s4 = new Student(4, "tom", 30);
        Student s5 = new Student(5, "rose", 31);
        Student s6 = new Student(6, "crane", 20);
        Student s7 = new Student(7, "jack", 25);
        Student s8 = new Student(8, "rose", 27);
        Student s9 = new Student(9, "lucy", 18);

        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        arrayList.add(s4);
        arrayList.add(s5);
        arrayList.add(s6);
        arrayList.add(s7);
        arrayList.add(s8);
        arrayList.add(s9);
        Comparator<Student> studentComparator = new Comparator<Student>() {

            /**
             *
             * @Title: compare
             * @Description: 先比较age,再比较name,最后比较id
             * @param: @param o1
             * @param: @param o2
             * @param: @return
             * @return: int
             * @throws
             */
            @Override
            public int compare(Student o1, Student o2) {
                // TODO Auto-generated method stub
                if (o1.getAge() != o2.getAge()) {
                    return o1.getAge() - o2.getAge();
                } else if (!o1.getName().equals(o2.getName())) {
                    return o1.getName().compareTo(o2.getName());
                } else if (o1.getId() != o2.getId()) {
                    return o1.getId() - o2.getId();
                }
                return 0;
            }
        };
        Collections.sort(arrayList, studentComparator);

        for (Student student : arrayList) {
            System.out.println(student.toString());
        }
    }
测试结果
Student [age=18, name=lucy, id=9]
Student [age=20, name=crane, id=6]
Student [age=20, name=jack, id=1]
Student [age=20, name=jack, id=2]
Student [age=25, name=jack, id=7]
Student [age=27, name=rose, id=8]
Student [age=29, name=lily, id=3]
Student [age=30, name=tom, id=4]
Student [age=31, name=rose, id=5]
时间: 2024-07-30 20:32:04

ArrayList等常见集合的排序问题的相关文章

c# 集合ArrayList;特殊集合Stack、Queue

一)  ArrayList 1.foreach遍历数组中各个元素,执行内部语句 2.  3. 4.  myarry.Clear();//将集合清空 bool b = myarry.Contains(3);//判断是否有括号内的数据,返回的是bool值(True或者False) int bb = myarry.IndexOf(2);int cc = myarry.LastIndexOf(2);Console.WriteLine(bb); ArrayList ar = new ArrayList()

常见集合知识

java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类,分别是HashMap Hashtable LinkedHashMap 和TreeMap. Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复了覆盖了),但允许值重复.Hashmap 是一个最常用的Map,它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度,遍历时,取得数据的顺序是完全随机的. HashMap最多只允许一条记录的键为Null;允许多条记录的值为 Null;

CSV-03- csv 读写框架支持数组、Map、Collection 等常见集合

集合类 有时候对象中会包含数组.Map.Collection 等常见集合. 为了存储的便利性,默认提供集合的相关支持. 特性和普通字段保持一致,如果指定注解转换,则以注解为准. 使用示例 UserCollection.java 用于演示集合的对象 public class UserCollection { private String[] arrays; private LinkedList<String> lists; private Map<String, String> ma

List、Set、Map常见集合遍历总结

Java中的集合有三大类,List.Set.Map,都处于java.util包中,List.Set和Map都是接口,不能被实例化,它们的各自的实现类可以被实例化.List的实现类主要有ArrayList,Set的实现类主要有HashSet和TreeSet,Map的实现类主要有HashMap和TreeMap. List中的对象有序,可以重复,允许按照对象在集合中的索引位置检索对象,如通过list.get(i)方式来获得List集合中的元素. Set中的对象无序,不可以重复.但它的有些实现类能对集合

JAVA中几种常见集合的使用实例

Java.util.ArrayList(类): *;import java.util.*;public class CollectionTest{//List是一个能包含重复元素的已排序的Collection,有时list也称为序列,List第一个元素的下标为0      public String colors[]={"red","white","blue"};//定义一个字符数组     //构造函数     public Collectio

C#中ArrayList和泛型集合List方法概述概述

首先,ArrayList集合和List都是非静态类,使用时都必须先实列化. ArrayList类似一个数组,但比数组优越的是它的长度大小可变,并且可以存放不同类型的变量,包括值类型和引用类型. 常用方法如下: 1.Add():方法用于添加一个元素到当前列表的末尾 2.AddRange():方法用于添加一批元素(数组或者集合)到列表的末尾 3.Remove():方法用于删除一个元素,通过元素本身的引用来删除,填什么删什么 4.RemoveAt():方法用于删除一个元素,通过索引值来删除 5.Rem

.Net学习笔记----2015-06-24(还是继承:ArrayList 和 Hashtable 集合)

protected 受保护的:可以在当前类的内部以及该类的子类中访问. ArrayList 集合:很多数据的一个集合 数组:长度不可变,类型单一 集合的好处:长度可以任意改变,类型随便放 ArrayList list = new ArrayList(); list.Add();添加单个元素 list.AddRange();添加集合 罗列ArrayList的各种用法 using System; using System.Collections.Generic; using System.Linq;

Java常见集合的默认大小及扩容机制

在面试后台开发的过程中,集合是面试的热话题,不仅要知道各集合的区别用法,还要知道集合的扩容机制,今天我们就来谈下ArrayList 和 HashMap的默认大小以及扩容机制. 在 Java 7 中,查看源码可以知道:ArrayList 的默认大小是 10 个元素,HashMap 的默认大小是16个元素(必须是2的幂,为什么呢???下文有解释).这就是 Java 7 中 ArrayList 和 HashMap  类 的代码片段: // from ArrayList.java JDK 1.7 pri

Java之ArrayList类(集合)

集合的由来 我们想存储多个数据,选择的容器可以是数组.而数组的长度是固定的,无法适应数据变化的需求.为了解决这个问题,Java提供了另一个容器 java.util.ArrayList 集合类,让我们可以更便捷的存储和操作对象数据.数组的长度不可以发生改变.但是ArrayList集合的长度是可以随意变化的. ArrayList类概述 java.util.ArrayList 是大小可变的数组的实现,存储在内的数据称为元素.此类提供一些方法来操作内部存储的元素. ArrayList 中可不断添加元素,