Java Collection Class Memo

Source: Internet
Author: User
Tags comparable

Collection
├list
│├linkedlist
│├arraylist
│└vector
│└stack
└set

└ Queue
Map
├hashtable
├hashmap

├ SortedMap

├ Enummap
└weakhashmap

Collection interface

collection is the most basic set interface, and a collection represents a set of object, the collection element (Elements). The Java SDK does not provide classes that inherit directly from collection.

The classes provided by the Java SDK are "sub-interfaces" such as list and set that inherit from collection.

All classes that implement the collection interface must provide two standard constructors: a parameterless constructor is used to create an empty collection, and a constructor that has a collection parameter is used to create

A new collection, this new collection has the same elements as the incoming collection. The latter constructor allows the user to copy a collection.

How do I traverse every element in the collection? Regardless of the actual type of collection, it supports a iterator () method that returns an iteration that uses the iteration to access the

Each element in the collection. Typical usage is as follows:
Iterator it = Collection.iterator (); Get an iteration child
while (It.hasnext ()) {
Object obj = It.next (); Get the next element
}
The two interfaces that are derived from the collection interface are list and set.

interface Realize Historical collection Classes
Set HashSet
TreeSet
List ArrayList Vector
LinkedList Stack
Map HashMap Hashtable
TreeMap Properties

Set

SetThe interface is alsoCollectioninterface, which represents a set concept in mathematical sense.SetContains no duplicate elements, i.e.SetThere are not two such elements in theE1 and E2, making e1.equals (E2) true. Since the data structure provided by the set interface is an abstraction of the set concept in mathematical sense, it needs to support the addition and deletion of objects without providing random access. So the Set interface is the same as the Collection interface, where the method is not introduced.

  HashSet class and TreeSet class

collection framework" support   Set   Interface Two common implementations: HashSet   and TreeSet . In more cases, you will use   HashSet   Stores the collection of duplicate freedom. For efficiency, add to   HashSet   objects need to implement the Hashcode ()   method in a way that properly distributes the hash code. Although most system classes cover  , Object   defaults to hashcode () Implementation, But when you create your own class to add to   HashSet  , do not forget to overwrite   hashcode () . The TreeSet   Implementation is useful when you want to extract elements in an orderly manner from the collection. To be successful, the elements added to TreeSet   must be sortable. The "collection Framework" adds support for   comparable   elements in sort Span style= "FONT-SIZE:18PX;" The "Comparable interfaces" section is described in detail. Let's assume for a moment that a tree knows how to keep the Java.lang   wrapper class elements in an orderly state. In general, it is faster to add elements to  , HashSet , and then convert the collection to TreeSet   for sequential traversal.

To optimize HashSet the use of space, you can tune the initial capacity and load factor. The TreeSet tuning option is not included because the tree is always balanced, ensuring the performance of insertions, deletions, and queries log(n) .

HashSetand TreeSet both implement Cloneable interfaces.

List interface

ListAn interface inherits an Collection interface to define an ordered collection that allows duplicates. This interface is not only able to process a part of the list, but also adds a location-oriented operation.

An ordered collection (also known as a sequence ). Users of this interface can precisely control the insertion position of each element in the list. The user can access the element based on the integer index of the element (where it is located in the list) and search for the elements in the list.

Unlike set, a list usually allows repeating elements. More formally, lists generally allow e1.equals (E2) elements to E1 and E2, and if the list itself allows null elements, they usually allow multiple null elements. It's hard to avoid repeating lists by throwing a run-time exception when a user tries to insert a repeating element, but we hope that the less you can use it, the better.

Linked Fast Reading Slow

Array Read fast Change slow

Hash two between all

collection is a collection interface
| ———— set sub-interface: unordered, no repetition allowed.
| ———— List sub-interface: Ordered, can have repeating elements.

Difference: Collections is a collection class

Set and list comparison:
Set: The retrieval element is inefficient, the deletion and insertion efficiency is high, and insertions and deletions do not cause the element position to change.
List: And arrays, lists can grow dynamically, find elements efficiently, and insert deleted elements inefficiently because they cause other elements to change position.

Set and list specific subclasses:
Set
| ———— HashSet: Storing elements in a hash table, inserting deletes quickly.

List
| ———— ArrayList: Dynamic array
| ———— linkedlist: Linked list, queue, stack.

Array and Java.util.Vector
Vector is an old dynamic array that is thread-synchronized and inefficient, and is generally not in favor of using

Java Collection Class Memo

Related Article

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.