Java Collection Class (Collection)

Source: Internet
Author: User
Tags set set

Why use a collection class?

You need to use a collection class when you don't know the number of data to store beforehand, or if you need a more flexible method than the array subscript access mechanism.

Collection classes are stored in the Java.util package

Java.util There are 13 classes that can be used to manage collection objects, which support collections such as sets, lists, or mappings

The collection class holds the object's reference, not the object itself, and for the convenience of expression, we say that the object in the collection refers to the reference to the object in the collection (reference)

There are 3 main types of collections:Set (set), list (listing), and map (map)

In general, the collection classes used in the Java API implement the Collection interface, and one of his class inheritance structures is as follows:

Collection<--list<--vector

Collection<--list<--arraylist

Collection<--list<--linkedlist

Collection<--set<--hashset

Collection<--set<--hashset<--linkedhashset

Collection<--set<--sortedset<-treeset 1. List of lists

The main feature of a list is that its objects are stored in a linear manner, without a specific order, with only one beginning and one end, and of course, it is different from a set that has no order at all.

The list is represented in the data structure as: array and vector, linked list, stack, queue.

Vector: array-based list, in fact, encapsulates the array does not have a number of features convenient for us to use, it is impossible to get out of the limit of the array.

Performance is also impossible beyond array. So, if possible, we need to use array more.

Another important point is the vector "synchronized", which is the only difference between the vector and the ArrayList.

ArrayList: Like a vector is an array-based implementation, but the difference is that ArrayList is not synchronous.

So it's better than vector in performance, but when running into a multithreaded environment, you need to manage the synchronization of threads.

Linkedlist:linkedlist differs from the previous two list, which is not array-based, so it is not limited by the array performance.

Each node of it contains two things:

1. Data of the node itself;

2. Information for the next node (nextnode).

So when adding to the LinkedList, deleting the action doesn't have to be a lot of data movement like Array-based lists.

As long as you change the information about the NextNode can be achieved. That's the advantage of LinkedList.

List Summary:

1. All lists can contain only a single table of different types of objects, not key-value key-value pairs. For example: [Tom,1,c];

2. All lists can have the same elements, such as vector can have [tom,koo,too,koo];

3. All lists can have null elements, for example [tom,null,1];

4. Array-based list (vector,arraylist) is suitable for querying, while LinkedList (linked list) is suitable for adding, deleting operations.


2.Set Set

Set is the simplest set of objects that are not sorted in a particular way, but simply adding objects to the collection, like putting things in a pocket.

Access to and operations on a centralized member are made through a reference to a centralized object, so the set cannot have duplicate objects.

The set also has multiple variants that can be sorted, such as TreeSet, which adds an object to a set of actions that will be inserted into an ordered sequence of objects according to a comparison rule.

It implements the SortedSet interface, which is the method of adding object comparisons. By iterating over the objects in the set, we can get an ascending set of objects.

HashSet: Although both set and list implement the collection interface, they are implemented in a very different way.

The list is basically based on an array. But set is implemented on the basis of HashMap, which is the fundamental difference between set and list.

The hashset is stored in the HashMap key as the corresponding storage of the set. Look at the implementation of the HashSet's add (Object obj) method to see it at a glance.

Public boolean Add (Object obj)

{

return Map.put (obj, PRESENT) = = NULL;

}

This is also why it is not possible to have duplicate entries in the set as in the list, because the HashMap key cannot be duplicated.

Linkedhashset:hashset a sub-class, a linked list.

Treeset:sortedset, which is different from HashSet, is that TreeSet is orderly. It is achieved through SORTEDMAP. (HashSet and TreeSet are ordered in order from small to large)

Set Summary:

1. Set implementation is based on map (HASHMAP);

2. The elements in set cannot be duplicated, and if an existing object is added using the Add (Object obj) method, the previous object is overwritten


3.map Mapping

Mappings differ significantly from sets or lists, and each item in the map is paired.

Each object stored in the map has an associated keyword (key) object that determines where the object is stored in the map, and must be supplied with the corresponding keyword when retrieving the object, as if looking up a word in the dictionary. The keyword should be unique.

The keyword itself does not determine where the object is stored, it needs to be processed over a hashing (hashing) technique, producing an integer value called a hash code.

A hash code is typically used as a bias, which determines where the key/object pair is stored relative to the starting position of the memory region assigned to the map.

Ideally, hash processing should result in a uniformly distributed value within a given range, and each keyword should have a different hash code.

Java Collection Class (Collection)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.