Home  >  Article  >  Java  >  What is a collection in java?

What is a collection in java?

青灯夜游
青灯夜游Original
2019-12-26 13:49:274652browse

What is a collection in java?

What is a collection?

1. Collection classes are stored in the java.util package.

2. There are three main types of collections: set, list and map.

3. Collections store references to objects, not the objects themselves. So we call the objects in the collection the references to the objects in the collection.

To put it simply: a collection is a container for data, to be precise, it is a container for data object references.

Note:

● Collections can only store objects. For example, if you store an int type data 66 into a collection, it is actually converted into an Integer class and then stored automatically. Each basic data type in Java has a corresponding reference type.

● Collections store references to objects, not the objects themselves. So we call the objects in the collection the references to the objects in the collection. The object itself is still placed in the heap memory.

● Collections can store different types of data and an unlimited number of data types.

The origin of collections

Usually, our Java program needs to know how many objects are created when the program is running. But if the program is not running, during the program development stage, we have no idea how many objects are needed, or even their exact type. In order to meet these general programming needs, we require that any number of objects can be created at any time and anywhere. What are these objects used to accommodate? We thought of arrays first, but! Arrays can only store data of the same type, and their length is fixed, so what should we do? The collection came into being.

Main structure diagram of Java collection framework

What is a collection in java?

I found a feature, all the above collection classes, except the map series Collections, that is, the collection on the left, all implement the Iterator interface.

Iterator is an interface used to traverse elements in a collection. It mainly has three methods: hashNext(), next(), and remove().

Its sub-interface ListIterator adds three more methods based on it, namely add(), previous(), and hasPrevious().

We can see from the picture:

1. Collections are mainly divided into two interfaces: Collection and Map.

2. Collection is inherited by List and Set respectively.

3. List is implemented by AbstractList and then divided into 3 subclasses, ArrayList, LinkList and VectorList.

4. Set is implemented by AbstractSet and is divided into 2 subclasses, HashSet and TreeSet.

5. Map is implemented by AbstractMap and is divided into 2 subclasses, HashMap and TreeMap.

6. Map is implemented by Hashtable.

Recommended learning: Java video tutorial

The above is the detailed content of What is a collection in java?. For more information, please follow other related articles on the PHP Chinese website!

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