0.Java集合框架图数组
1.集合类(Collection)数据结构
(1)集合是存储对象最经常使用的一种方式。框架
(2)集合的特色:
①存储对象
②集合长度可变
③集合不能够存储基本数据类型ide
集合容器由于内部数据结构的不一样,有多种具体容器,不断的向上抽取,就造成了集合框架。。函数
(3)框架的顶层Collection接口this
Collection的常见方法:spa
①添加
boolean add(E e) code
boolean addAll(Collection c)
②删除
boolean remove(Object o)
boolean removeAll(Collection c)
void clear()
③判断
boolean contains(Object o)
boolean containsAll(Collection c)
boolean isEmpty() 对象
④获取
size() 返回此 collection 中的元素数
Iterator iterator() 取出元素的方式,迭代器
⑤该对象必须依赖于具体容器,由于每个容器的数据结构都不一样,因此该迭代器对象是在容器内部进行实现的。
对于容器使用者而言,具体的实现方式不重要,只要经过容器获取到该实现的迭代器对象便可,也就是iterator方法。
Iterator接口就是对全部的Collection容器进行元素取出的公共接口。blog
(4)其它
boolean retainAll(Collection c) 取交集
toArray() 将集合转成数组
(5)分支
--List:有序(存入和取出的顺序一致),元素都有索引(角标),元素能够重复。
--Set:元素不能重复,无序。
2.List
(1)List常见特有方法:
①添加
void add(index,element);
void add(index,collection);
②删除
Object remove(index);
③修改
Object set(index,element);
④获取
Object get(int index);
int indexOf(Object);
int lastIndexOf(Object);
List subList(from,to);
(2)ListIterator接口
在迭代过程当中,不要使用集合操做元素,容易出现异常。
可使用Iterator接口的子接口ListIterator来完成迭代中对元素进行更多操做。
(3)List常见
--Vector:内部是数组数据结构,是同步的。
--ArrayList:内部是数组数据结构,不一样步,替代Vector。
--LinkList:内部是链表数据结构,是不一样步的。
addFirst();
addLast():
jdk1.6
offerFirst();
offetLast();
getFirst();.//获取但不移除,若是链表为空,抛出NoSuchElementException.
getLast();
jdk1.6
peekFirst();//获取但不移除,若是链表为空,返回null.
peekLast():
removeFirst();//获取并移除,若是链表为空,抛出NoSuchElementException.
removeLast();
jdk1.6
pollFirst();//获取并移除,若是链表为空,返回null.
pollLast();
3.Set
Set接口中的方法和Collection一致
(1)HashSet:内部数据结构是哈希表,是不一样步的。
如何保证该集合的元素的惟一性呢?
是经过对象的hashCode和equals方法来完成对象惟一性的。
若是对象的hashCode值不一样,那么不判断equals方法,就直接存储到哈希表中。
若是对象的hashCode值相同,那么要再次判断对象的equals方法。
记住:
若是元素要存储到HashSet集合中,必须覆盖hashCode方法和equals方法。
通常状况下,若是定义的类会产生不少对象,好比人,学生,书,一般都须要覆盖
equals,hashCode方法。创建对象判断是否相同的依据。
public class Person extends Object { private int age; private String name; Person(String name,int age){ this.age=age; this.name=name; } public boolean equals(Object e) { if(this==e) return true; if(!(e instanceof Person)) return false; Person p=(Person)e; return (this.age==p.age)&&(this.name.equals(p.name)); } public int hashCode() { return name.hashCode()+age*39; } }
(2)TreeSet:能够对Set集合中的元素进行排序。是不一样步的。
判断元素惟一性的方式,就是根据compareTo()比较方法返回结果是否为0。
①TreeSet对元素进行排序的方式1:
让元素自身具有比较功能,就须要实现Comparable接口,覆盖compareTo方法。
public class Person extends Object implements Comparable{ private int age; private String name; Person(String name,int age){ this.age=age; this.name=name; } @Override public int compareTo(Object o) { // TODO Auto-generated method stub Person p=(Person)o; int temp=this.age-p.age; return temp==0?this.name.compareTo(p.name):temp; } }
②TreeSet对元素进行排序的方式2:
让集合自身具有比较功能,定义一个类实现Comparator接口,覆盖compare方法。
将该类对象做为参数传递给TreeSet集合的构造函数。
public class ComparatorByName implements Comparator { @Override public int compare(Object o1, Object o2) { // TODO Auto-generated method stub String p1=(String)o1; String p2=(String)o2; int temp=p1.length()-p2.length(); return temp==0?p1.compareTo(p2):temp; } }
public class TreeSetTest{ public static void main(String[] args) { // TODO Auto-generated method stub TreeSet ts=new TreeSet(new ComparatorByName()); ts.add("aaa"); ts.add("ab"); ts.add("abcd"); ts.add("aaabbbb"); Iterator it=ts.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
4.集合的一些技巧
(1)须要惟一吗?
须要:Set
须要制定顺序:
须要: TreeSet
不须要:HashSet
可是想要一个和存储一致的顺序(有序):LinkedHashSet
不须要:List
须要频繁增删吗?
须要:LinkedList
不须要:ArrayList
(2)如何记录每个容器的结构和所属体系呢?
看名字!
List
|--ArrayList
|--LinkedList
Set
|--HashSet
|--TreeSet
后缀名就是该集合所属的体系。
前缀名就是该集合的数据结构。
看到array:就要想到数组,就要想到查询快,有角标.
看到link:就要想到链表,就要想到增删快,就要想要 add get remove+frist last的方法
看到hash:就要想到哈希表,就要想到惟一性,就要想到元素须要覆盖hashcode方法和equals方法。
看到tree:就要想到二叉树,就要想要排序,就要想到两个接口Comparable,Comparator 。
并且一般这些经常使用的集合容器都是不一样步的。