1、描述:类集仅仅是提供了处理事情的一个更好的方法。尽管类集的增加改变了许多原始工具的结构,但却不会导致被抛弃。
2、集合框架图:
3、 ArrayList:能够自动增长容易的数组,其底层以对象数组的方式实现。
code:
import java.util.*;
public class Test
{
public static void printElements(Collection c)
{
Iterator it=c.iterator();
//remove方法是一个可选择的方法,移去上次返回的对象
//大部分集合框架类,都实现了接口的这个方法
//it.next();
//it.remove();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String args[])
{
ArrayList al=new ArrayList();
//al.add("太阳");
//al.add("星星");
//al.add("月亮");
//添加对象元素
al.add(new Student("张三",21));
al.add(new Student("李四",22));
al.add(new Student("王五",25));
//get()方法获取元素
for(int i=0;i<al.size();i++)
{
System.out.println(al.get(i));
}
//直接打印
System.out.println(al);
//ArrayList的toArray()方法获得的数组
Object[] objs=al.toArray();
for(int i=0;i<objs.length;i++)
{
System.out.println(objs[i]);
}
//Arrays.asList()返回一个固定列表
List l=Arrays.asList(objs);
System.out.println(l);
//Iterator:通用的访问数据的方法
printElements(al);
}
}
class Student
{
private String name;
private int age;
Student(String name,int age)
{
this.name=name;
this.age=age;
}
public String toString()
{
return "name"+name+"age:"+age;
}
}
4、Collections类:
import java.util.*;
public class Test
{
public static void printElements(Collection c)
{
Iterator it=c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String args[])
{
ArrayList al=new ArrayList();
al.add(new Student(2,"zhangsan"));
al.add(new Student(1,"lisi"));
al.add(new Student(3,"wangwu"));
al.add(new Student(3,"laoqi"));
//调用Collections的方法进行排序,被排序对象要实现Comparable接口compareTo方法
//Collections.sort(al);
//指定比较器——实现Comparator接口的compare()方法
Collections.sort(al,new Student.StudentComparator());
//反序排列
//Collections.sort(al,Collections.reverseOrder());
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:"+num+"\tname:"+name;
}
}
4、LinkedList:采用双向链表实现的,可实现栈、队列、双向队列
import java.util.*;
public class Test
{
public static void main(String[] args)
{
MyStack ms=new MyStack();
ms.push("one");
ms.push("two");
ms.push("three");
while(!ms.empty())
{
System.out.println(ms.pop());
}
MyQueue mq=new MyQueue();
mq.put("one");
mq.put("two");
mq.put("three");
while(!mq.empty())
{
System.out.println(mq.get());
}
}
}
class MyStack
{
private LinkedList ll=new LinkedList();
public void push(Object o)
{
ll.addFirst(o);
}
public Object pop()
{
return ll.removeFirst();
}
public Object peek()
{
return ll.getFirst();
}
public boolean empty()
{
return ll.isEmpty();
}
}
class MyQueue
{
private LinkedList ll=new LinkedList();
public void put(Object o)
{
ll.addLast(o);
}
public Object get()
{
return ll.removeFirst();
}
public boolean empty()
{
return ll.isEmpty();
}
}
注:ArrayList底层采用数组完成,Linkedlist以一般的双向链表完成,除了数据本身,还有两个引用。如果要经常在列表的开始处增加元素,或在列表中大量地增删操作,应采用LinkedList,否则使用ArrayList更快。
5、HashSet类
import java.util.*;
public class Test
{
public static void printElements(Collection c)
{
Iterator it=c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String args[])
{
HashSet hs=new HashSet();
hs.add(new Student(1,"zhangsan"));
hs.add(new Student(2,"lisi"));
hs.add(new Student(3,"wangwu"));
hs.add(new Student(1,"zhangsan"));
printElements(hs);
}
}
class Student
{
int num;
String name;
Student(int num,String name)
{
this.num=num;
this.name=name;
}
public String toString()
{
return num+":"+name;
}
//Object类里的hashCode()哈希的键是地址
//要为存放到散列表的各个对象同时定义hasCode()和equals()
public int hashCode()
{
return num*name.hashCode();
}
public boolean equals(Object o)
{
Student s=(Student)o;
return num==s.num&&name.equals(s.name);
}
}
6、TreeSet类
import java.util.*;
public class Test
{
public static void printElements(Collection c)
{
Iterator it=c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String args[])
{
//指定比较器
TreeSet ts=new TreeSet(new Student.StudentComparator());
ts.add(new Student(1,"zhangsan"));
ts.add(new Student(2,"lisi"));
ts.add(new Student(3,"wangwu"));
ts.add(new Student(1,"zhangsan"));
ts.add(new Student(2,"ahu"));
printElements(ts);
}
}
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 String toString()
{
return num+":"+name;
}
public int compareTo(Object o)
{
Student s=(Student)o;
return num>s.num?1:(num==s.num?0:-1);
}
}
注:HashSet是能过哈希算法实现的,其性能通常优于TreeSet,只有当需要排序功能时,使用后者。
7、 HashMap类与TreeMap类
HashMap类的实例:
import java.util.*;
public class Test
{
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","lise");
hm.put("three","wangwu");
System.out.println(hm.get("one"));
System.out.println(hm.get("two"));
System.out.println(hm.get("three"));
//返回此映射中所包含的键的 set 视图
Set keys=hm.keySet();
printElements(keys);
System.out.println("Key:");
//返回此映射所包含的值的 collection 视图
Collection values=hm.values();
System.out.println("Value:");
printElements(values);
//返回此映射所包含的映射关系的 collection 视图
//在返回的集合中,每个元素都是一个 Map.Entry
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());
}
}
}
注:HashMap(对key散列)一般比TreeMap(据key排序)速度要快,只有需要排序功能时才需要后者。TreeMap与HashMap的实现类似,实例免。
8、 Properties类
import java.io.*;
import java.util.*;
public class Test
{
public static void main(String args[])
{
Properties pps1=System.getProperties();
pps1.list(System.out);
Properties pps=new Properties();
try
{
//w.ini中存储的是形如"copyright=20080804"的键值对
//读写配置信息
pps.load(new FileInputStream("w.ini"));
Enumeration enumm=pps.propertyNames();
while (enumm.hasMoreElements())
{
String strKey=(String)enumm.nextElement();
String strValue=pps.getProperty(strKey);
System.out.println(strKey+"="+strValue);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
注:同步之外,ArrayList代替Vectior,HashMap代替Hashtable,LinkedList代替Stack