1. 类集框架主要接口
在整个Java类集中最常使用的类集接口是:
- Collection:是存放一组单值的最大父接口,所谓的单值是指集合中的每个元素都是一个对象。
- List:是Collection接口的子接口,也是最常用的接口。
- Set:是Collection接口的子类,没有对Collection接口进行扩展,不允许存放重复内容。
- Map:是存放一对值的最大父接口,即接口中的每个元素都是一对,以key->value的形式保存
- Iterator:集合的输出接口
- ListIterator:是Iterator的子接口,可以进行由前向后或由后向前的双向输出
- Enumeration:是最早的输出接口,用于输出指定集合中的内容
- SortedSet:单值的排序接口,实现此接口的集合类,里面的内容可以使用比较器排序
- SortedMap:存放一对值的排序接口,实现此接口的集合类,里面的内容按照key排序,使用比较器排序
- Queue:队列接口,此接口的子类可以实现队列操作
- Map.Entry:内部接口,每个Map.Entry对象都保存着一对key->value的内容,每个Map接口中都保存有多个Map.Entry接口实例。
2. List接口
List是Collection的子接口。接口定义如下:
public interface List<E> extends Collection<E>
//下面是List接口的拓展方法
public void add(int index,E element)//指定位置增加元素
public boolean addAll(int index,Collection<?extends E>c)//在指定位置增加一组元素
E get(index) //返回指定位置的元素
public int indexOf(Object o)//查找指定元素的位置
public int lastIndex(Object o)//从后向前指定元素的位置
public ListIterator<E> listIterator()//为ListIterator接口实例化
public E remove(int index)//按指定的位置删除元素
public List<E> subList(int fromIndex,int toIndex)//取出集合中的子集合
public E set(int index,E element)//替换指定位置的元素
- ArrayList是List子类,可以直接通过对象的多态性为List接口实例化。此类的定义如下所示:
public class ArrayList<E> extends AbstractList<E>
implements List<E>,RandomAccess,Cloneable,Serializable
//ArrayList类继承了AbstractList类。定义如下
public abstract class AbstractList<E>
extends AbstractCollection<E> implements List<E>
//常用方法
List<String> allList=null;
allList=new ArrayList<String>();
allList.add("Hello");//增加
allList.remove("Hello");//删除
allList.get(i);//获取
String str[]=allList.toArray(new String[]{});//指定泛型
- LinkedList表示的是一个链表的操作类,即Java中已经为开发者提供好了一个链表程序,直接调用即可:
public class LinkedList<E> extends AbstractSequentiaList<E>
implements List<E>,Queue<E>,Cloneable,Serializable
//此类实现了List接口,同时实现了Queue接口。
//Queue接口是Collection的子接口
public interface Queue<E> extends Collection<E>
//Queue常用方法
public E element() //找到链表的表头
public boolean offer(E o)//将指定元素增加到链表的结尾
public E peek()//找到但并不删除链表的头
public E poll()//找到并删除此链表的头
public E remove()//检索并移除表头
//LinkedList常用方法
public void addFirst(E o)//在链表开头增加元素
public void addLast(E o)//在链表结尾增加元素
public boolean offer(E o)//将指定元素增加到链表的结尾
public E removeFirst()//删除链表的第一个元素
public E removeLast()//删除链表的最后一个元素
3. Set接口
Set接口不能加入重复的元素。定义如下:
public interface Set<E> extends Collection<E>
散列存放:HashSet
主要特点:里面不能存放重复元素,而且采用散列存储方式,所以没有顺序有序的存放:TreeSet
public class TreeSet<E> extends AbstractSet<E>
implements SortedSet<E>,Cloneable,Serializable
//用法
Set<String> allSet=new TreeSet<String>();
要对类进行排序,必须实现Compaable继承。
public int compareTo(Person per)
如何实现类去掉重复,具体需要实现equals函数和hashCode函数的覆写。
class Person{
private String name;
private int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
public boolean equals(Object obj){
if(this==obj){ //地址相同,同一个对象
return true;
}
if(!(obj instanceof Person)){//传递不是本类对象
return false;
}
Person p=(Person)obj;//进行向下转型
if(this.name.equals(p.name)&&this.age==p.age){
return true;
}else{
return false;
}
}
public int hashCode(){
return this.name.hashCode()*this.age;
}
public String toString(){
return "姓名:"+this.name+":年龄:"+this.age;
}
}
4、集合的输出
- 迭代输出:Iterator。用法如下:
//遍历
Iterator<String> iter=all.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
//删除元素
while(iter.hasNext()){
String str=iter.next();
if("_".equals(str)){
iter.remove();//删除
}
}
- 双向迭代输出:ListIterator
public boolean hasNext()
public E next()
public void remove()
public void add(E o)
public boolean hasPrevious()//判断是否有前一个元素
public E previous()//取出当前元素
public int nextIndex()//返回下一个元素的索引号
public int previousIndex()//返回上一个元素的索引号
public void set(E o)//替换元素
5. Map接口
public interface Map<K,V>
常用子类:
- HashMap:无序存放,key不允许重复
- TreeMap:可以排序的Map集合,按结合中的key排序,key不允许重复(对于键值为类,需要覆写equals和hashCode方法)
- IdentityHashMap:key可以重复的Map集合。
常用操作如下所示:
public boolean containsKey(Object key)
public boolean containsValue(Object value)
public V put(K key,V value)//添加键值
public V get(Object key)//获取
public V remove(Object key)//删除
public Set<K> keySet()//获取所有键值
Map<Integer,Integer> map=null;
map= new HashMap<Integer,Integer>();
//遍历用法
Set<String> keys=map.keySet();
Iterator<String> iter=keys.iterator()
while(iter.hasNext()){
String str=iter.next();
System.out.println(str+"->"+map.get(str));
}
Map map = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
}
6. 集合工具类:Collections
相当于C++的算法类,实现一些算法的操作。
- 返回不可变的集合
List<String> allList=Collections.emptyList();//不可添加元素的空集合
- 为集合增加内容
List<String> all=new ArrayList<String>();
Collections.add(all,"1","2");
- 反转集合中的内容
Collections.reverse(all);
- 检索内容
int point=Collections.binarySearch(all,"1");
- 替换集合内容
Collections.replaceAll(all,"原始内容","替换内容");
- 集合排序
Collections.sort(all);
- 交换指定位置
Collections.swap(all,0,2);
7. 其他集合类
Stack类,栈,先进后出,常见操作如下:
public boolean empty() //测试栈是否为空
public E peek() //查看栈顶,但不删除
public E pop() //出栈,同时删除
public E push(E item) //入栈
public int search(Object o) //在栈中查找
Stack<String> s=new Stack<String>();
s.push("A");
s.pop();