Java集合类大致分为List,Set和Map
List和Set都实现了继承自Collection接口,Map没有继承自这个接口。
List下面主要有LinkedList<E>和ArrayList,它们是有顺序的,使用它们提供的方法操作即可,遍历可以使用遍历器Iterator,方法如下:
Iterator<E> itr = mylist.iterator();
while(itr.hasNext())
{
itr.next()...
}
Set下面有HashSet和TreeSet。
HashSet<E>是一个集合,不允许有重复的元素,也是无序的,继承自Collection接口,根据提供的方法进行操作,因为它是无序的,所以得使用遍历器Iterator来遍历。遍历方法和上面的List一样。
Iterator<E> itr = mylist.iterator();
while(itr.hasNext())
{
itr.next()...
}
TreeSet<E>是实现Set接口的类,它可以给Set集合中的元素进行指定方式的排序,通过实现Comparable接口。底层数据结构是:二叉树。
package cn.swpu;
import java.lang.ref.SoftReference;
import java.util.Iterator;
import java.util.TreeSet;
class Person
{
static
{
System.out.println("父类静态块");
}
public Person(int i, String kk)
{
System.out.println("父类构造函数");
}
}
class Student extends Person implements Comparable
{
int English = 0;
String name ;
public Student(int e,String n)
{
super(e,n);
English = e;
name = n;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
Student st = (Student)o;
return (this.English-st.English);
}
}
public class Example {
public static void main(String[] args) {
TreeSet<Student> mytree = new TreeSet<Student>();
SoftReference<Student>[] stus = new SoftReference[3];
stus[0] = new SoftReference<Student>(new Student(90,"zhan ying"));
stus[1] = new SoftReference<Student>(new Student(66,"wang hen"));
stus[2] = new SoftReference<Student>(new Student(89,"Li qi"));
for(int i = 0; i < stus.length; i++)
{
mytree.add(stus[i].get());
}
Iterator<Student> ite = mytree.iterator();
while(ite.hasNext())
{
Student stu = ite.next();
System.out.println(""+stu.name+":"+stu.English);
stu = null;
}
}
}
在将对象添加到TreeSet中的时候,会调用compareTo方法比较插入的位置,输出的时候调用iterator,会有序输出。
Map下面同样有HashMap和TreeMap,它们没有实现Collection接口,在遍历数据的时候使用:
Iterator<Student> iter = treemap.values().iterator();
TreeMap<K,V>和TreeSet一样是可以定义key的顺序,可排序,实现Comparable接口。
而HashMap<K,V>则不能进行排序。HashMap使用时要保证Key不能相同,否则后面的相同Key的Value会覆盖前面的Value。
TreeMap的一段代码:
package cn.swpu;
import java.util.Iterator;
import java.util.TreeMap;
class MyKey implements Comparable
{
int number = 0;
MyKey(int number)
{
this.number = number;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
MyKey st = (MyKey)o;
if((this.number - st.number) == 0)
{
return -1;
}
else
return this.number - st.number;
}
}
class Student
{
String name = null;
int height,weight;
public Student(int w,int h,String name)
{
weight = w;
height = h;
this.name = name;
// TODO Auto-generated constructor stub
}
}
public class Example_7_14 {
public static void main(String[] args)
{
Student st1 = new Student(65,177,"张三"),
st2 = new Student(85, 168, "李四"),
st3 = new Student(76,170,"王五");
TreeMap<MyKey,Student> treemap = new TreeMap<MyKey,Student>();
treemap.put(new MyKey(st1.weight), st1);
treemap.put(new MyKey(st2.weight), st2);
treemap.put(new MyKey(st3.weight), st3);
int num = treemap.size();
System.out.println("树映射中有"+num+"个对象");
Iterator<Student> iter = treemap.values().iterator();
while(iter.hasNext())
{
Student stu = iter.next();
System.out.printf("%s,%d\n",stu.name,stu.weight);
stu = null;
}
treemap.clear();
treemap.put(new MyKey(st1.weight), st1);
treemap.put(new MyKey(st2.weight), st2);
treemap.put(new MyKey(st3.weight), st3);
int number = treemap.size();
System.out.println("树映射中有"+number+"个对象");
Iterator<Student> iterator = treemap.values().iterator();
while(iterator.hasNext())
{
Student stu = iterator.next();
System.out.printf("%s,%d\n",stu.name,stu.height);
stu = null;
}
}
}
输出结果:
树映射中有3个对象
张三,65
王五,76
李四,85
树映射中有3个对象
张三,177
王五,170
李四,168
compareTo()在TreeMap中和TreeSet中的作用一样,在将对象放到集合中时,会循环判断插入的位置,从第一个开始比较,一直到最后一个。