Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the collections commonly used in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what are the collections commonly used in Java". In daily operation, I believe that many people have doubts about the collections commonly used in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "what collections are commonly used in Java?" Next, please follow the editor to study!

Let's take a look at an overview of collections. Here are some of the most commonly used collections from top to bottom.

1. Collection interface

Java.util.Collection is a collection interface. It provides a general interface method to perform basic operations on collection objects. The Collection interface has many concrete implementations in the Java class library. The meaning of the Collection interface is to provide a maximum unified mode of operation for a variety of specific collections. Declares a general method that applies to JAVA collections (including only Set and List). Both Set and List inherit Conllection,Map.

2. Collection type

There are three main types of JAVA collections: Set (set), List (list), and Map (mapping)

Set collection: collection elements cannot be repeated, elements are not ordered, so it cannot access elements based on location.

List collection: collection elements are repeatable and sequential, so it can access elements based on location.

Map: it contains key-value pairs. Map keys can not be repeated, you can ensure the insertion order of elements, can also be sorted.

3. Collection introduction

Set (set):

HashSet

HashSet is implemented based on HashMap, which does not allow repeating elements, does not guarantee the order of elements in the and political set, and allows elements with a value of null, but there can be at most one null element.

TreeSet

TreeSet can implement collections with functions such as sorting. When object elements are added to the collection, it automatically inserts them into an ordered sequence of objects according to some comparison rule, and ensures that the collection elements are arranged in "ascending order".

LinkedHashSet

It has the query speed of HashSet, and internally uses linked lists to maintain the order of elements (the order in which they are inserted), so when you use iterators to traverse the Set, the results are displayed in the order in which elements are inserted.

List (list):

ArrayList

The internal structure is based on an array implementation, which allows random access to elements, and is slow to insert and delete elements into the ArrayList.

LinkedList

LinkedList is a bi-directional linked list inherited from AbstractSequentialList and can be operated as a stack, queue, or double-ended queue. LinkedList also implements List, Deque (double-ended queue), Cloneable (capable of cloning), java.io.Serializable (support for serialization and de-transmission through serialization) and other interfaces. LinkedList is asynchronous.

Each node contains forward and backward pointers in addition to elements.

Create a new LinkedList and generate a header node (header, that is, a header pointer) whose element is null.

It is self-contained, and both next and previous pointers point to themselves. After executing the add (Object obj) method, a new node is generated

The next of the Header node points to the first node of the linked list, and the previous points to the last node of the linked list, which in this case is all first, and then adds an object that looks like this.

Vector

Vector is a vector queue, which is a class added by the JDK1.0 version. Inherit from AbstractList, implement List (support for related add, delete, modify, traversal, etc.), RandomAccess (random access function), Cloneable (can be cloned) these interfaces.

Vector actually stores data through an array. When we construct Vecotr; if we use the default constructor, the default capacity size of Vector is 10.

When the Vector capacity is not enough to accommodate all the elements, the capacity of the Vector increases. If the capacity increase factor is more than 0, the value of the capacity is increased by the "capacity increase factor"; otherwise, the capacity is doubled. The clone function of Vector, which clones all elements into an array. Unlike ArrayList, operations in Vector are thread-safe.

Stack

Stack is a stack, and its characteristics are: first in, then out (FILO, First In Last Out).

Stack inherits from Vector (vector queue), and since Vector is implemented through arrays, this means that Stack is also implemented through arrays, not linked lists.

Map (Mapping):

Map is based on the implementation of a hash table. Map is a set of mapping key objects and value objects, each of which contains a pair of key objects and value objects.

HashMap

At the bottom of the HashMap is an array structure (called Entry Table), and each item in the array is a linked list (called Bucket, designed to resolve hash conflicts). When you create a new HashMap, an array is initialized. The overhead of inserting and querying "key-value pairs" is fixed, and the capacity capacity and load factor load factor can be set by the constructor to adjust the performance of the container. The initialization structure is as follows:

Each bucket contains a linked list of Entry (a structure customized by map that contains a subsequent pointer).

After put (key, value), its structure is as follows:

LinkedHashMap

Similar to HashMap, but when iterating through it, the order in which "key-value pairs" are obtained is the order in which they are inserted, or the most recently least used (LRU) order, only a little slower than HashMap. It is faster during iterative access because it uses linked lists to maintain internal order.

TreeMap

Based on the implementation of the red-black tree data structure, when you view "keys" or "key-value pairs", they are sorted (the order is determined by Comparabel or Comparator). The characteristic of TreeMap is that the results you get are sorted. TreeMap is the only Map with the subMap () method, which can return a subtree.

WeakHashMap

Objects used in the weak key Map,Map are also allowed to be released: this is designed to solve special problems. If no reference other than map points to a key, the key can be reclaimed by the garbage collector.

IdentifyHashMap

Hash map, which uses = = instead of equals () to compare "keys", is designed to solve special problems.

Hashtable

Hashtable is similar to HashMap, Hashtable inherits from the Dictionary class and implements the Map interface, except that it does not allow recorded keys or values to be empty; compared with HashMap, Hashtable is thread synchronous, that is, only one thread can write Hashtable at any time, which also causes Hashtable to be slow to write. And Hashtable can be traversed through Enumeration.

At this point, the study of "what are the common collections of Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report