Collection
|--List:元素是有序的,元素可以重复。因为该集合体系有索引。
|--ArrayList:底层的数据结构使用的是数组结构。特点:查询速度很快。但是增删稍慢。线程不同步。
|--LinkedList:底层使用的链表数据结构。特点:增删速度很快,查询稍慢。线程不同步。
|--Vector:底层是数组数据结构。线程同步。被ArrayList替代了。因为效率低。
List集合是用来存储对象的,其也只可存储对象,若不为对象。如:取出对象是时可用
|--Set:元素是无序,元素不可以重复。、
集合是用来存储对象的,其也只可存储对象,若不为对象。如:取出对象是时可用
List:
特有方法。凡是可以操作角标的方法都是该体系特有的方法。
增
add(index,element);
addAll(index,Collection);
删
remove(index);
改
set(index,element);
查
get(index):
subList(from,to);
listIterator();
int indexOf(obj):获取指定元素的位置。
ListIterator listIterator();
List集合特有的迭代器。ListIterator是Iterator的子接口。
在迭代时,不可以通过集合对象的方法操作集合中的元素。
因为会发生ConcurrentModificationException异常。
所以,在迭代器时,只能用迭代器的放过操作元素,可是Iterator方法是有限的,
只能对元素进行判断,取出,删除的操作,
如果想要其他的操作如添加,修改等,就需要使用其子接口,ListIterator。
该接口只能通过List集合的listIterator方法获取。
如:
public static void main(String[] args)
{
//演示列表迭代器。
ArrayList al = new ArrayList();
//添加元素
al.add("java01");
al.add("java02");
al.add("java03");
sop(al);
ListIterator li = al.Iterator();
while(li.hasNext())
{
Object obj = li.next();
if(obj.equals("java02"))
li.remove();
System.out.println(obj);
}
public static void main(String[] args)
{
//演示列表迭代器。
ArrayList al = new ArrayList();
//添加元素
al.add("java01");
al.add("java02");
al.add("java03");
ListIterator li = al.listIterator();
//sop("hasPrevious():"+li.hasPrevious());
while(li.hasNext())
{
Object obj = li.next();
if(obj.equals("java02"))
//li.add("java009");
li.set("java006");
}
list去除重复元素
public static ArrayList singleElement()
{
List al = new ArrayList();
al.add("java1");
al.add("java1");
al.add("java2");
al.add("java3");
//定义一个临时容器。
ArrayList newAl = new ArrayList();
Iterator it = al.iterator();
while(it.hasNext())
{
Object obj = it.next();
if(!newAl.contains(obj))
newAl.add(obj);
}
return newAl;
}</span>
Set:无序,不可以重复元素。 |--HashSet:数据结构是哈希表。线程是非同步的。 保证元素唯一性的原理:判断元素的hashCode值是否相同。 如果相同,还会继续判断元素的equals方法,是否为true。 |--TreeSet:可以对Set集合中的元素进行排序。 底层数据结构是二叉树。 保证元素唯一性的依据: compareTo方法return 0. TreeSet排序的第一种方式:让元素自身具备比较性。 元素需要实现Comparable接口,覆盖compareTo方法。 也种方式也成为元素的自然顺序,或者叫做默认顺序。 TreeSet的第二种排序方式。 当元素自身不具备比较性时,或者具备的比较性不是所需要的。 这时就需要让集合自身具备比较性。
在集合初始化时,就有了比较方式。
treeset元素第一种让元素自身具备比较性实现Comparable接口
compareTo(T t)
返回 负整数、零或正整数,根据此对象是小于、等于还是大于指定对象。
tree第一种排列方式
class Student implements Comparable//该接口强制让学生具备比较性。
{
private String name;
private int age;
Student(String name,int age)
{
this.name = name;
this.age = age;
}
public int compareTo(Object obj)
{
//return 0;
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student s = (Student)obj;
System.out.println(this.name+"....compareto....."+s.name);
if(this.age>s.age)
return 1;
if(this.age==s.age)
{
return this.name.compareTo(s.name);
}
return -1;
/**/
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}class TreeSetDemo
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Student("lisi02",22));
ts.add(new Student("lisi007",20));
ts.add(new Student("lisi09",19));
ts.add(new Student("lisi08",19));
//ts.add(new Student("lisi007",20));
//ts.add(new Student("lisi01",40));
Iterator it = ts.iterator();
while(it.hasNext())
{
Student stu = (Student)it.next();
System.out.println(stu.getName()+"..."+stu.getAge());
}
tree第二种排列方式
public static void main(String[] args)
{
TreeSet ts = new TreeSet(new StrLenComparator());
ts.add("abcd");
ts.add("cc");
ts.add("cba");
ts.add("aaa");
ts.add("z");
ts.add("cc");
Iterator it = ts.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class StrLenComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
String s1 = (String)o1;
String s2 = (String)o2;
int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
if(num==0)
return s1.compareTo(s2);
return num;
}
}
}