Home  >  Article  >  Java  >  java collection

java collection

高洛峰
高洛峰Original
2016-11-30 15:07:281181browse

1. What is a collection? A container for storing objects. Object-oriented languages ​​embody things in the form of objects. Therefore, in order to facilitate the operation of multiple objects and store objects, collections are the most commonly used way to store objects. .

The appearance of collections is to hold objects. Collections can store objects of any type and have variable length. In the program, it may not be possible to know in advance how many objects are needed, so if an array is used to hold the objects, the length is difficult to define, and collections solve this problem.

2. The difference between sets and arrays

Arrays and collection classes are both containers

The length of arrays is fixed, and the length of collections is variable. Basic data types can be stored in arrays, and collections can only store objects. The data type stored in arrays is single, and objects of any type can be stored in collections.

Features of the collection class

are used to store objects, the length is variable, and different types of objects can be stored.

2.1. Disadvantages of arrays

A data container with a single storage type, complex operations (an array is immutable once declared) CRUD

1.3. Classification of collections

What collections do

1: Add objects To the collection

2: Delete the object from the collection

3: Find an object from the collection

4: Modify an object from the collection by adding, deleting, modifying and checking

Note: Collections and arrays store references to objects Rather than the object itself, Java engineers define different containers. Although the containers are different, there are still some commonalities that can be extracted. Finally, a top-level interface is extracted, and a collection framework is formed. How to learn? Of course, start from the top level, which contains the most common and basic behaviors. For specific use, you must choose a specific container. Why? Because the things that are constantly extracted may not be able to create objects. The possibility of abstraction is very high, and there are more methods for subclass objects. So look at the top layer and create the bottom layer. So what is the top level of the collection? It’s called Collection

Collection framework system

Collection: Single column collection

java collection List: Storage order, repeatable

ArrayList: Array implementation, fast search, slow addition and deletion

                                                                                                                              out Out             Through Out Implementation, when adding and deleting, will involve increasing the capacity of the array and copying elements. So it is slow. Arrays can be searched directly by index, so the search is faster

              LinkedList:         Linked List:                                                             ’ s ’ s ’ s ’ ‐ ‐ ‐ ‐ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ out out of ​​

Just do it yourself, delete it When the previous element remembers the next element, and the latter element remembers the previous element. This kind of addition and deletion efficiency is higher, but the query needs to traverse one by one, so the efficiency is low

       Vector : Same principle as ArrayList, but thread-safe, slightly lower efficiency

                                                    ’ s ’ ’ s ’ ’ s ’ s ’ s ’ s 1 t         onto             out out out out                 through ’ through HashSet

TreeSet

                                                       -                                                                                                                   ’ ’ s             ‐     ‐ out‐‐through ‐ t d to

HashMaps Treemap

Hashtable

Linkedhashmap

Why does so many sets of containers appear because each container storage method is different, this storage method is called data structure

Pay attention to the collection and array of collection and array All stored in are references to objects.

4. When to use what kind of collection

java collection

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn