The collection framework for Java

Source: Internet
Author: User

Collection Frame

Overview:
  Collection Frame is a Java container class that is designed to load objects, and the basic data types are transformed into wrapper classes .

        


the characteristics of the array : the length is fixed.
features of the collection framework : The length is not fixed.

      

Classification:

Three main interfaces:

Collection: (base class)

  |--list: elements are ordered and elements can be duplicated . Because the collection system has an index .

    |--arraylist: The underlying data structure uses an array structure . Features: Delete quickly, find slow. thread is out of sync.

    |--linkedlist: A linked list data structure used at the bottom . Features: Delete slowly, Find Fast. thread is out of sync.

    |--vector: The underlying is the array data structure . Thread synchronization. replaced by the ArrayList. Because the efficiency is low. (SAFE)

       

      && Enumeration is the unique way to remove vectors. Find enumerations and iterators much like. In fact, enumerations and iterations are the same. Because the name of the enumeration and the name of the method are too long. So it was replaced by an iterator.
 

&& Vector---JDK1.0 version, there is no ArrayList(JDK1.2 version, replace Vector)

|--set: elements are unordered (the order in which they are deposited and taken out is not necessarily consistent) and the elements cannot be duplicated .

    |--hashset: The underlying data structure is a hash table (). is not thread-safe. Different steps.

The principle of guaranteeing element uniqueness is to determine whether the hashcode value of an element is the same. If it is the same, it continues to judge whether the element's Equals method is true.

  |--treeset:

becomes an ordered set of elements without duplicates, each TreeSet element must implement a sort of interface. Comparable, the set is the standard for the sorted field, and if it is 0, the second element is not added if the duplicate is judged.
The underlying TREEMAP implementation.

The underlying data structure is a two-fork tree. The basis for guaranteeing the uniqueness of the element: CompareTo method return 0.

TreeSet the first way to sort: to make the elements themselves comparable. The element needs to implement the comparable interface, overriding the CompareTo method. It also becomes the natural order of the elements, or is called the default order.

The second sort of treeset. When the elements themselves are not comparable, or have the comparative is not required. At this point you need to make the collection itself comparable. When the collection is initialized, there is a comparison method.

The functions and Collection of the set set are consistent.

List:

Unique method. All the methods that can operate the angle mark are the methods peculiar to the system.

Orderly: There are order, subscript, and subscript to access the element.
unordered: no order, no subscript, no direct access to elements

      

      

List Interface
    is an orderly Collection interface. There are subscript, you can directly access the element by subscript. You can add duplicate elements. Multiple null elements can be added

Three methods of traversal:

Iterators:

An iterator that is unique to the list collection. Listiterator is a sub-interface of iterator. In an iteration, you cannot manipulate the elements in the collection through the methods of the collection object.

Because the concurrentmodificationexception exception occurs. So, in an iterator, you can only use an iterator-sparing element, but the iterator method is limited,

Can only judge the elements, take out, delete the operation, if you want to other operations such as add, modify, etc., you need to use its sub-interface, Listiterator.

This interface can only be obtained through the Listiterator method of the list collection.

MAP:

  Map is a form of key-value, andkey cannot be duplicated and is associated with only one value.  

      

Two ways to remove a map collection:

1,set<k> KeySet: Deposits all the keys in the map into the set collection. Because set has iterators.

All the keys that can be iterated out are taken in accordance with the Get method. Gets the value corresponding to each key.

The extraction principle of the Map collection: The Map collection is turned into a set set. is removed by an iterator.


2,set<map.entry<k,v>> EntrySet: The mapping relationships in the map collection are stored in the set set,

And the data type of the relationship is: map.entry

Entry is actually a static internal interface in the map.

Why should it be defined in the interior?

Because only with the map collection, there is a key-value pair, there will be a key value of the mapping relationship.

A relationship belongs to an internal thing in the map collection.

And the object accesses the elements in the map collection directly.

      

traversal of map:

Generic type:

    A Java generic parameter can only represent a class and cannot represent an individual object. Because the actual type of the type parameter of the Java generic is

Compile-time is eliminated, so it is not possible to know the type of the type parameter at run time. When the Java compiler compiles generics, it automatically

The encoding of the type conversion is added, so the speed of operation is not accelerated by the use of generics. Java allows type parameters for individual generics

Constraints, including the following two forms (assuming T is a generic type parameter, C is a generic class, a generic class, or a type of generics

Parameter): T implements interface I. T is C, or inherits from C. A generic class cannot implement the Throwable interface.

Collection of all class diagrams

The collection framework for Java

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.