A collection framework for the Java Foundation

Source: Internet
Author: User
Tags comparable

Collections and arrays are containers, and their biggest differences are:

    • Arrays can store basic data types or objects, and collections can store objects only (1.5 can also add basic data types);
    • The array length is fixed, and the collection length is variable, and you can store different types of objects.

Because of this, collections are more suitable for storing objects than arrays in some scenarios. In memory, arrays and collections are stored in the object's address. There are many containers under the Java Collection framework, and different containers have different data organization and storage methods, that is, different data structures. The composition diagram for the collection framework is as follows:

The methods of many containers under the collection framework are the same, and the methods of the collection are described below from the collection interface.

Collection interface

Common methods

    • Increment: Add (Object obj);
    • Delete: Remove (Object obj); Clear ();
    • Change: Seek intersection Remainall (Collection CLT); Finding the difference set RemoveAll (Collection CLT)
    • Judgment: Contains (Object obj); IsEmpty ();

An iterator is a method that takes an element from a collection, defined inside a collection class as an inner class, so that it can directly access the elements in the collection. Because different collection container data structure is different, the operation of removing elements is different, but it includes the common operation of taking out elements and judging content. Method iterator () returns the Iterator object for the corresponding collection Iterator,iterator object has Hasnext (), Next (), remove ()

The collection interface has two sub-interfaces: List and set. The elements in the list can be repeated and the elements are ordered (indexed), and the elements in the set are unordered and cannot be duplicated.

List interface

In addition to the common methods of the set mentioned above, List has some unique methods. Because the list is indexed, the method of manipulating the index is a unique method of the list.

    • Add: AddAll (intdex,collection); Add (Index,elem);p
    • Delete: Remove (index);
    • Change: Set (Index,elem);
    • Check: Get (index); Sublist (from, to); Listiterator (); GetIndex (Elem);

The list iterator Listiterator inherits the iterator interface because, when iterating over the collection elements, the collection element cannot be manipulated using the method of the set object, so the collection can only be manipulated using the iterator method, and there are too few methods in the iterator. The elements in the collection can be manipulated only through the methods in the Subinterface listiterator.

The common methods in Listiterator are Add (elem); Set (Elem); Hasprevious (); Nextindex (); Previous (); Previousindex ();

Common subclasses in List

    1. ArrayList array data structure, fast query speed, slow modification, thread out of sync.
    2. LinkedList linked list data structure, query speed is slow, modified fast.

Unique method: AddFirst (Elem); AddLast (Elem); Removefirst (); Removelast (); GetFirst (); GetLast (); Offerfirst appeared after 1.6 (elem); Offerlast (Elem); Pollfist (); Polllast ()

Vector array data structure, same as ArrayList, old version container, thread synchronization. Enumeration is the iterative way of vectors, similar to the Iterator object method.

Set interface

The elements in Set are not duplicated, and the function operation is consistent with the collection interface.

Common sub-categories include

    1. HashSet: The data structure is a hash table, which guarantees element uniqueness by calling Hashcode and the Equals method successively. If the hash value of the object is the same, then the Equals method is called to compare, so if the custom element is deposited, the two methods need to be overwritten. The contains and remove methods are also true.
    2. TreeSet: The data structure is a two-fork tree, which allows you to sort the elements in the set collection.
    • TreeSet one of the sorting methods: to make the elements themselves comparable, you need to implement the comparable interface, covering the CompareTo method. This method guarantees the uniqueness of the element, which does not matter with the hash value. This is also known as the natural order or the default order.
    • TreeSet Ordering method Two: When the element itself does not have the comparability, or the comparability is not required, it is necessary to make the collection itself comparable. When the collection TreeSet is initialized, the comparer (Comparator interface object) is specified in the constructor. The comparator implements the Compare method in the comparator interface.

When both methods are present, the comparator is the main one.

?

Generic type

After version 1.5, it is a security mechanism. The type conversion exception thrown at runtime is transferred to the compile time, avoiding coercion of type conversions. It is often common in a collection frame. When the type of data being manipulated is undefined, the object class is earlier defined to extend, and now defines generics to extend.

Generics can be defined on classes, methods, and interfaces, and for static methods in a class, the generics defined on the class cannot be accessed because the type of the generic is determined when the object is initialized, and the static method already exists at the time the class is created. If the type of the static method is undefined, you can define the generic on the method.

Generic qualification

? For wildcard characters, <? Extends e> can receive e-type or subclass of E; < The super E> can receive the E-type or the parent class of E.

?

Map<k,v> interface

The key k is mapped to an object of value V, and the keys in the map cannot be duplicated, and each key k only corresponds to a unique value of V.

General Method:

    • Add: Put (obj,obj);p utall (map<? extends K,? extends V>)
    • Delete: Clear (); Remove (obj k)
    • Get: Get (obj); size (); Values (); EntrySet (); keyset ()
    • Judgment: Contianskey (obj); containsvalue (obj); IsEmpty ()

?

    • Remove all keys at once keyset ()

All the keys in the map are stored in the set collection and returned.

    • Remove all values at once EntrySet (), return type set<map.entry<k,v>>

The mapping relationship is stored in a collection, and entry is an internal interface in the map interface.

The Map<k,v> interface has two common implementations of subclasses:

    1. HashMap

The underlying data structure is a hash table that allows null values and NULL keys to be unsynchronized

    1. TreeMap

The underlying data structure is a two-fork sort tree, sorted by key.

A collection framework for the Java Foundation

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.