集合和数组的区别?
数组的固定长度的,一个数组只能存储相同类型的基本数据类型或引用数据类型的数据。
集合石可变长度的,只能存放引用数据类型的数据
Collection集合接口,List,Set是其子接口。
|--List:它对元素的存取是有顺序的。可以存入重复元素,因为该集合中的元素都有角标。
|--ArrayList:底层是数组数据结构。不同步的。对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。
|--Vector:底层是数组数据结构。同步的。效率非常低。支持枚举。但枚举已被迭代器所取代。
|--LinkedList:底层是链表数据结构的。对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据。
(如果集合中的元素的数目大于目前集合数组的长度时,vector增长率为目前数组长度的100%,而arraylist增长率为目前数组长度的50%,ArrayList比较节省内存空间。)
ArrayList、LinkedList和Vector都可以使用自身的get()方法跟迭代器进行集合元素遍历,Vector还可使用枚举进行遍历,实现的功能跟迭代器是相同的,但迭代器接口中添加了一个可选的移除操作,并使用较短的方法名。新的实现应该优先考虑使用 Iterator 接口而不是 Enumeration 接口。
例:
import java.util.*;
public class TestList {
public static void main(String[] args) {
ArrayList al = new ArrayList();
LinkedList ll = new LinkedList();
Vector vc = new Vector();
al.add(new Person("xiaoming",20));
al.add(new Person("feifei",25));
ll.add(new Person("dongdong",30));
ll.add(new Person("jingjing",26));
vc.add(new Person("kaikai",28));
vc.add(new Person("xiaoxiao",21));
for(int i=0;i<al.size();i++){
System.out.println(al.get(i)); // 使用自带的get()方法
}
for(Iterator it = ll.iterator();it.hasNext();){ //使用迭代器
System.out.println(it.next());
}
for(Enumeration en = vc.elements();en.hasMoreElements();){ //使用枚举
System.out.println(en.nextElement());
}
}
}
class Person{
private String name;
private int age;
Person(String name,int age){
this.name = name;
this.age =age;
}
public String toString(){
return name+"..."+age;
}
}
|--Set:它对元素的存取是无序的。不可以存入重复元素。只能使用迭代器遍历set的集合元素。
|--HashSet:底层数据结构是哈希表。对于判断该集合元素是否重复,通过hashCode方法,equals方法方法来保证元素的唯一性。
先会判断两个对象hashCode值,只有哈希值相同的情况下,才会判断equals方法。是不同步的。
|--TreeSet:底层的数据结构是二叉树。可以对Set集合中的元素进行排序。通过return 0来保证元素的唯一性。如果往TreeSet集合中存入元素并未实现Comparable接口,或者实现了Comparable接口,但是其比较方式并不是我们所需要的这时怎么办呢?
比较方式一:
让元素自身具备比较性。元素实现Comparable接口,覆盖compareTo方法。
比较方式二:
让TreeSet集合自身具备比较性。自定义Comparator的子类对象,覆盖compare方法。将比较器对象作为参数传递给TreeSet集合构造函数。
例:
方式一:
import java.util.*;
public class TestTreeSet
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Student("zhangsan",30));
ts.add(new Student("lisi",34));
ts.add(new Student("wangwu",22));
ts.add(new Student("zhaoliu",32));
ts.add(new Student("zhangsan",30));
for(Iterator it = ts.iterator();it.hasNext();)
System.out.println(it.next());
}
}
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)
{
Student s = (Student)obj;
if(this.age>s.age)
return 1;
else if(this.age==s.age)
return this.name.compareTo(s.name);
return -1;
}
public String toString()
{
return name+":"+age;
}
}
方式二:
import java.util.*;
public class TestTreeSet
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet(new Comparator()
{
public int compare(Object obj1,Object obj2)
{
Student s1 = (Student)obj1;
Student s2 = (Student)obj2;
if(s1.getAge()>s2.getAge())
return 1;
else if(s1.getAge()==s2.getAge())
return s1.getName().compareTo(s2.getName());
return -1;
}
});
ts.add(new Student("zhangsan",30));
ts.add(new Student("lisi",34));
ts.add(new Student("wangwu",22));
ts.add(new Student("zhaoliu",32));
ts.add(new Student("zhangsan",30));
for(Iterator it = ts.iterator();it.hasNext();)
System.out.println(it.next());
}
}
class Student
{
private String name;
private int age;
Student(String name,int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name+":"+age;
}
}