其中 Set代表无序、不可重复的集合; List代表有序、重复的集合; Map则代表具有映射关系的集合。 Queue体系集合代表一种队列集合实现。
使用Collections.sort对List集合排序
Collections.sort的两种重载形式
public static <T extends Comparable<? super T>> void sort(List<T> list) {
}
public static <T> void sort(List<T> list, Comparator<? super T> c) {
}
People.java
package sort;
public class People implements Comparable<People> {
public int age;
public String name;
public People(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* 这个方法定义排序的规则
*
* @param o
* @return
*/
@Override
public int compareTo(People o) {
return this.age - o.getAge();
}
@Override
public String toString() {
return "People{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
Student.java
package sort;
/**
* Created with IntelliJ IDEA.
* User: ASUS
* Date: 14-9-10
* Time: 下午10:39
* To change this template use File | Settings | File Templates.
*/
public class Student {
public String name;
public int age;
public Student(String name, int age) {
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
Teacher.java
package sort;
import java.util.Comparator;
/**
* Created with IntelliJ IDEA.
* User: ASUS
* Date: 14-9-10
* Time: 下午11:24
* To change this template use File | Settings | File Templates.
*/
public class Teacher implements Comparator<Teacher> {
public int age;
public Teacher() {
}
public Teacher(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compare(Teacher o1, Teacher o2) {
return o1.getAge() - o2.getAge();
}
@Override
public String toString() {
return "Teacher{" +
"age=" + age +
'}';
}
}
排序的测试:
package sort;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: ASUS
* Date: 14-9-10
* Time: 下午10:32
* To change this template use File | Settings | File Templates.
*/
public class SortTest {
/**
* 排序List<String> 升序排序
* 排序规则是String类的compareTo
*/
@Test
public void test() {
//sort方法的第一种使用方法
List<String> list1 = new ArrayList<String>();
list1.add("b");
list1.add("a");
list1.add("c");
list1.add("e");
list1.add("g");
list1.add("j");
list1.add("w");
list1.add("b");
Collections.sort(list1); //默认的按升序排序
for (String s : list1) {
System.out.println(s);
}
}
/**
* 排序List<String> 降序排序
* 排序规则是String类的compareTo
*/
@Test
public void test1212() {
List<String> list1 = new ArrayList<String>();
list1.add("b");
list1.add("a");
list1.add("c");
list1.add("e");
list1.add("g");
list1.add("j");
list1.add("w");
list1.add("b");
Collections.sort(list1, Collections.reverseOrder());
for (String s : list1) {
System.out.println(s);
}
}
/**
* 反转元素的顺序
*/
@Test
public void test7675() {
List<String> list1 = new ArrayList<String>();
list1.add("b");
list1.add("a");
list1.add("c");
list1.add("e");
list1.add("g");
list1.add("j");
list1.add("w");
list1.add("b");
Collections.reverse(list1);
for (String s : list1) {
System.out.println(s);
}
}
/**
* People类实现Comparable接口,实现compareTo方法。
* 在compareTo方法中定义排序规则
*/
@Test
public void test89() {
//sort的第二种使用方法
List<People> peoples = new ArrayList<People>();
peoples.add(new People(12, "lyx"));
peoples.add(new People(13, "lyx"));
peoples.add(new People(4, "lyx"));
peoples.add(new People(1, "lyx"));
Collections.sort(peoples);
for (People p : peoples) {
System.out.println(p.toString());
}
}
/**
* Teacher类实现Comparator接口,实现compare方法
*/
@Test
public void test86() {
List<Teacher> teachers = new ArrayList<Teacher>();
teachers.add(new Teacher(12));
teachers.add(new Teacher(1));
teachers.add(new Teacher(5));
teachers.add(new Teacher(2));
teachers.add(new Teacher(9));
Collections.sort(teachers, new Teacher());
for (Teacher t : teachers) {
System.out.println(t.toString());
}
}
/**
* 在sort方法中,传入Comparator接口的匿名对象,实现compare方法
* 在compare方法中定义排序规则
*/
@Test
public void test80990() {
//sort的第三种使用方法
List<Student> students = new ArrayList<Student>();
students.add(new Student("lyx", 12));
students.add(new Student("lyx", 1));
students.add(new Student("lyx", 7));
students.add(new Student("lyx", 4));
students.add(new Student("lyx", 3));
//实现Comparator接口(使用匿名内部类实现Comparator接口)
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge() - o2.getAge();
}
});
for (Student s : students) {
System.out.println(s.toString());
}
}
}
LinkedHashSet以元素插入顺序遍历元素
(1)HashSet是Set接口的实现。HashSet按Hash算法来存储集合中的元素,具有很好的存取和查找性能。
(2)HashSet不能保证元素的排列顺序,顺序可能与添加顺序不同,顺序也有可能发生变化。
(3)当向HashSet集合中存入一个元素时,HashSet会调用该对象的hashCode()方法来得到该对象的hashCode值,然后根据该HashCode值决定该对象在HashSet中的存储位置。如果有两个元素通过equals()方法比较返回true,但它们的hashCode()方法返回值不相等,HashSet将会把它们存储在不同的位置,依然可以添加成功。即,HashSet集合判断两个元素相等的标准是两个对象通过equals()方法比较相等,并且两个对象的hashCode()方法返回值也相等。
LinkedHashSet集合也是根据元素的hashCode值来决定元素的存储位置,但它同时使用链表维护元素的次序,这样使得元素看起来是以插入的顺序保存的。也就是说,当遍历LinkedHashSet集合里的元素时,LinkedHashSet将会按元素的添加顺序来访问集合里的元素。
package sort;
import org.junit.Test;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* Created with IntelliJ IDEA.
* User: ASUS
* Date: 14-9-10
* Time: 下午10:55
* To change this template use File | Settings | File Templates.
*/
public class SortTest2 {
/**
* 测试LinkedHashSet
* LinkedHashSet集合也是根据元素的hashCode值来决定元素的存储位置,
* 但它同时使用链表维护元素的次序,这样使得元素看起来是以插入的顺序保存的。
*/
@Test
public void test867() {
Set<String> stringSet = new HashSet<String>();
stringSet.add("aasdfasdf");
stringSet.add("b");
stringSet.add("c");
stringSet.add("dddddd");
stringSet.add("e");
for (String s : stringSet) {
System.out.println(s);
}
/**
* 这是打印结果,可以看到不是按照插入顺序排列的
* b
* c
* dddddd
* aasdfasdf
* e
*/
System.out.println();
Set<String> set = new LinkedHashSet<String>();
set.add("aasdfasdf");
set.add("b");
set.add("c");
set.add("dddddd");
set.add("e");
for (String s : set) {
System.out.println(s);
}
/**
* 可以看到LinkedHashSet是按插入顺序遍历的,
* 要注意其保存顺序还是按照hashCode值来决定
* aasdfasdf
* b
* c
* dddddd
* e
*/
}
}
TreeSet使集合处于排序状态
TreeSet是SortedSet接口的实现类,可以确保集合元素处于排序状态。
TreeSet需要额外的红黑树算法来维护集合元素的次序。只有当需要一个保持排序的Set时,才应该使用TreeSet,否则都应该使用HashSet。
TreeSet中的几个方法:
-
Object first():返回集合中的第一个元素。
-
Object last():返回集合中的最后一个元素。
-
Object lower(Object e):返回集合中位于指定元素之前的元素(即小于指定元素的最大元素,参数元素不需要是TreeSet集合里的元素)。
-
Object higher(Object e):返回集合中位于指定元素之后的元素(即大于指定元素的最小元素,参数元素不需要是TreeSet集合里的元素)。
-
SortedSet subSet(formElement,toElement):返回次Set的子集合,范围从formElement(包含)到toElement(不包含)。
-
SortedSet headSet(toElement):返回此Set的子集,由小于toElement的元素组成。
-
SortedSet tailSet(fromElement):返回此Set的子集,由大于或等于fromElement的元素组成。
下面贴代码:
@Test
public void test323() {
Set<People> peoples = new TreeSet<People>();
peoples.add(new People(12, "lyx"));
peoples.add(new People(13, "lyx"));
peoples.add(new People(4, "lyx"));
peoples.add(new People(1, "lyx"));
for (People p : peoples) {
System.out.println(p.toString());
}
}
@Test
public void test80990() {
//sort的第三种使用方法
Set<Student> students = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge() - o2.getAge();
}
});
students.add(new Student("lyx", 12));
students.add(new Student("lyx", 1));
students.add(new Student("lyx", 7));
students.add(new Student("lyx", 4));
students.add(new Student("lyx", 3));
for (Student s : students) {
System.out.println(s.toString());
}
}
LinkedHashMap以元素插入顺序遍历元素
LinkedHashMap是HashMap的子类,使用双向链表来维护entry的插入次序,迭代输出时,元素顺序与插入顺序一致。
/**
* LinkedHashMap是HashMap的子类,使用双向链表来维护entry的插入次序,迭代输出时,元素顺序与插入顺序一致。
*/
@Test
public void test121() {
Map<String, String> map0 = new HashMap<String, String>();
map0.put("qwer", "value");
map0.put("b", "value");
map0.put("c", "value");
map0.put("jklkjh", "value");
map0.put("a", "value");
Set<String> keySet = map0.keySet();
for (String s : keySet) {
System.out.println(s);
}
System.out.println();
Map<String, String> map1 = new LinkedHashMap<String, String>();
map1.put("qwer", "value");
map1.put("b", "value");
map1.put("c", "value");
map1.put("jklkjh", "value");
map1.put("a", "value");
Set<String> keySet1 = map1.keySet();
for (String s : keySet1) {
System.out.println(s);
}
}
打印结果:
a
b
c
qwer
jklkjh
qwer
b
c
jklkjh
a
TreeMap使集合处于排序状态
TreeMap采用“红黑树”的排序二叉树来保存Map中的每个Entry。每个Entry为红黑树的一个节点。
所有的Entry总是根据key按指定的规则保持有序。红黑树是一种自平衡二叉树,每个节点的值都大于或等于它的左子树中所有节点的值,都小于或等于它的右子树中所有节点的值。
/**
* 使用TreeMap使集合处于排序状态
*/
@Test
public void test78728() {
Map<Integer, String> map0 = new TreeMap<Integer, String>();
map0.put(1, "value");
map0.put(90, "value");
map0.put(89, "value");
map0.put(34, "value");
map0.put(21, "value");
map0.put(3, "value");
Set<Integer> keySet1 = map0.keySet();
for (Integer i : keySet1) {
System.out.println(i);
}
}
/**
* TreeMap集合排序的对象必须要实现Comparable接口
* 实现compareTo方法
*/
@Test
public void test898686() {
Map<People, Integer> peopleIntegerMap = new TreeMap<People, Integer>();
peopleIntegerMap.put(new People(12, "lyx"), 9876);
peopleIntegerMap.put(new People(9, "lyx"), 9876);
peopleIntegerMap.put(new People(2, "lyx"), 9876);
peopleIntegerMap.put(new People(8, "lyx"), 9876);
peopleIntegerMap.put(new People(7, "lyx"), 9876);
peopleIntegerMap.put(new People(6, "lyx"), 9876);
peopleIntegerMap.put(new People(5, "lyx"), 9876);
Set<People> peopleSet = peopleIntegerMap.keySet();
for (People people : peopleSet) {
System.out.println(people.toString());
}
}
/**
* 实例化TreeMap时,传入Comparator接口的匿名类对象
* 实现compare方法,定义排序规则
*/
@Test
public void test7868() {
Map<Student, Integer> studentIntegerMap = new TreeMap<Student, Integer>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge() - o2.getAge();
}
});
studentIntegerMap.put(new Student("lyx", 12), 7890);
studentIntegerMap.put(new Student("lyx", 11), 7890);
studentIntegerMap.put(new Student("lyx", 10), 7890);
studentIntegerMap.put(new Student("lyx", 9), 7890);
studentIntegerMap.put(new Student("lyx", 8), 7890);
studentIntegerMap.put(new Student("lyx", 7), 7890);
studentIntegerMap.put(new Student("lyx", 6), 7890);
studentIntegerMap.put(new Student("lyx", 5), 7890);
Set<Student> studentSet = studentIntegerMap.keySet();
for (Student student : studentSet) {
System.out.println(student.toString());
}
}
==============END==============