注意:
Vector:用ArrayList代替Vector。
Hashtable:用HashMap代替Hashtable。
Satck:用LinkedList代替Stack。
ArrayList底层采用数组完成,而LinkedList则是以一般的双向链表(double-linked list)完成,其内每个对象除了数据本身外,还有两个 引用,分别指向前一个元素和后一个元素。
如果我们经常在List的开始处增加元素,或者在List中进行插入和删除操作,我们应该使用LinkedList,否则的话,使用ArrayList将更加快速。
Collection最要用来对列表进行操作,array主要用来对数组进行操作!
HashSet是基于Hash算法实现的,其性能通常都优于TreeSet。我们通常都应该使用HashSet,在我们需要排序的功能时,我们才使用TreeSet。
列表排序:
1)Collections(位于java.util包中,它里面的方法都是静态的)的sort可以对容器的序列进行排序
其中sort有两重构造方式
(a)static <T extends Comparable<? super T>> void sort(List<T> list),列表中的所有元素都必须实现 Comparable 接口。Comparable 位于java的lang包中,它有一个int compareTo(T o)方法,根据返回值的大小,确定比较的大小。具体的情况写自己的compareTo()方法。此外
,列表中的所有元素都必须是可相互比较的(也就是说,对于列表中的任何 e1 和 e2 元素,e1.compareTo(e2) 不得抛出ClassCastException。此排序方法具有稳定性:不会因调用 sort 方法而对相等的元素进行重新排序。
(b)public static <T> void sort(List<T> list,Comparator<? super T> c),它除了需要一个list,还需要一个实现Comparator接口的进行比较的类。
方法一的实现如下:
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(2,"zhangsan");
Student s2=new Student(1,"lisi");
Student s3=new Student(3,"wangwu");
Student s4=new Student(2,"mybole");
ArrayList al=new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
al.add(s4);
Collections.sort(a1);/*对ArrayList对象a1进行排序,它需要对容器中要比较的元素实现Comparable 接口,并重写接口中的compareTo(Object o)方法*/
//Collections.sort(al,new Student.StudentComparator()))
printElements(al);
}
}
class Student implements Comparable
{
int num;
String name;
/*static class StudentComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
Student s1=(Student)o1;
Student s2=(Student)o2;
int result=s1.num > s2.num ? 1 : (s1.num==s2.num ? 0 : -1);
if(result==0)
{
result=s1.name.compareTo(s2.name);
}
return result;
}
}*/
Student(int num,String name)
{
this.num=num;
this.name=name;
}
public int compareTo(Object o)
{
Student s=(Student)o;//类型转换
return num > s.num ? 1 : (num==s.num ? 0 : -1);
}
public String toString()
{
return 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(2,"zhangsan");
Student s2=new Student(1,"lisi");
Student s3=new Student(3,"wangwu");
Student s4=new Student(2,"mybole");
ArrayList al=new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
al.add(s4);
Collections.sort(al,new Student.StudentComparator()))/*除了第一个参数,还需要一个实现了Comparator 的类,并实现它的compare ()方法*/
printElements(al);
}
}
class Student
{
int num;
String name;
static class StudentComparator implements Comparator
{/*用内部类实现是因为它是专门实现student类的比较函数,这样可以显的跟紧凑些,声明为static则不需要产生外部类的对象就可以调用它*/
public int compare(Object o1,Object o2)
{
Student s1=(Student)o1;
Student s2=(Student)o2;
int result=s1.num > s2.num ? 1 : (s1.num==s2.num ? 0 : -1);
if(result==0)
{
result=s1.name.compareTo(s2.name);
}
return result;
}
}
Student(int num,String name)
{
this.num=num;
this.name=name;
}
public String toString()
{
return num+":"+name;
}
}
TreeSet
排序:
实现Set接口的hashSet (哈希表),依靠HashMap来实现的。
TreeSet是依靠TreeMap来实现的。
TreeSet是一个有序集合,TreeSet中元素将按照升序排列,缺省是按照自然顺序进行排列,意味着TreeSet中元素要实现Comparable接口。//方法一
我们可以在构造TreeSet对象时,传递实现了Comparator接口的比较器对象。//方法二
我们应该为要存放到散列表的各个对象定义hashCode()和equals()。可以参考下面的例子
方法一上面的第一个例子相同,所以这里略去,法二如下:
import java.util.*;
class TreeSetTest
{
public static void main(String[] args)
{
TreeSet ts=new TreeSet(new Student.StudentComparator());
ts.add(new Student(2,"lisi"));
ts.add(new Student(1,"wangwu"));
ts.add(new Student(3,"zhangsan"));
ts.add(new Student(3,"mybole"));
Iterator it=ts.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class Student
{
int num;
String name;
static class StudentComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
Student s1=(Student)o1;
Student s2=(Student)o2;
int result=s1.num > s2.num ? 1 : (s1.num==s2.num ? 0 : -1);
if(result==0)
{
result=s1.name.compareTo(s2.name);
}
return result;
}
}
Student(int num,String name)
{
this.num=num;
this.name=name;
}
public String toString()
{
return num+":"+name;
}
}
对于Map<K,V>,它有三个重要的方法:
Set<K> keySet(),返回此映射中包含的键的 Set
视图。由于Set没有实现从中取元素的方法,所以只能通过迭代器Iterator从中迭代元素。
Collection<V> values(),返回此映射中包含的值的 Collection
视图。
Set<Map.Entry<K,V>> entrySet(),返回此映射中包含的映射关系的 Set
视图。
import java.util.*;
class HashMapTest
{
public static void printElements(Collection c)
{
Iterator it=c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String[] args)
{
HashMap hm=new HashMap();
hm.put("one","zhangsan");
hm.put("two","lisi");
hm.put("three","wangwu");
System.out.println(hm.get("one"));
System.out.println(hm.get("two"));
System.out.println(hm.get("three"));
Set keys=hm.keySet();
System.out.println("Key:");
printElements(keys);
Collection values=hm.values();
System.out.println("Value:");
printElements(values);
Set entry=hm.entrySet();
//printElements(entry);
Iterator it=entry.iterator();
while(it.hasNext())
{
Map.Entry me=(Map.Entry)it.next();
System.out.println(me.getKey()+":"+me.getValue());
}
}
}