Java集合类
接口 | 说明 | 实体类 |
Set | Set扩展了Collection接口,用来提供集合的维护所需功能(无序) 不可重复。 | HashSet LinkedHashSet |
SortedSet | SortedSet扩展了Set接口,用来提供集合的维护所需功能(有序) | TreeSet |
List | List扩展了Collection接口,用来存放某个元素序列(可无序) | ArrayList Vector LinkedList |
Map | 一种基本接口,用来实现键值对(key-value)映射关系维护的操作 | HashMap HashTable LinkedHashMap |
SortedMap | 有序存放的扩展Map接口 | TreeMap |
实体类 | 重复 | 有序 | 数据结构 |
HashSet | 元素唯一 | 无序(随机存放) | Hash表 |
LinkedHashSet | 元素唯一 | 插入顺序 | Hash表、双向链表 |
TreeSet | 元素唯一 | 有序 | 平衡树 |
ArrayList | 可重复 | 插入顺序 | 变长数组 |
Vector | 可重复 | 插入顺序 | 变长数组 |
LinkedList | 可重复 | 插入顺序 | 链表 |
HashMap | 键唯一 | 无序(随机存放) | Hash表 |
HashTable | 键唯一 | 无序(随机存放) | Hash表 |
LinkedHashMap | 键唯一 | 键插入顺序 | Hash表、双向链表 |
TreeMap | 键唯一 | 键有序 | 平衡树 |
Collection接口常见方法:
- 元素添加和删除
- boolean add(Object o);
- boolean remove(Object o);
- 查询操作:
- int size();
- boolean isEmpty();
- boolean contains(Object o);
- Iterator iterator();返回一个迭代器,用来访问集合中的各个元素
3、组操作:作用于整个集合或元素组
- boolean containsAll(Collection c);///查找集合中是否含有集合c中的所有元素
- boolean addAll(Collection c);///将集合c中的所有元素添加给该集合
- Void clear();///删除集合中所有元素
- Void removeAll(Collection c);///从集合中删除c中的所有元素
- Void retainAll(Collection c);//从集合中删除c中不包含的元素
4、Collection转换为Object数组;
- Object[] toArray();//返回一个内含集合所有元素的数组
- Object[] toArray(Object[] a);//返回一个内含集合所有元素的数组,返回数组合参数a的类型相同。
*** Collection不提供get方法,如果要遍历所有元素,就必须使用Iterator
//转为数组,来遍历元素 Object[] iarr = new Object[tt.size()]; iarr= tt.toArray(); for(int i=0;i<iarr.length;i++) { System.out.println(iarr[i]); }输出1 2 3 4 | //使用迭代器遍历元素 Iterator<Integer> it; it=tt.iterator(); while(it.hasNext()) { System.out.println(it.next()); }输出1 2 3 4 | 迭代器: 1:hasNext()判断是否存在另一个可访问元素 2:next()返回要访问的下一个元素。如果达到结尾,就抛出NoSuchElementException异常 3:void remove()删除上次访问的对象;需在访问后执行,如果访问后集合已被修改,方法抛出IllegalStateException异常 |
各种实现类举例:
void testArrayList() { ArrayList a=new ArrayList(); a.add(1);a.add(7);a.add(3); Iterator it=a.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } 运行结果:1 7 3 ArrayList是非同步的,也不是线性安全的;ArrayList是按照50%扩展容量的,会比vector更节省空间; 适用于要求速度的环境。 | void testVector() { Vector v=new Vector(); v.add(1);v.add(7);v.add(3); Iterator it=a.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } 运行结果:1 7 3 Vector是同步的、线程安全的;; 适用于多线程环境; | void testLinkedList() { LinkedList l=new LinkedList(); l.add(1);l.add(7);l.add(3); l.addFirst(9);l.addLast(11); Iterator it=l.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } 运行结果:9 1 7 3 11 LinkedList实现了List接口,允许null元素,它还提供get()/remove()/insert()等方法。 所以它可以被用作stack/queue/deque |
void testHashMap() { HashMap hm=new HashMap<>(); hm.put(1, null);hm.put(5, "55"); hm.put(3, "33");hm.put(4, "44"); Collection c=hm.values(); Iterator it= c.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } 运行结果:null 33 44 55 HashMap和HashTable都是散列表,就算是排好序,也会被打乱;;可放入null值;;输出会根据key的顺序来 get(key) containsKey(key) Set <Integer> set= hm.keySet(); Set<Map<Integer,String>> sm=hm.entrySet(); | void testHashTable() { Hashtable ht=new Hashtable<>(); ht.put(2, "22"); ht.put(5, "55"); ht.put(3, "33"); ht.put(4, "44"); Collection c=ht.values(); Iterator it= c.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } 运行结果:55 44 33 22 Hashtable是散列表,不可放null值;;输出会根据key的逆序(不是value)
| void testTreeMap() { TreeMap tm=new TreeMap<>(); tm.put(2, "22");tm.put(5, "55"); tm.put(3, "33");tm.put(4, "44"); tm.put(1, "11"); Collection c=tm.values(); Iterator it= c.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } 运行结果:11 22 33 44 55 TreeMap,不可放null值;;会根据key数自动排序(不是value) |