Collection中List,Set的排序
来源:互联网 发布:淘宝正常一天能出几单 编辑:程序博客网 时间:2024/06/17 15:29
排序分为两种
1. 自然排序:自定义类需实现Comparable接口,依据CompareTo方法,确定按照哪一个或哪几个属性排序,如果返回值为0,则表示两个自定义对象相同,(如果需要降序,就在CompareTo的返回值前面加一个“-”号)。【如果可以操作一个类,就用自然排序】
2. 定制排序:实现Comparator接口,在重写compare方法时指明按照自定义类的哪个属性排序,并生成一个对象;【如果不可以操作一个类,就用定制排序】
两种排序都要使用Collections.sort()来排序。
基本用法如下:
list升序,降序,逆序
List<Integer>
list =new ArrayList<Integer>
();
//如果list是 5 7 2 6 8 1 4
1、升序:
Collections.sort(list) //list: 1 2 4 5 6 7 8
2、降序:
Collections.sort(list,Collections.reverseOrder())//list:8 7 6 5 4 2 1
3、逆序:
Collections.reverse(list)//list:4 1 8 6 2 7 5
数组升序,降序
int[] a={5 7 2 6 8 1 4}
1、升序:
Arrays.sort(a)//a: 1 2 4 5 6 7 8
2、降序:
Arrays.sort(a,Collections.reverseOrder())//a: 8 7 6 5 4 2 1
补充:
Arrays.asList(数组)//将数组转成list
Arrays.asList(a);
List<Integer>
list =new ArrayList<Integer>
(Arrays.asList(a));
例子
Collections使用sort(List) 根据元素的自然顺序对指定List 集合元素按升序排序;
Collections使用 sort(List,Comparator) 根据指定的Comparator 产生的顺序对 List 集合元素进行排序。
使用sort(List)
@Testpublic void test1(){ //排序操作List ls=new ArrayList();ls.add(82);ls.add(93);ls.add(34);ls.add(25);ls.add(46);ls.add(27);Collections.sort(ls);for(Object o:ls)System.out.println(o);}
使用 sort(List,Comparator)
Customer类
public class Customer {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 Customer() {super();}public Customer(String id, String name) {super();this.id = id;this.name = name;}@Overridepublic String toString() {return "Customer [id=" + id + ", name=" + name + "]";}}
测试类,实现Comparator接口实现排序
@Testpublic void testOrder(){List lps=new ArrayList<Customer>();for(Integer i=0;i<10;i++){lps.add(new Customer(i.toString(),i+"name"));}Collections.sort(lps,new Comparator<Customer>() {@Overridepublic int compare(Customer c1, Customer c2) {//升序return c1.getName().compareTo(c2.getName());//降序//return c2.getName().compareTo(c1.getName());}});Iterator is = lps.iterator();while(is.hasNext()){System.out.println(is.next());}}
使用sort(List)并在类中实现Comparable接口
Person类
public class Person implements Comparable{String name;Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(int age) {this.age = age;}@Deprecatedpublic Person(String name, int age) {super();this.name = name;this.age = age;}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + ", getName()=" + getName() + ", getAge()=" + getAge()+ ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString()+ "]";}public Person() {super();}@Overridepublic int compareTo(Object o) {if(o instanceof Person){Person p=(Person) o;int i=this.name.compareTo(p.name);if(i==0){i=this.age.compareTo(p.age);return i;}else{return i;}}return 0;}}
测试类
@Testpublic void testOrder2(){List lps=new ArrayList<Person>();for(Integer i=0;i<10;i++){lps.add(new Person(i+"name",i*10+i));}Collections.sort(lps);Iterator is = lps.iterator();while(is.hasNext()){System.out.println(is.next());}}
- Collection中List,Set的排序
- Java Collection中 List 与 Set,TreeSet排序方法
- 有关JAVA中collection和set和list的用法,以及元素的升序排序方法
- Collection,Set,List的区别
- Collection、Set、List的区别
- Collection、Set、List的区别
- java 中 Collection, List, Map, Set的总结
- Java Collection中List和Set的区别
- JAVA基础-Collection中List,Set,Map的比较总结
- Java中collection set list map
- java中collection、map、set、list简介
- Collection中List和Set、Map学习
- List和Set中忽略的方法addAll(Collection c)和retainAll(Collection c)
- List和Set中忽略的方法addAll(Collection c)和retainAll(Collection c)
- Collection List Set Map的详细分析
- Collection List Set Map的那些事
- Java Collection List Set Map 的区别
- Collection、List、Set、Map之间的关系
- Oracle之经典查询
- Linux编译更新内核
- QT 学习之------ 读写ini文件
- 进程的三种基本状态及其转换
- git 学习
- Collection中List,Set的排序
- js中json对象key值首字母大写化
- TCP为什么需要3次握手与4次挥手
- C++ string类型
- Pythonic QuickSort
- 第三周项目一C/C++顺序表的基本运算
- 【转】linux grep命令
- UNIX 设置 JAVA 环境变量
- JDBC技术简介