集合
Collection
- 所有的单列集合的父类
- List 有序集合
- ArrayList
- Vector
- LinkedList
- Set 无序集合
- HashSet
- TreeSet
Collection常用功能
Collection<String> col = new ArrayList<>();
col.add("我");
col.remove("你");
col.contains("他");
col.isEmpty();
col.size();
col.clear();
Iterator迭代器 – 接口
Collection<String> col = new ArrayList<>();
Iterator<String> it = col.iterator();
it.hasNext();
it.next();
增强for
for(String item: col){
sout(item);
}
泛型
- 通配符? ArrayList<?> arr = new ArrayList<>()
- 上限 ? extends E ,使用泛型只能是E类型的子类或者本身
- 下限 ? super E ,使用泛型只能是E类型的父类或者本身
数据结构
- 栈(先进后出)
- 队列 (先进先出)
- 数组(查询快,增删慢)
- 链表(查询慢,增删快)
- 红黑树
List
- 有序
- 实现类
- ArrayList (查询快,增删慢)
- LinkedList(查询快,增删慢)
LinkedList<String> link = new LinkedList<>();
link.addFirst("a");
link.addLast("b");
link.getFirst();
link.getLast();
link.clear();
link.isEmpty();
link.removeFirst();
link.removeLast();
Set集合
- 是一个接口
- 不包含重复元素
- 没有索引
- 实现类
- HashSet
- 无序集合
- 底层是存储的是一个哈希表
- 数据存储结构:数组+链表/红黑树
- LinkedHash
- 继承HashSet
Set<Integer> hash = new HashSet<>();
hash.add(1);
Iteator it = hash.iterator();
while(it.hasNext()){
sout(it.next());
}
Map集合
- 键值对
HashMap
Map<String,String> hm = new HashMap<>();
hm.put("key","value");
hm.remove("key");
hm.get("key");
hm.containsKey("key");
ArrayList list = hm.keySet();
HashTable
- 键值对都不能为null
- 单线程