java collection framework related

This article mainly refers to the rookie tutorial and other website content, intrusion and deletion.

 

Java collection framework (mainly includes two types of containers), a unified architecture used to represent and manipulate collections.

1: Collection, which stores a collection of elements.

The Collection interface has 3 subtypes, List, Set, and Queue. Below are some abstract classes. Finally, there are concrete implementation classes. Commonly used are ArrayList, LinkedList, HashSet, LinkedHashSet, HashMap, LinkedHashMap, and so on.set

2: Map (Map), which stores the key/value pair mapping.

 

All collection frames include the following:

  1. Interface: is an abstract data type representing a collection. For example, Collection, List, Set, Map, etc. The reason for defining multiple interfaces is to manipulate collection objects in different ways
  2. Implementation (class): It is the concrete realization of the collection interface. Essentially, they are reusable data structures, such as: ArrayList, LinkedList, HashSet, HashMap.
  3. Algorithm: Some useful calculations performed by methods in objects that implement the collection interface, such as searching and sorting. These algorithms are called polymorphism, because the same method can have different implementations on similar interfaces.
Serial number Interface description
1 Collection interface

Collection is the most basic collection interface. A Collection represents a group of Objects, that is, the elements of the Collection. Java does not provide classes that directly inherit from Collection, but only provides sub-interfaces (such as List and set) inherited from.

The Collection interface stores a set of non-unique, unordered objects.

2 List interface

The List interface is an ordered Collection. Using this interface, you can precisely control the position where each element is inserted, and you can access the elements in the List by index (the position of the element in the List, similar to the subscript of the array), the first The index of the element is 0, and the same elements are allowed.

The List interface stores a group of non-unique, ordered (insertion order) objects.

3 Set

Set has exactly the same interface as Collection, but the behavior is different. Set does not store duplicate elements.

The Set interface stores a set of unique, unordered objects.

4 SortedSet
inherits from Set to store an ordered collection.
5 Map

The Map interface stores a set of key-value objects and provides a key (key) to value (value) mapping.

6 Map.Entry
describes an element (key/value pair) in a Map. It is an internal class of Map.
7 SortedMap
inherits from Map, keeping the Key in ascending order.
8 Enumeration
is a traditional interface and defined method through which you can enumerate (obtain one at a time) elements in a collection of objects. This traditional interface has been replaced by iterators.

The difference between Set and List

  • 1. The instance of the Set interface stores out-of-order, non-repetitive data. The List interface instance stores ordered and repeatable elements.

  • 2. Set retrieval efficiency is low, deletion and insertion efficiency is high, insertion and deletion will not cause element position change <implementation class has HashSet, TreeSet> .

  • 3. List is similar to array, which can grow dynamically, and automatically grow the length of List according to the length of the actual stored data. Finding elements is efficient, inserting and deleting are inefficient, because it will cause the position of other elements to change <implementation classes are ArrayList, LinkedList, Vector> .

 

Serial number Class description
1 Vector

This class is very similar to ArrayList, but this class is synchronous and can be used in multi-threaded situations. This class allows setting the default growth length, and the default expansion method is twice the original.

2 The Stack
stack is a subclass of Vector, which implements a standard last-in, first-out stack.
3 Dictionary
Dictionary 类是一个抽象类,用来存储键/值对,作用和Map类相似。
4 Hashtable

Hashtable 是 Dictionary(字典) 类的子类,位于 java.util 包中。

5 Properties
Properties 继承于 Hashtable,表示一个持久的属性集,属性列表中每个键及其对应值都是一个字符串。
6 BitSet
一个Bitset类创建一种特殊类型的数组来保存位值。BitSet中数组大小会随需要增加。

接口:

 

Collection 接口存储一组不唯一,无序的对象。

List 接口存储一组不唯一,有序(插入顺序)的对象。

Set 接口存储一组唯一,无序的对象。

SortedSet,继承于Set保存有序的集合。

Map 接口存储一组键值对象,提供key(键)到value(值)的映射。

Map.Entry,描述在一个Map中的一个元素(键/值对)。是一个Map的内部类。

SortedMap,继承于 Map,使 Key 保持在升序排列。

类:

HashSet,该类实现了Set接口,不允许出现重复元素,不保证集合中元素的顺序,允许包含值为null的元素,但最多只能一个。

LinkedHashSet,具有可预知迭代顺序的 Set 接口的哈希表和链接列表实现。

TreeSet,该类实现了Set接口,可以实现排序等功能。

 

HashMap ,根据键的HashCode值存储数据,具有很快的访问速度,最多允许一条记录的键为null,不支持线程同步。

TreeMap ,继承了AbstractMap,并且使用一颗树。

 

Vector ,该类和ArrayList非常相似,但是该类是同步的,可以用在多线程的情况,该类允许设置默认的增长长度,默认扩容方式为原来的2倍。

Stack ,栈是Vector的一个子类,它实现了一个标准的后进先出的栈。

Dictionary 类是一个抽象类,用来存储键/值对,作用和Map类相似。

Hashtable 是 Dictionary(字典) 类的子类,位于 java.util 包中。

BitSet,一个Bitset类创建一种特殊类型的数组来保存位值。BitSet中数组大小会随需要增加。

 

Java集合中Map接口的使用方法

Map接口

  • Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value;
  • Map中的键值对以Entry类型的对象实例形式存在;
  • 建(key值)不可重复,value值可以重复,一个value值可以和很多key值形成对应关系,每个建最多只能映射到一个值。
  • Map支持泛型,形式如:Map<K,V>
  • Map中使用put(K key,V value)方法添加 ,使用put()方法来修改Map中已存在的映射   // student.put(id, newStu);    Student stu = student.get(id);
  • 使用Map中的containsKey()和containsValue()方法来判断Map中是否存在键或值    // student.containsKey(stuID);   student.containsValue(new Student(null,name))   
  • 使用entrySet()方法遍历Map
  • 使用Map中的remove()方法删除Map中的映射

//eg:

Map的一般用法

1.声明一个Map :

Map map = new HashMap();

2 .向map中放值 ,注意: map是key-value的形式存放的,如:

map.put("sa","dd");

3 .从map中取值 :

String str = map.get("sa").toString,

The result is: str = "dd' // String value = map.get("key");

4. Traverse a map and get the key and value from it:

Map m= new HashMap();

for(Object obj : map.keySet()){

Object value = map.get(obj );

}


HashMap class

  • HashMap is an important implementation class of Map, and it is also the most commonly used, based on hash table implementation
  • Entry objects in HashMap are arranged out of order
  • Both the Key value and the value value can be null, but a HashMap can only have one mapping whose key value is null (key values ​​cannot be repeated)

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_41635653/article/details/86744347