- Collection接口:单列集合,用来存储一个一个的对象
- List接口:储存“有序”“可重复”“动态”数据 --高中集合
- ArrayList:线程不安全,效率高,底层obj[]存储
- LinkedList:
- vector:线程安全,效率低
- Set接口:储存“无序”“不重复”
- HashSet: 线程不安全,可以存null
- LinkedHashSet:可以按添加的顺序遍历
- TreeSet:可按照添加对象的指定属性,进行排序
- List接口:储存“有序”“可重复”“动态”数据 --高中集合
- Map接口:双列集合,用来储存一对一对的数据 -- 高中函数
- HashMap: 主要实现类;线程不安全;效率高;储存null的key和value
- LinkedHashMap:保证在遍历map元素时,可以按照添加的顺序实现遍历: 原因:在原油的Hashmap底层结构基础上,添加了一对指针,效率高于Hashmap
- TreeMap:保证按照添加的key-value排序,实现排序遍历,底层使用红黑树。
- HashTable: 古老实现类;线程安全;效率低,不能储null的key和value
- Properties:常用来处理配置文件:key和value都是String类型
- HashMap: 主要实现类;线程不安全;效率高;储存null的key和value
面试题:
-
HashMap底层实现原理? 数组 + 链表 (jdk7before) 数组 + 链表 + 红黑树 (jdk8)
-
HashMap 和 HashTable异同?
-
CurrentHashMap 与 Hashtable 异同?
Colection
遍历集合中的元素
Collection coll = new ArrayList();
coll.add(123);
coll.add("Tom");
Iterator it = coll.iterator();
System.out.println(coll);
//iterate method1
for (int i = 0; i < coll.size(); i++) {
System.out.println(it.next());
}
//iterate method2
while(it.hasNext()){
System.out.println(it.next());
}
//method3
public void Test3() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add("Tom");
coll.add("Amy");
coll.add(false);
for(Object obj : coll){
System.out.println(obj);
}
}
删除集合中的数据
@Test
public void Test2() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(345);
coll.add("Tom");
Iterator it = coll.iterator();
while(it.hasNext()){
Object obj = it.next();
if("Tom".equals(obj)){
it.remove();
}
}
Iterator it2 = coll.iterator();
while(it2.hasNext()){
System.out.println(it2.next());
}
}
List
- 增:add(Object obj)
- 删:remove(int index) / remove (Object obj)
- 改:set(int index, Object ele)
- 查:get(int index), arraylist.indexOf(ele) / lastIndexOf
- 插:add(int index, Object ele)
- 长:size()
- 遍:interator/增强for循环/while
@Test
public void Test4() {
ArrayList al = new ArrayList();
al.add(123);
al.add(456);
al.add(456);
//insert element at index
al.add(0, 111);//000->0
//insert a list as an element
List ll = Arrays.asList(8, 9, 10);
al.add(ll);
System.out.println(al);
System.out.println(al.size());
System.out.println(al.get(4));
int index = al.indexOf(456);
System.out.println(index);
System.out.println(al.lastIndexOf(456));
//delete obj
Object obj = al.remove(0);
System.out.println(obj);
}
- 遍:interator/增强for循环/while
Iterator it = al.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
for(Object obj : al){
System.out.println(obj);
}
for(int i = 0;i < al.size();i++){
System.out.println(al.get(i));
}