Simple to understand: a collection of

001. overview

Java is a collection vessel which is located under the java.util package, including: Collection (interface), the Map (the interface), a set of tools.

Collection structure 002. FIG.

003.List Interface

0031. Features

  • Ordered, allowing duplicate set of elements

0032. The two main implementation class:

  • ArrayList: Based on the underlying array, the memory addresses are contiguous
  • LinkedList: Based on the underlying doubly linked list of memory addresses are not continuous

0033.ArrayList

  • Element may be one or more null;
  • Default initial capacity of 10, when the array size is insufficient, expand the capacity of 1.5 times;
List<String> list = new ArrayList<>();
list.add("hua");
list.add("ming");
list.add("fan");
list.add(null);
list.add("hua");
list.add(null);
System.out.println(list); 

//输出
[hua, ming, fan, null, hua, null]
复制代码

0034.LinkedList

  • Element may be one or more null
  • While achieving List and Deque interfaces

0035.ArrayList and LinkedList commonly used in several ways:

void add(E element)             添加元素
void add(int i,E element)       指定位置添加元素
E get(int i)                    获取元素
E set(int i,E element)          替换元素
E remove(int i)                 移除元素
int size()                      元素数量
boolean isEmpty()               集合中没有元素,返回true
复制代码

0036. traversal three ways

//举以下例子
public static void main(String[] args) {
    List<Integer> list = new ArrayList<>();
    for(int i=0;i<10;i++){
    	list.add(i);
    }
    System.out.println(list);
    
    //迭代器遍历
    Iterator<Integer> it = list.iterator();
    while(it.hasNext()){
    	System.out.print(it.next() +" ");
    }
    
    System.out.println();
    //遍历:增强型for循环
    for(Integer n:list){
    	System.out.print(n+" ");
    }
    
    System.out.println();
    //遍历:for
    for(int i=0;i<list.size();i++){
    	System.out.print(list.get(i)+" ");
    }	
}
复制代码

0037. scenarios

  • ArrayList is more suitable for query operations;
  • LinkedList more suitable insert and delete operations;

004.Set Interface

0041. Features

  • Disordered, does not allow the collection of repeating elements

0042. implementation class

  • HashSet: Based on the underlying HashMap achieve
  • TreeSet: Based on the underlying Binary Tree

0043.HashSet

  • Only one null elements
Set<String> sh = new HashSet<>();
sh.add("fan");
sh.add(null);
sh.add("hua");
sh.add("ming");
sh.add("hua");//被过滤掉
sh.add(null); //被过滤掉
System.out.println(sh);//[null, fan, hua, ming]
复制代码

005.Map Interface

0051. Features

  • It has a collection of mappings
  • Key data for (key-value) is stored

0052. The two main implementation class

  • HashMap: + list array based underlayer (and before the JDK7), an array of red-black tree + + list (JDK8)
  • HashTable: Based on the underlying hash table

0053.HashMap

  • Unique, value may not be unique
  • Null keys and allow a null value
  • Entry object is randomly oriented
  • 16 default initial capacity, the default load factor 0.75

0054.HashMap common method

Object put(Object key,Object value)  添加
Object get(Object key)               查询
Object remove(Object key)            移除
int size()                           长度
复制代码

0055. Code

Map<Integer,String> map = new HashMap<>();
map.put(1, "wang");
map.put(2, "li");
map.put(3, "hua");
map.put(4, null);
map.put(5, null);
map.put(2, "fan"); //直接覆盖前面相同的key的value
map.put(null, null);//key value 可以为空
System.out.println(map);//{null=null, 1=wang, 2=fan, 3=hua, 4=null, 5=null}
复制代码

0056. traversal way

//方式一:遍历
for(Integer i:map.keySet()){
    System.out.println("key:"+i+" values:"+map.get(i));
}

//方式二:遍历
Iterator<Entry<Integer,String>> list = map.entrySet().iterator();
while(list.hasNext()){
    Entry<Integer,String>entry = list.next();
    System.out.println("key:"+entry.getKey()+" values:"+entry.getValue());
}
复制代码

006. Tools

  • Collections, Arrays: class set of tools to help provide order the collection elements, such as search and thread-safe operations;
  • Comparable, Comparator: comparing the object is generally achieved for sorting the two slightly different;

Guess you like

Origin juejin.im/post/5dd2ae375188257322382d50