• Collentions工具类--java.util.Collections

  Collentions是Java集合框架中,用来操作集合对象的工具类,也是Java集合框架的成员,与List、Map和Set是并列的。

  Collections.sort() 排序方法,实现对List对象中的元素进行排序.

package com.test.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random; public class CollectionsTest { /**
* Integer泛型的List进行排序
*/
public void testSort1(){
List<Integer> integerList = new ArrayList<Integer>();
//插入10个100以内不重复的随机整数
Random random = new Random();
Integer num;
for (int i = 0; i < 10; i++) {
do {
num = random.nextInt(100);
} while(integerList.contains(num));
integerList.add(num);
System.out.println("成功插入整数:" + num);
}
System.out.println("==============排序前=============");
for (Integer integer : integerList) {
System.out.print(integer + " ");
}
//调用Collections.sort()方法排序
Collections.sort(integerList);
System.out.println("==============排序后=============");
for (Integer integer : integerList) {
System.out.print(integer + " ");
}
}
/**
* String泛型的List进行排序
   * 字符串类型进行比较,先数字后字母,数字0-9,字母A-Za-z
*/
public void testSort2() {
List<String> stringList = new ArrayList<String>();
//添加3个乱序的String元素
stringList.add("google");
stringList.add("lenovo");
stringList.add("baidu");
System.out.println("==============排序前=============");
for (String string : stringList) {
System.out.println(string);
}
Collections.sort(stringList);
System.out.println("==============排序后=============");
for (String string : stringList) {
System.out.println(string);
}
} public static void main(String[] args) {
CollectionsTest ct = new CollectionsTest();
ct.testSort1();
ct.testSort2();
} }
  • Comparable接口和Comparator接口

  在Java中,如果两个对象需要进行排序,那么它们必须是可以比较的。用Comparable这个接口表示某个对象是可以比较的。Comparable相当于给对象定义了默认的排序规则,而如果改用其他规则进行排序,可用Comparator接口,它定义了临时比较规则。Comparable接口和Comparator接口,都是Java集合框架的成员。

  Comparable接口:

  1. 实现该接口表示:这个类的实例可以比较大小,可以进行自然排序;
  2. 定义了默认的比较规则
  3. 其 实现类需实现compareTo()方法
  4. Obja.compareTo(Obj2)方法返回正数表示a比b大,负数表示a比b小,0表示a和b相等

  Comparator接口:

  1. 用于定义临时比较规则,而不是默认比较规则
  2. 其 实现类需要实现compare()方法
  3. 用法:
    Collections.sort(List<T> list, Comparator<? super T> c),根据指定比较器产生的顺序对指定列表进行排序。

  实例:学生系列排序,默认按学生id来排序,先随机生成3个不相同的1000以内的整数做为学生的id;然后再按学生姓名来排序。

package com.test.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random; public class CollectionsTest { public void testSort() {
List<Student> studentList = new ArrayList<Student>();
List<Integer> studentId = new ArrayList<Integer>();
Random random = new Random();
Integer num;
for (int i = 0; i < 3 ; i++) {
do {
num = random.nextInt(1000);
} while (studentId.contains(num));
studentId.add(num);
} studentList.add(new Student(studentId.get(0) + "", "Tom"));
studentList.add(new Student(studentId.get(1) + "", "Jack"));
studentList.add(new Student(studentId.get(2) + "", "Xiaoming"));
studentList.add(new Student(1000 + "", "Lily")); System.out.println("===========排序前=============");
for (Student student : studentList) {
System.out.println("学生:" + student.id + "——" + student.name);
}
Collections.sort(studentList);
System.out.println("===========按照id排序后=============");
for (Student student : studentList) {
System.out.println("学生:" + student.id + "——" + student.name);
}
System.out.println("===========按照姓名排序后========");
Collections.sort(studentList, new StudentComparator());
for (Student student : studentList) {
System.out.println("学生:" + student.id + "——" + student.name);
}
} public static void main(String[] args) {
CollectionsTest ct = new CollectionsTest();
ct.testSort();
} }

  执行结果:

===========排序前=============
学生:118——Tom
学生:460——Jack
学生:51——Xiaoming
学生:1000——Lily
===========排序后=============
学生:1000——Lily
学生:118——Tom
学生:460——Jack
学生:51——Xiaoming
===========按照姓名排序后========
学生:460——Jack
学生:1000——Lily
学生:118——Tom
学生:51——Xiaoming

  其中,Student类需要实现Comparable接口的compareTo()方法,StudentComparator类需要实现Comparator接口的compare()方法:

  Student.java

package com.test.collection;

import java.util.HashSet;
import java.util.Set;
/**
* 学生类
* @author Administrator
*
*/
public class Student implements Comparable<Student> {
public String id;
public String name;
public Set<Course> courses;//所选课程
public Student(String id, String name) {
this.id = id;
this.name = name;
this.courses = new HashSet<Course>();//实例化sourses(Set是接口,接口不能被直接实例化)
}
@Override
public int compareTo(Student o) {
return this.id.compareTo(o.id);
}
}

  StudentComparator.java

package com.test.collection;

import java.util.Comparator;

public class StudentComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o1.name.compareTo(o2.name);
}
}

Java—集合框架 Collections.sort()、Comparable接口和Comparator接口的更多相关文章

  1. java集合框架(Collections Framework)

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  2. Java集合框架Collections【List/Set】

    1.基本介绍: 集合就是存放对象的,他比数组好的一点就是他一开始不清楚自己长度 容器一般是分为很多种的,很多的容器在一起然后进过断的抽象和抽取就成了一个体系,我们称之为集合框架 我们看体系首先是看顶层 ...

  3. Java集合框架之Set接口浅析

    Java集合框架之Set接口浅析 一.java.util.Set接口综述: 这里只对Set接口做一简单综述,其具体实现类的分析,朋友们可关注我后续的博文 1.1Set接口简介 java.util.se ...

  4. 浅入深出之Java集合框架(下)

    Java中的集合框架(下) 由于Java中的集合框架的内容比较多,在这里分为三个部分介绍Java的集合框架,内容是从浅到深,哈哈这篇其实也还是基础,惊不惊喜意不意外 ̄▽ ̄ 写文真的好累,懒得写了.. ...

  5. java 集合框架小结

    一:集合框架  集合框架是为表示和操作集合而规定的一种统一的标准的体系结构.  任何集合框架都包含三大块内容:对外的接口.接口的实现和对集合运算的算法.   接口:即表示集合的抽象数据类型.Colle ...

  6. python 中的sort 和java中的Collections.sort()函数的使用

    x=[1,2,3] x.sort()对的,x这个都变了 y=x.sort()错误 y=sorted(x)对的,x拍好序的一个副本 python中用匿名函数和自定义函数排序:(很奇怪的是比较函数返回的是 ...

  7. Java—集合框架List

    集合的概念 现实生活中:很多的事物凑在一起 数学中的集合:具有共同属性的事物的总和 Java中的集合类:是一种工具类,就像是容器,存储任意数量的具有共同属性的对象 集合的作用 在类的内部,对数据进行组 ...

  8. (Set, Map, Collections工具类)JAVA集合框架二

    Java集合框架部分细节总结二 Set 实现类:HashSet,TreeSet HashSet 基于HashCode计算元素存放位置,当计算得出哈希码相同时,会调用equals判断是否相同,相同则拒绝 ...

  9. 牛客网Java刷题知识点之Java 集合框架的构成、集合框架中的迭代器Iterator、集合框架中的集合接口Collection(List和Set)、集合框架中的Map集合

    不多说,直接上干货! 集合框架中包含了大量集合接口.这些接口的实现类和操作它们的算法. 集合容器因为内部的数据结构不同,有多种具体容器. 不断的向上抽取,就形成了集合框架. Map是一次添加一对元素. ...

随机推荐

  1. checkstyle 各标签 (有几个没翻译,不懂意思)

    以下是对checkstyle 7.8.1 version各标签的翻译,有少数几个标签没翻译,不太懂官网的意思,就空了,希望游客能帮忙补充补充,另外有错的话也希望大家留言下哈,另外转载的话请标明一下 1 ...

  2. 【算法笔记】B1028 人口普查

    1028 人口普查 (20 分) 某城镇进行人口普查,得到了全体居民的生日.现请你写个程序,找出镇上最年长和最年轻的人. 这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 2 ...

  3. Luogu P1156 垃圾陷阱 DP

    f[i][j]表示在第i个垃圾,高度为j的最大生命值 转移分三部分: 如果j>=当前垃圾的高度,且两个垃圾间的时间小于等于上一个状态f[i-1][j-a[i].v]的生命值,则可以垫高度 如果j ...

  4. Olesya and Rodion (思维)

    Olesya loves numbers consisting of n digits, and Rodion only likes numbers that are divisible by t. ...

  5. ElementUI 时间控件

    <template> <div class="block"> <span class="demonstration">默认& ...

  6. [水题AC乐] - 贪心

    HDU - 1009 https://paste.ubuntu.com/p/rgSYpSKkwW/ POJ - 1017 麻烦的模拟 贪心 题意就是用尽量少的66h箱子装nnh的物品,贪心策略很明显, ...

  7. FFMpeg 版本错误

    错误情况: relocation error: /usr/local/lib/libavfilter.so.3: symbol sws_get_class, version LIBSWSCALE_2 ...

  8. PIE SDK中值滤波

    1.算法功能简介 中值滤波是一种最常用的非线性平滑滤波器,它将窗口内的所有像素值按高低排序后,取中间值作为中心像素的新值. 中值滤波对噪声有良好的滤除作用,特别是在滤除噪声的同时,能够保护信号的边缘, ...

  9. Knime读取Jason数据

    Knime ETL 工具 Jason数据解析到DB 1. 下面例子是一段Jason代码 [{,,},{,,},{,,}] 2. 用文本文件存储上面代码. test_jason.txt 3. 用File ...

  10. PIXI 精灵及文本加载(4)

    预习下官网的知识. 及字母消除接上文 Pixi 精灵 Pixi拥有一个精灵类来创建游戏精灵.有三种主要的方法来创建它: 用一个单图像文件创建. 用一个 雪碧图 来创建.雪碧图是一个放入了你游戏所需的所 ...