Java basics: Containers and java basic containers

Source: Internet
Author: User
Tags comparable

Java basics: Containers and java basic containers

Reprinted with the source: jiq •'s technical Blog



1. Collection: stores independent elements

The interfaces in Collection are all optional operations, and their implementation classes do not necessarily implement all of their interfaces. This is to prevent "interface explosion ". The most common Unsupported Operation is derived from the containers supported by a fixed-size data structure. For example, when ArrayList. asList is used to convert an array to a List, such containers are obtained.

(1)Collection List

List has many additional interfaces compared with Collection. In particular, the upload list.

 

Advantages

Disadvantages

Save the order of Elements

Application

ArrayList

Fast Random AccessIs implemented using arrays internally.

Slow iteration, insertion, and deletion of elements, especially when the List] size is relatively large.

Insert sequence

Variable Length Array

Shortlist

Iteration (optimized sequential access), fast insertion and Deletion

Implement using a two-way linked list internally

Slow random access speed

Insert sequence

Sequential access: insert and delete elements in batches

 

(2) Set of Collection

Each element stored in the Set must be unique, because the Set does not store duplicate elements, but the elements stored in the Set must define the equals () method to ensure the uniqueness of the object. In addition, Set and Collection have the same interface, and Set does not guarantee the order of elements. Therefore, Set does not contain the get Method for random access, because Set maintains its own internal order and does not require random access.

 

Advantages

Save the order of Elements

Requirements

HashSet

Design for quick search

Hash Storage

The hashCode () method must be defined.

LinkedHashSet

The query speed is the same as that of HashSet, but insertion is slower than that of HashSet because it maintains elements in the form of a linked list.

Use the linked list to maintain the element sequence (insertion sequence)

The hashCode () method must be defined.

TreeSet

Stores ordered sets. The underlying layer is implemented through TreeMap.

Maintain elements in order

Must implement the Comparable interface (including the compareTo method)

NOTE: If there are no special restrictions, HashSet should be your default choice because it optimizes the speed.

 

(3) Queue of Collection:

 

Features

Save the order of Elements

Shortlist

In addition to the common List, the consumer List also adds three data structures: <queue, stack, and bidirectional queue>.

In particular, when simulating the Queue, inserting and deleting elements at both ends is very fast (optimized ).

Insertion Sequence

PriorityQueue

The Comparable interface is required to retrieve elements in order of sorting.

Sorting order

 

2. Map: store key-value pairs

To ensure the uniqueness of the Map, any "key" requires an equals () method to determine whether the current key is the same as the key in the table.

Features

Save the order of Elements

Requirements

HashMap

Map is based on hashed storage. The overhead of inserting and querying "key-value pairs" is fixed.

Hash Storage

The key to be saved must have the hashCode () method. Of course, the returned ID does not have to be unique.

LinkedHashMap

To increase the speed of hashed all elements, the insert query is only a little slower than HashMap, because it maintains the hashed data structure and maintains the Linked List (insertion sequence ). However, iterative access is faster because linked lists are used internally to maintain the order.

Insert sequence

You also need to implement the hashCode () method by using a key.

TreeMap

Map is implemented based on the red and black trees. Therefore, the results are sorted.

Red/black tree

To sort data, the Comparable interface must be implemented.

Other maps designed to solve special problems include IdentityHashMap, WeakHashMap, and ConcurrentHashMap.

Experiments show that the insertion of all maps except IdentityHashMap slows down significantly as the Map size increases, but the search cost is much lower. I guess this is because the equals method is used to ensure the uniqueness of the key value for each insert. However, the most common operations of Map are query operations, so the situation is optimistic.


Summary:

(1) to ensure the security of your container type, you need to use generics (otherwise, the compiler allows you to insert various types into the container, as long as they are objects );

(2) try to convert a container to an interface when creating it, as shown in the following figure:

List <Apple> apples = newArrayList <Apple> ();

When you decide to modify your implementation, you only need to modify it in the created place.

(3) Java has a large number of excellent methods for containers. They are encapsulated into the java. util. Collections class, all of which are static methods. For example, in the Collections class, the unmodifiableMap/List/Set () method sets Collection and Map as unchangeable, and synchronizedCollection/List/Set/Map Methods synchronize the entire container. In addition, sorting, filling, reverse setting, and maximum and minimum values can also be found.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.