整体概述
部分描述与遍历方法
List
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
通过判断oldCapacity与minCapacity
扩容扩到原来长度+二分之一倍 即1.5倍
- 遍历
Collection<String> list= new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
迭代器:
Iterator<String> it = list.iterator();
while(it.hasNext()) {
System.out.print(it.next())
}
foreach :
for (String aa:list) {
System.out.println(aa);
}
Vector
Vector查找元素时,是分为元素为null和不为null两种方式来判断的,这也说明Vector允许添加null元素;同时,如果这个元素在Vector中存在多个,则只会找出从index开始,最先出现的那个
public synchronized int indexOf(Object o, int index) {
if (o == null) {
for (int i = index ; i < elementCount ; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = index ; i < elementCount ; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
LinkList
- 遍历
可以利用LinkedList实现栈(stack)、队列(queue)、双向队列(double-ended queue )
LinkedList<String> list= new LinkedList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
迭代器:
Iterator<String> it = list.iterator();
while(it.hasNext()) {
System.out.print(it.next())
}
foreach :
for (String aa:list) {
System.out.println(aa);
}
Set
HashSet 和TreeSet的底层实现分别来自于 HashMap TreeMap
Set<String> list= new HashSet<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
迭代器:
Iterator<String> it = list.iterator();
while(it.hasNext()) {
System.out.print(it.next())
}
foreach :
for (String aa:list) {
System.out.println(aa);
}
HashMap
java8中 hashMap的数据存储是链表+红黑树的组合
当链表长度大于8,且总数据量大于64的时候,链表就会转化成红黑树
阈值:默认大小*负载因子
当前存放新值的时候已有元素的个数大于等于阈值 就会扩容
存入数据到某一条链表上,此时数据大于8,且总数量小于64即发生扩容
- 遍历
HashMap<String,Object> map = new HashMap<>();
map.put("aa",11);
map.put("bb",22);
map.put("cc",33);
map.put("dd",44);
Foreach
//分别遍历key value
for(String key : map.keySet()){
System.out.println(key);
}
for(Object value : map.values()){
System.out.println(value);
}
//通过key集合获得value
Set<String> keySet = map.keySet();
System.out.println(keySet);
for(String key : keySet){
System.out.println(key+" "+map.get(key));
}
//通过entry遍历
//Set<Map.Entry<String,Object>> entrySet = map.entrySet();
for(Map.Entry<String,Object> entry : map.entrySet()){
System.out.println(entry.getKey()+" "+entry.getValue());
}
迭代器
Iterator<Map.Entry<String,Object>> iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<String,Object> entry = iterator.next();
// System.out.println(entry);
System.out.println(entry.getKey()+" "+entry.getValue());
}
TreeMap
一般情况下我们可以通过自然排序对key进行排序,但是一些场景下就需要自定义排序了
例如key是user时:
package com.swyee01.controller;
/**
* @Program Blog
**/
public class User implements Comparable{
String name;
Integer age;
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public int compareTo(Object o) {
if(o instanceof User){
User user = (User) o;
int i = this.name.compareTo(user.name);
if (i != 0){
return i;//正序
//return -i;//倒叙
} else {
return Integer.compare(this.age,user.age);
}
} else {
throw new RuntimeException("输入格式不正确");
}
}
}
public static void main(String[] args) {
TreeMap map = new TreeMap();
User user1 = new User("aa",27);
User user2 = new User("aa",3);
User user3 = new User("bb",22);
User user4 = new User("cc",19);
map.put(user1,1);
map.put(user2,2);
map.put(user3,3);
map.put(user4,4);
Set entrySet = map.entrySet();
Iterator iterator = entrySet.iterator();
while (iterator.hasNext()){
Object o = iterator.next();
Map.Entry entry = (Map.Entry) o;
User key = (User) entry.getKey();
System.out.println( key.name+""+key.age+ "" + entry.getValue());
}
}