目录
1.集合 = Collection+Map
2.实现集合排序
1.集合 = Collection+Map
(Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射。)
Collection = Set + List + Queue
Set = HashSet (乱序)
+ LinkedHashSet (按插入有序)
+ TreeSet
List = ArrayList(数组实现)
+ LinkedList(链表实现
Map = HashMap(散列图,null可以为key)
+ LinkedHashMap(链式散列图,按插入顺序或者访问顺序排序,双链表)
+ TreeMap (树形图)
2.实现集合排序
(1)实现Comparable接口的compareTo方法
(2)实现Comparator接口的compare方法
import java.util.*;
/**
* java.lang.Comparable的compareTo方法是把排序逻辑写在类里边,自然排序,且 e1.compareTo(e2) == 0 的结果要和 e1.equals(e2)一致
* java.util.Comparator的compare方法是把排序逻辑写在类外边,方便定制化。
*
*/
public class CollectionTest {
public static void main(String[] args) {
Collection c;
LinkedList llist;
ArrayList alist;
//Collections.sort();
LinkedList list1;
ArrayList list2;
String ss;
StringBuffer s1;
StringBuilder s2;
ArrayListlist = new ArrayList();
list.add( new Student("s60", 60));
list.add( new Student("s40", 40));
list.add( new Student("s80", 80));
list.add( new Student("s50", 50));
Iterator it = list.iterator();
while(it.hasNext()) {
System.out.println(((Student)it.next()).getName());
}
Collections.sort(list,new SortByScore());
for (Student s: list) {
System.out.println(s.getName());
}
System.out.println("------Student2 start------");
ArrayListlistStudent2 = new ArrayList();
listStudent2.add( new Student2("s60", 60));
listStudent2.add( new Student2("s40", 40));
listStudent2.add( new Student2("s80", 80));
listStudent2.add( new Student2("s50", 50));
Iterator it2 = listStudent2.iterator();
while(it2.hasNext()) {
System.out.println(((Student2)it2.next()).getName());
}
Collections.sort(listStudent2);
for (Student2 s: listStudent2) {
System.out.println(s.getName());
}
System.out.println("------Student2 end------");
Map map = new HashMap();
map.put("s60", 60);
map.put("s40", 40);
map.put("s80", 80);
map.put("s50", 50);
Iterator mapit = map.entrySet().iterator();
while (mapit.hasNext()) {
Map.Entryentry = (Map.Entry)mapit.next();
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
static class Student {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
String name;
int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
}
static class SortByScore implements Comparator{
public int compare(Student s1, Student s2) {
if (s1.score > s2.score) {
return 1;
} else if (s1.score < s2.score) {
return -1;
} else {
return 0;
}
}
}
static class Student2 implements Comparable{
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
String name;
int score;
public Student2(String name, int score) {
this.name = name;
this.score = score;
}
//s
public int compareTo(Object o) {
Student2 s2 = (Student2) o;
if (this.score > s2.score) {
return 1;
} else if (this.score < s2.score) {
return -1;
} else {
return 0;
}
}
}
}