Learn Java Collections

Source: Internet
Author: User
Tags repetition set set

1, List interface (inherit from collection interface) and its implementation class

The list interface and its implementation class are variable-capacity lists that access elements in the collection by index .

Features: The elements in the set are ordered and repeatable ;

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

Implementation class:
ArrayList implements an array that is variable in size and can be accessed like a linked list. It provides functionality similar to the vector class but is out of sync , it is a list implemented in array mode, allowing fast random access .

LinkedList implement a linked list that provides the best sequential access for inserting and removing elements. A linked list defined by this class can also be used as a stack or as a queue. Provides the best sequential access , suitable for inserting and removing elements .

2, set set interface (inherit from collection interface) and its implementation class

Features: The elements in the collection are not sorted in a particular way , but simply adding objects to the collection is like putting something in your pocket.

Access to and manipulation of members in set is done through references to objects in set , so there can be no duplicate objects in the set .

Set also has a variety of variants, such as the ability to sort, such as TreeSet, which adds an object to a set of actions to 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.

Implementation class:

HashSet To quickly locate an element, it is important to note that: objects stored in HashSet must implement the Hashcode () method ;

TreeSet The elements into which they are stored .

3. Mapping map interface and its implementation class

The map is a separate interface and does not inherit from the Collection. Map is a container that associates key objects with value objects.

Features:key does not allow repetition .

Mappings are significantly different from sets or lists, and each item in the map is paired, and map is the container that associates the key object with the value object. 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, meaning that the key object in the map does not allow repetition, which is to ensure consistency of the query results.

The keyword itself does not determine where the object is stored, it requires a hash (hashing) technology, which produces an integer value called a hash code, which is typically used as a bias, which is relative to the starting position of the memory region allocated to the mapping, thus determining where the key/object pair is stored. Ideally, hash processing should result in a uniformly distributed value within a given range, and each keyword should have a different hash code.

Implementation class:

HashMap implements a key-to-value mapping hash table, which takes a key to the value object, does not have a sequence, gets the value through get (key), allows the storage of an empty object, and allows the key to be empty (since the key must be unique, of course there is only one);

HashTable implements an image, all keys must be non-empty . In order to work efficiently, the class defining the key must implement the Hashcode () method and the equal () method . This class is an inheritance of the previous Java implementation and is usually better used in other classes that implement the image.

Select TreeMap when the order of the elements is important , and use HashMap when the elements do not have to be stored in a particular order. The use of Hashtable is not recommended because HashMap provides all the similar features and is faster. HashMap can also be converted to synchronous when you need to use it in a multithreaded environment.

Properties in general, after the property file is read into the stream, it is saved in the form of a key-value pair to facilitate reading of the data.

4. Iterator interface

The iterator interface is located in the Java.util package, which is an iterator that iterates over the collection.

A collection container (such as List, Set, map, etc.) itself provides a way to handle the placement and removal of elements, but a single selection of elements is restricted. So we're going to use iterator to select the element in the container, which converts the container into a sequence.

Iterator Iter=object.iterator ();

while (Iter.hasnext ()) {}

Learn Java Collections

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.