Java Collection Class Learning Note 2

Source: Internet
Author: User

Two, a specific set

Td>linkedlist
Collection type description
ArrayList An index sequence that can grow and shrink dynamically
An ordered sequence of efficiently inserting and deleting operations at any location
arraydeque A double-ended queue implemented with a looping array
HashSet an unordered collection without repeating elements
TreeSet an ordered set
E Numset A set of values that contain enumerated types
linkedhashset A collection of elements that can remember the insertion order of an element
Pri Orityqueue A collection that allows for efficient deletion of the smallest element
HashMap A data structure that stores key/value associations
TR Eemap A map table with an ordered array of key values
enummap A key-value-enumerated mapping table
LINKEDHASHM AP A mapping table that remembers the order in which key/value items are added
weakhashmap A mapping table that can be reclaimed by the garbage collector after its value is not available
Identityhashmap A mapping table that uses = = instead of comparing key values with equals

As in the table above, other classes implement the collection interface in addition to the classes that end in map. The class with the end of map implements the map interface.

1. Linked list

There is a big flaw in arrays and array lists, and it pays to remove an element from the middle of an array because all elements in the array that are behind the deleted element are moved to the front end of the array. Inserting an element in the middle of an array is also true. However, the list just solves the problem.

All linked lists are two-way linked, and the list stores each object in a separate node. Each node holds a reference to the next node in the sequence and a reference to the predecessor node.

A linked list provides a get () method to access a specific element. You should never use a for loop to traverse a linked list with very little efficiency. Each time you look for an element to start the search again from the head of the list, the LinkedList object does not do anything to cache location information at all.

The list iterator interface also has a method that can tell the index of the current position. In fact, because a Java iterator points to a position between two elements, you can produce two indexes at the same time: The Nextindex method returns the integer index of the element returned when the next method is called The Previousindex method returns the integer index of the element returned the next time the previous method is called. The efficiency of these two methods is higher.

2. Hash table

Lists and arrays can order the elements according to People's wishes. But if you want to see a specific element and forget its location, you need to access all the elements until you find it. Hash table solves this problem, the hash table calculates an integer for each object, called the hash code. Objects of different data regions will produce different hash codes.

In Java, a hash table is implemented with a list array. Each list is called a bucket, and you can set the number of buckets if you know roughly how many elements will eventually be inserted into the element. It is better to set the number of buckets to a prime, in case the key is clustered, the number of buckets is set to 75%-150% of the number of elements.

A hash table can be used to implement several important data structures. The simplest of these is the set type. Set is a collection of elements that have no repeating elements. The Add method of set first finds the object to be added in the set, and if it does not, adds the object.

A HashSet class is provided in the Java class Library, which implements a hash-list-based set. You can add elements by using the Add method. The Contains method has been redefined to quickly see if an element already appears in the set.

3. Tree Set

TreeSet is very similar to a hash set, but it is better than a hash set. A tree set is an ordered set of elements that can be inserted into a collection in any order, and each value is automatically rendered in sorted order when the collection is traversed. For example:

1 New Treeset<string>(); 2 sorter.add ("Bob"); 3 sorter.add ("Amy"); 4 sorter.add ("Carl"); 5  for (String Sorter) System.println (s);

At this time, print it out: Amy,bob,carl. Adding an element to a tree is slower than adding it to a hash table, but it is much faster than adding an element to an array or to the correct location of a linked list. If the tree contains n elements, it is necessary to find the correct location for the new element on average, log is 2 for the base n-th comparison. Therefore, adding an element to TreeSet is slower than adding it to hashset.

4. Priority queue

Elements in the priority queue can be inserted in any order, but are always retrieved by sort. In other words, when the Remove method is called, the smallest element in the current priority queue is always obtained.

A typical instance is task scheduling.

Java Collection Class Learning Note 2

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.