一、集合框架总体图
对上图的简要说明:
- 该图是用idea生成的java.util包中关于集合框架的类图,建议大家方大点看。
- Java的集合类主要由两个接口派生而出:Collection和Map,Collection和Map是Java集合框架的根接口,这两个接口又包含了一些子接口或实现类。
- Collection接口下主要有Set、List和Queue接口:List 接口继承 Collection,允许重复,维护元素插入顺序。List常用的实现类有LinkedList, ArrayList, Vector, Stack;Set接口表示集合类型,不允许重复,Set接口常用实现类有Hashset和TreeSet,Queue接口表示队列。常用实现类有LinkedList、PriorityQueue等。
- Map是一个映射接口,即key-value键值对。Map中的每一个元素包含“一个key”和“key对应的value”。AbstractMap是个抽象类,它实现了Map接口中的大部分API。而HashMap,TreeMap,WeakHashMap等都是继承于AbstractMap。
二、常用接口和类的使用方法
1.Stack类:jdk实现了栈的数据结构以及常用的方法,同时实现了Iterator接口。peek方法返回栈顶元素,push和pop方法实现入栈和出栈,search方法实现了查找元素位置(栈顶为1),empty方法实现是否为空的判断。
Stack<String> ssk=new Stack<String>();
ssk.push("item1");
ssk.push("item2");
System.out.println(ssk.search("item1"));
System.out.println(ssk.peek());
System.out.println(ssk.pop());
System.out.println(ssk.empty());
ssk.push("lasd");
Iterator<String> it= ssk.iterator();
while(it.hasNext())
{
System.out.print(it.next()+" ");
}
2.List接口:List接口继承自Collection,用于定义以列表形式存储的集合,List接口为集合中的每个对象分配了一个索引(index),标记该对象在List中的位置,并可以通过index定位到指定位置的对象。
- get(int) - 返回指定index位置上的对象
- add(E)/add(int, E) - 在List末尾/指定index位置上插入一个对象
- set(int, E) - 替换置于List指定index位置上的对象
- indexOf(Object) - 返回指定对象在List中的index位置
- subList(int,int) - 返回指定起始index到终止index的子List对象
List<String> list=new LinkedList<>();
List<Object> list1=new ArrayList<>();
list.add("item");
list.size();
list1.size();
list.isEmpty();
Iterator<String> it=list.iterator();
Iterator<Object> it1=list1.iterator();
3. Set接口:Set接口继承自Collection接口,不允许其中有两个相同的元素。不能直接实例化,TreeSet(间接实现了SortedSet接口,会把其中的元素自动排序)和HashSet是其的两个实现类。
Set<String> treeset=new TreeSet<String>();
treeset.add("d");
treeset.add("a");
treeset.add("c");
treeset.add("b");
Iterator<String> it1=treeset.iterator();
while (it1.hasNext())
{
System.out.print(it1.next());
}
Set<String> hashset=new HashSet<String>();
hashset.add("g");
hashset.add("a");
hashset.add("c");
hashset.add("b");
Iterator<String> it2=hashset.iterator();
while (it2.hasNext())
{
System.out.print(it2.next());
}
4.Map接口: Map接口定义了Map的Key-Value结构和一些基本的方法,用法如下
Map<String,Object> hashmap=new HashMap<>();
Map<String,String> hashtable=new Hashtable<>();
Map<String,Integer> concurrenthashmap=new ConcurrentHashMap<>();
5.Queue接口:该接口继承了Collection接口,与List和Set处于同一级别。PriorityQueue类(优先队列:存储结构为堆架构,使用poll弹出时依弹出排序后的元素),inkedBlockingQueue和LinkedBlockingDeque,两个都是并发堵塞队列队列,只不过前者只能一端出一端入,后者则可以两端同时出入。
Queue<String> queue1=new ArrayDeque<>();
Queue<String> queue2=new LinkedList<>();
Queue<String> queue3=new PriorityQueue<>();
Queue<String> queue4=new LinkedBlockingDeque<>();
Queue<String> queue5=new LinkedBlockingQueue<>();
Queue<Integer> q=new PriorityQueue<>();
q.add(10);
q.add(3);
q.add(9);
while(!q.isEmpty())
{
System.out.print(q.poll()+" ");
}
三、常见注意事项
1.HashMap、Hashtable和ConcurrentHashMap
Hashmap:线程不安全,所以性能高。允许key和value设置为null;Hashtable:线程安全,使用一把锁处理并发问题,当多个线程访问时,需要多个线程竞争一把锁,容易造成堵塞。key或value都不允许为null;concurrentHashMap:线程安全的,在多线程下效率更高。实现原理相当于把一个HashMap分成多个Hashtable来处理。
2.Array和ArrayList
Array和Arraylist都是数组,区别在于Array大小需要提前给定(类似于C语言中的数组),可以包含基本数据类型和对象类型,适用于长度已知的情况下。而Arraylist是长度可变的数组,只能包含对象类型,可以自动扩容。
3.ArrayList 和 LinkedList
ArrayList 和 LinkedList 都实现了 List 接口,他们有以下的不同点:ArrayList底层是数组,对其中任意一个元素进行访问的时间复杂度都为O(1),但是删除和插入元素的效率相对较低。而LinkedList底层是链表,查找某个 元素的时间复杂度是 O(n),相对于 ArrayList,LinkedList 的插入,添加,删除操作速度更快,因为当元素 被添加到集合任意位置的时候,不需要像数组那样重新计算大小或者是更新索 引,但占用的内存相较ArrayList更大。