"Learning Notes" Java Collection framework 1

Source: Internet
Author: User
Tags repetition set set

Content:

/********************

Arrays and collections
Set Frame system
Set interface
List interface
Iterator interface
Map interface
Use of the old collection class
Internal comparator vs. external comparator
Integrated use of collection classes

********************/

One. Arrays

int[] Aryint = new INT[100]
Used to hold a set of data
Features of the array:
In Java, arrays are treated as objects
The array length must be fixed when the array is initialized
After the length is fixed, to change the size of the array length, it is necessary to control the length-size change of the logarithm group.

 PackageCom.fjnu.study2; Public classArray { Public Static voidMain (string[] args) {string[] STRs=NewSTRING[10];//as an object, define an array length of tenString[] strs1 = {"1", "2" }; String[] Strs2=NULL;        String[] STRS3; String[] STRS4=Newstring[] {};        System.out.println (strs.length);        System.out.println (strs1.length); //System.out.println (strs2.length);//error during run because the object is not instantiated//System.out.println (strs3.length);//error during compilation because no initial value is assignedSystem.out.println (strs4.length); }}
1020

Advantages and disadvantages of arrays:

The array access efficiency is high, the capacity is fixed when using, and it is suitable for basic data type access;

And the collection access efficiency has certain sacrifices, the aggregate capacity size can be changed according to the actual needs, provide rich access methods, suitable to become the object "container";

Collection is used to store objects.

equals equal of two objects, then their hash code must be equal

Two objects have equal hash codes, then their equal are not necessarily equal.

Collection interface

common                 methods for the root of a collection frame Boolean  boolean  equals (Object a)  // traverse int  size ()   void  Clear ()  boolean  Add (Object a),  Boolean Remove (Object a) 

Set interface and Features

The extended collection interface does not allow duplicate elements to allow a null value to add a limit to the Add (), Equals (), and Hashcode () methods

Implementation class for the set interface

Hashsetlinkedhashsetcopyonwritearraysettreeset

Features of HashSet

The bottom layer is implemented with a hash table algorithm. A collection class that is not thread synchronization discusses the concept of repetition in HashSet ? is the element in the HashSet really disordered? 

What is repetition

 Public Boolean equals (Object obj)    Indicates whether some other object is "equal" to this object.       Public int hashcode () generates the hash code for the object. Returns the physical address of the object store (which may not actually be), but is a token number used to identify the object. 

Linkedhashset Features

The Linkedhashset is characterized by a list of linkedhashset that are ordered (in order of insertions) that are not a collection class for thread synchronization

Copyonwritearrayset

The underlying feature of Copyonwritearrayset is the thread-safe collection class implemented in an array. elements are ordered. (inserted in order)

Copyonwritearrayset is implemented using arrays, and Linkhashset is implemented with a linked list, which is faster

Features of TreeSet

The underlying feature of the TreeSet is that the red-black tree structure implemented in TREEMAP is not thread-safe between the class elements that are ordered (not the insertion order, but rather the ordering between elements).

Iterator iterator

Boolean hasnext ()     true . Object Next ()    booleanremove  (object o)    removes the last element returned by the iterator from the collection that the iterator points to (optional action). This method can only be called once per call to next

The iterator will appear with a fast failure exception

What is the reason for a fast failure exception with a quick fail exception how to resolve a fast failure exception a: Using iterators to remove B: Using a thread-safe collection

Examples of reasons for a fast failure exception: if there are other threads modifying the data of this iteration during the iteration, the data for the iteration will be non-qualitative and an error will occur.

 PackageCom.fjnu.study2;ImportJava.util.HashSet;ImportJava.util.Set;ImportJava.util.Iterator; Public classInterg { Public Static voidMain (string[] args) {Set Set=NewHashSet (); Set.add ("AA"); Set.add ("BB"); Set.add ("CC");        SYSTEM.OUT.PRINTLN (set); Iterator Iterator=Set.iterator ();  while(Iterator.hasnext ()) {String str=(String) iterator.next (); if(Str.equals ("BB")){                //set.remove (str);//A quick failure exception occursIterator.remove ();    }} System.out.println (set); }}

List interface and features

Implementation class for the list interface

ArrayList class:      pushed by an array, suitable for frequently queried lists LinkedList class:      pushed by a doubly linked list, suitable for inserting frequently deleted lists

Features of ArrayList

The underlying feature of ArrayList is that elements implemented in an array are ordered (insert order) and are not thread-safe collection classes suitable for finding frequent operations

LinkedList

The underlying feature of LinkedList is that the array elements implemented in the list are ordered (insert order) and are not thread-safe collection classes suitable for adding and removing more frequent operations

Copyonwritearraylist

The Copyonwritearraylist feature is that the elements implemented in an array are ordered (insert order) thread-Safe Collection class lookup speed is relatively fast, add and delete change speed is relatively slow.

Note the use of sublist in list


Map interface

Keyword uniquely maps a key to a value each key can only be mapped to one value of the key value repeat, followed by overwriting the value of the preceding element. Important Methods Basic operations put (), get (), remove (), ContainsKey (), Containsvalue (), size (), and IsEmpty ()   batch Operations Putall () and Clear ()     collection views KeySet (), values (), and EntrySet ()

HashMap

The underlying is a hash algorithm implementation that allows a null key to allow multiple null value elements to be unordered between the collection classes that are not thread-safe

Linkedhashmap

The underlying is a list implementation that allows a null key to allow multiple null value elements to be ordered (in order of insertions) that are not thread-safe collection classes

TreeMap

The underlying is a set class that implements elements in a red-black tree algorithm that is orderly (ordered between objects) that is not thread-safe

Iteration Map

Iterate by using values to iterate through map.entry through keyset

The Old collection class

Vectorhashtableenumeration

HashTable

Hashtable feature does not allow null keys to not allow null values other features and usages are the same as HashMap.

Enumeration

contains the following methods: Boolean hasmoreelements ()      true false . Object nextelement ()    gets the next element of this enumeration. Differences from iterator:        iterators allow callers to take advantage of well-defined semantics to remove elements from the collection that the iterator points to during the iteration. Consider why the Remove method is not available in enumeration ?

"Learning Notes" Java Collection framework 1

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.