Java collection knowledge finishing

concept:

The Java class library provides us with a lot of "collection classes". These collection classes use different "data structures" to store. Different data structures will lead to different performances of adding, deleting, modifying and checking.
Common data structure :
1). Array; query fast increase and delete slow
2). Linked list; add and delete fast query slow
3). Stack; last in first out push stack bomb stack
4). Queue; first in first out
5). Hash table; Queries and additions and deletions are fast
6).
Insert picture description here

set

Note: ①, collection can only store objects. For example, if you store an int type data 1 and put it into a collection, it is automatically converted into an Integer class and stored. Every basic type in Java has a corresponding reference type.
   ② The collection stores references to multiple objects, and the objects themselves are still placed in the heap memory.
   ③, the collection can store different types, an unlimited number of data types.
Insert knowledge:
packaging class:
basic data type ----- packaging type
byte ----- Byte
boolean ----- Boolean
short ----- Short
char ----- Character
int ---- -Integer
long ----- Long
float ----- Float
double ----- Double
A). Collection collection : single-column collection
| –List collection: 1. Ordered; 2. Can store duplicate elements; can Index operation elements;
| – ArrayList (subclass): array implementation
| – LinkedList (subclass): linked list implementation
| – Set collection: 1. Unordered; 2. Cannot store duplicate elements;
| – HashSet (subclass): Hash table
| –LinkedHashSet (subclass): linked list, hash table. Ordered
B). Map collection : two-column collection (key-value pairs)
| –HashMap (subclass): hash table implementation;
| –LinkedHashMap (subclass): linked list, hash table implementation

Common methods in Collection_Collection :
1). The methods declared in Collection will be implemented in List and Set collection;
2). Several commonly used methods:
add:
public boolean add (E e): give The object is added to the current collection, the return value: For ArrayList always returns true; For Set, only when storing duplicate elements, it will return false, storage failure;

Delete:
public void clear (): Clear all elements in the collection.
public boolean remove (E e): Remove the given object from the current collection. Return value: true if the deletion is successful; otherwise: false Note: If there are multiple "duplicate elements" in the collection, only the first one will be deleted. (Removal is dependent on the comparison of: equals ())

Judgment:
public boolean contains (Object obj): judge whether the current collection contains the given object. Note: The search is dependent on the comparison of equals ();
public boolean isEmpty (): determine whether the current collection is empty.

Get:
public int size (): Returns the number of elements in the collection.
public Object [] toArray (): Store the elements in the collection into an array. Convert the collection to an array.
public Iterator iterator (): Get an "iterator" for traversing the collection.

Supplementary knowledge
You cannot add or delete the original object using an iterator, otherwise the program will report an error (ConcurrentModificationException)

//定义一个list集合
ArrayList<String> list=new ArrayList<>();
Iterator it=list.iterator();
while(it.hasNext()){ //获取浮标 判断是否有下一个
System.out.print(it.next());//输出
}

Supplementary knowledge:
Enhanced for:
is a loop statement used to traverse arrays and collections. Syntactically it is more concise than a normal for loop.

//快捷键 arr.iter  
//注意增强for底层是迭代器不可以执行增删操作
  int [] arr={2,14,1,3,22,11,4};
        for (int i : arr) {
            System.out.println(i);
        }

List collection:

1). It inherits from the Collection interface. In addition, it has added some methods.
2). List collection features: ordered; can store duplicate elements; can operate elements through "index";
3). New commonly used method:
add public void add (int index, E element): will specify The element is added to the specified position in the collection.
Modification: public E set (int index, E element) Replace the element at the specified position in the set with the specified element, and return the element before the update.
Delete: public E remove (int index): remove the element at the specified position in the list, and return the element that was removed.
Get: public E get (int index): Returns the element at the specified position in the collection.
ArrayList (list collection subclass):
Array implementation, no unique method.
LinkedList (list collection subclass):
linked list implementation, there are some unique methods. Used to simulate stack and queue operations

set collection

Features
1. Unordered (refers to a different order than adding); 2. Cannot store duplicate elements; 3. Cannot be accessed by index.
hashSet (subclass):
Features: faster addition and deletion query
Note: When adding an element at a certain position, use hashCode () method to verify whether the index position exists, and then use equse () method to determine whether the elements are the same.

HashSet<String> set = new HashSet<>();
set.add("张三");
set.add("张三");//错误 不可存储重复 输出只会有一个“张三”
set.add("李四");
for (String s : set) {
            System.out.println(s);// 李四 张三
        }
set.clear();//清空集合
set.iterator();//迭代器 作用遍历

LinkedHashSet (subclass):
Features: It is a special case of Set, linked list, hash table structure. Ordered

 LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
        linkedHashSet.add("张三");
        linkedHashSet.add("李四");
        linkedHashSet.add("王五");
        for (String s : linkedHashSet) {
            System.out.println(s);//张三 李四 王五
        }

Map collection

Features: Two-column collection stores two objects: key-value pair
HashMap (subclass): hash table implementation;

HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("1","张三");//put() 添加元素的方法 “1”是键,“张三”是值
        hashMap.put("2","李四");
        hashMap.put("3","王五");
        hashMap.get("1");//get() 获取指定索引位置的值的方法
        hashMap.remove("2");//remove() 移除指定位置的键和值

There are two ways to traverse:

 Set<String> keySet = hashMap.keySet();//获取键的集合
        for (String s : keySet) {
            System.out.println(hashMap.get(s));
        }
    }
//获取集合中的每一个键值对对象的集合遍历
 Set<Map.Entry<String, String>> entrySet = hashMap.entrySet();
        for (Map.Entry<String, String> s : entrySet) {
            String key=s.getKey();
            String value=s.getValue();
            System.out.println(key+":"+value);
        }

LinkedHashMap (subclass): linked list, hash table implementation
Features: Ordered
No special method, similar to hashMap traversal method.

Published 8 original articles · Likes0 · Visits 38

Guess you like

Origin blog.csdn.net/FearSun/article/details/105385757