Collection Framework Classes

array

  • Three ways to create an array

    int[] array1 = new int[5];
    
    int[] array2 = new int[]{1,2,3,4,5};
    
    int[] array3 = {1,2,3,4,5}
  • array properties
    • Once created, the length of the array never changes

Arrays are all static methods that are utility classes for arrays

Located in: java.util

Throws NullPointerException if array reference is null

  • Common method

    sort(int a) //对数组进行排序
    fill(int[] a,int val) //将制动内容填充到数组中
    equals(int[] a,int[] b) //判断两个数组是否相等
    toString(int[] a) //输出数组信息
    deepToString(int[] a) //输出多维数组信息
    deepEquals(int[] a,int[] b) //判断多维数组是否相等

    container class

graph TD
a[java.lang.object]-->b[Map接口]
b-->c[HashMap]
b-->d[TreeMap]
a-->e[Collection接口]
e-->f[Set子接口]
e-->g[List子接口]
f-->h[HashSet]
f-->i[TreeSet]
g-->j[ArrayList]
g-->k[LinkedList]

Compared with sets, it is mainly reflected in efficiency, type, and the ability to save basic types

  • Sets are simple linear arrangements, so they are fast
    • But not flexible, the size is fixed once created
  • Collections can be saved directly, basic data types
  • The container class saves the object type, and the basic data type needs to be converted into a wrapper class before it can be saved (completed by automatic packing and unboxing)

  • The bottom layer of ArrayList is implemented with arrays, which are called dynamic arrays. They are not thread-synchronized and thread-unsafe Object types.

  • Vector Automatically growing array of objects, thread-safe, generally similar to the Object type of arrayslist

    ```
    ArrayList a = new ArrayList():
    a.add("ss");
    System.out.println(a.get(0));

get(int index) //Get the value at the specified index
````

  • HashSet can only get values ​​through iterators

  • Call the hash function, the storage method is disordered and not repeated, and the bottom layer is implemented with HashMap
    • The hash function is to obtain the hash value by calculation, and use the hash value as the storage location
  • Advantages: fast

  • TreeSet can only get values ​​through iterators

  • Orderly and not repeated, the bottom layer is implemented with TreeMap

  • The Map interface is similar to the dictionary in c# python

    ```Java
    Map d = new HashMap();
    d.put("a",2); //Add key-value pair
    System.out.println(d.get("a")//Get value by key) ;

### HashMap 值是无序的
* 底层由数组构建
* 影响性能的主要参数是: 底层容量  加载因子
    * 底层容量 底层数组的大小
    * 加载因子 当满足什么条件的时候自动扩容
* #### 遍历

* 第一种转换为entrySet;//效率高O(1)
Map d = new HashMap();
d.put("a",2);
d.put(1,333);
Iterator e = d.entrySet().iterator(); //把Map类型转换为entry集合的迭代器
while (e.hasNext()){
    Map.Entry a = (Map.Entry) e.next(); //转换为Map类型
    a.getKey(); //获取键
    a.getValue(); //获取值
}
* 第二种转换为keySet;//效率低O(2)
Map d = new HashMap();
d.put("a",2);
d.put(1,333);

Iterator a = d.keySet().iterator(); //把键集合转换为迭代器
while (a.hasNext()){
    Object c = a.next();    //获取键
    Object e = d.get(c);    //通过获得的键获取值
}
### TreeMap 基于红黑树实现,值是有序的,能够保持固定的顺序


## Conllections 
* 是服务于Conllection的工具类
* 关系类似于 Arrays 对 数组


* ## Java对集合的遍历
* **Iterator** 遍历迭代,最常见的迭代方式
* **ListIterator** : **Iterator** 的子接口,专门用于迭代List中的元素
* **Enumeration** 一个旧的接口类似于 Iterator
* **foreach** 可以便利数组和集合  JDK 5 之后增加的


**Iterator**常用方法

Iterator

boolean hasNext(); //Is it still possible to iterate

E next() //returns the next element of type E

void remove() //Remove the last returned element from the iterator's object


**ListIterator**常用方法

void add(E e) //Insert the specified element into the list
boolean hasNext() //Is it still possible to iterate

E next() //returns the next element of the iteration
boolean hasPrevious() //if iterates backwards, returns whether there are any elements before

int nextIndex() //returns the index value of the next element

E previous() //return to the previous element

void set(E e) //Replace the last element of forward, or reverse with the specified element
```

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325724786&siteId=291194637