所谓框架就是一个类库的集合。集合框架就是一个用来表示和操作集合的同意架构,包含了实现集合的接口和类。Java中的集合框架结构图如下:
Collection (i) Map(i)
/ \ |
/ \ |
Set(i) List(i) SortedMap(i)
/
SortedSet(i)
区别:
Collection: 集合层次中的根接口。
Set: 不能包含重复的element。SortedSet按照升序排列elements的Set。
List: 有序(不是排序,而是指elements按照一定的顺序排列),可以包含重复element,提供了索引访问的方式。
△ArrayList
1) ArrayList 我们可以看作是一个可以自动增长的数组,这是和数组的不同之处。
2) 利用ArrayList的toArray()方法返回一个对象数组。
3) Arrays的asList()返回一个列表。 注: 返回固定尺寸的列表。asList()返回的列表不支持remove()方法。
4) 迭代器(Iterator)提供了一组访问集合的通用方法。 hasNext(), next(), remove(),调用remove()方法之前必须至少调用一次next().
5) 类Collections与Arrays,前者对列表排序,后者对数组排序。
* 当我们在打印一个集合类的对象的时候,它会调用集合类中的toString()方法,所以我们自定义的类就必须重写toString()方法.
* List stooges = Arrays.asList("Larry", "Moe", "Curly");
* public static void printElements(Collecion c){
Iterator it = c.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
△ Collections
1) 排序Collections.sort()
a. 自然排序(natural ordering);
b. 实现比较器(Comparator)接口.
2) 取最大最element: Collections.max(); Collections.min();
3) 在已经排序的List中搜索指定的element: Collections.binarySerach()。
△ 一般方法实现
import java.util.*;
class ArrayListTest
{
public static void printElements(Collection c){
Iterator it = c.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public static void main(String[] args){
Student s1 = new Student("antsoul",25);
Student s2 = new Student("feiyang",35);
Student s3 = new Student("gll",24);
Student s4 = new Student("andylau",40);
ArrayList al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
al.add(s4);
Collections.sort(al);
printElements(al);
}
}
class Point
{
int x,y;
public Point(int x,int y){
this.x = x;
this.y = y;
}
public String toString(){
return ("x="+x+","+"y="+y);
}
}
class Student implements Comparable
{
private String name;
private int num;
public Student(String name,int num){
this.name = name;
this.num = num;
}
public int compareTo(Object o){
Student s =(Student)o;
return num > s.num ? 1 : (num == s.num ? 0 : -1);
}
public String toString(){
return "num:"+num+" "+"name:"+name;
}
}
△ 比较器总是和特定的类相关的,具体到某一个类。比如说对student排序,你要用到学号,所以排序前必须要转换Object为Student,也就是为某一个类指定一个比较器,可以写一个类去实现比较器的接口,但是为了联系紧密,可以在这里用内部类在实现比机器接口。
import java.util.*;
class ArrayListTest
{
public static void printElements(Collection c){
Iterator it = c.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public static void main(String[] args){
Student s1 = new Student("antsoul",2);
Student s2 = new Student("feiyang",1);
Student s3 = new Student("gll",3);
Student s4 = new Student("andylau",4);
ArrayList al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
al.add(s4);
Collections.sort(al,new Student.StudentComparator()); //student提供自己的比较器
printElements(al);
}
}
class Point
{
int x,y;
public Point(int x,int y){
this.x = x;
this.y = y;
}
public String toString(){
return ("x="+x+","+"y="+y);
}
}
class Student implements Comparable
{
private String name;
private int num;
//为了调用方便声明为static
static class StudentComparator implements Comparator
{
public int compare(Object o1,Object o2){
Student s1 =(Student)o1;
Student s2 =(Student)o2;
return s1.num > s2.num ? 1 :(s1.num==s2.num ? 0 : -1);
}
}
public Student(String name,int num){
this.name = name;
this.num = num;
}
public int compareTo(Object o){
Student s =(Student)o;
return num > s.num ? 1 : (num == s.num ? 0 : -1);
}
public String toString(){
return "num:"+num+" "+"name:"+name;
}
}
如果student的num相等的情况下,要以name来排序可以这样实现:
import java.util.*;
class ArrayListTest
{
public static void printElements(Collection c){
Iterator it = c.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public static void main(String[] args){
Student s1 = new Student("antsoul",2);
Student s2 = new Student("feiyang",1);
Student s3 = new Student("dorydoo",3);
Student s4 = new Student("sun",4);
Student s5 = new Student("gll",4);
ArrayList al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
al.add(s4);
al.add(s5);
Collections.sort(al,new Student.StudentComparator()); //student提供自己的比较器
printElements(al);
}
}
class Point
{
int x,y;
public Point(int x,int y){
this.x = x;
this.y = y;
}
public String toString(){
return ("x="+x+","+"y="+y);
}
}
class Student implements Comparable
{
private String name;
private int num;
//为了调用方便声明为static
static class StudentComparator implements Comparator
{
public int compare(Object o1,Object o2){
Student s1 =(Student)o1;
Student s2 =(Student)o2;
int result;
result = s1.num > s2.num ? 1 :(s1.num==s2.num ? 0 : -1);
if(result == 0){ //student的num相同,比较name,因为name为String类型,它实现了Comparable<String>
result = s1.name.compareTo(s2.name);
}
return result;
}
}
public Student(String name,int num){
this.name = name;
this.num = num;
}
public int compareTo(Object o){
Student s =(Student)o;
return num > s.num ? 1 : (num == s.num ? 0 : -1);
}
public String toString(){
return "num="+num+" "+"name="+name;
}
}