Java Collection Learning Notes

Source: Internet
Author: User

1.Collection (single row combination)

List (ordered, data can be duplicated)

ArrayList: The underlying data structure is an array, querying fast, adding and deleting slow, thread unsafe, high efficiency.

Vector: The underlying data structure is an array, query fast, delete and subtract slow, thread safety, low efficiency.

LinkedList: The underlying data structure is a linked list, slow query, delete and deletion fast, thread unsafe, high efficiency.

Set (unordered, data unique)

HashSet: The underlying data structure is a hash table. A hash table relies on two methods: Hashcode () and Equals (), which can be generated automatically by the compiler.

Execution order: First determine if the hashcode () value is the same

Yes: Continue with equals () to see that the return value is true: The description element is duplicated, not added, or false: it is added directly to the collection.

No: It is added directly to the collection.

Linkerhashset: The underlying data structure consists of a list of drinks Hashibian, the list guarantees the elements are ordered, and the hash table guarantees the elements unique.

TreeSet: The underlying data structure is a red-black tree (a self-balancing two-fork tree).

How to guarantee element uniqueness: Based on whether the return value of the comparison is determined by the.

How to ensure the ordering of elements:

Natural ordering (elements are comparative): the class to which the element belongs implements the comparable interface.

Comparator Ordering (collection is comparative): Let the collection receive a comparator implementation class object.

2.Map (double-column collection)

The data structure of the map collection is only valid for the key, regardless of the value. A key-value pair is stored as an element, the key is unique, and the value is repeatable.

HASHMAP: The underlying data structure is a hash table, thread insecure, high efficiency.

A hash table relies on two methods: Hashcode () and Equals (), which can be generated automatically by the compiler.

Execution order: First determine if the hashcode () value is the same

Yes: Continue with equals () to see that the return value is true: The description element is duplicated, not added, or false: it is added directly to the collection.

No: It is added directly to the collection.

Linkedhashmap: The underlying data structure consists of a list of drinks Hashibian, the list guarantees the elements are ordered, and the hash table guarantees the elements unique.

Hashtable: The underlying data structure is a hash table, thread-safe, inefficient.

A hash table relies on two methods: Hashcode () and Equals (), which can be generated automatically by the compiler.

Execution order: First determine if the hashcode () value is the same

Yes: Continue with equals () to see that the return value is true: The description element is duplicated, not added, or false: it is added directly to the collection.

No: It is added directly to the collection.

TREEMAP: The underlying data structure is a red-black tree (a self-balancing two-fork tree).

How to guarantee element uniqueness: Based on whether the return value of the comparison is determined by the.

How to ensure the ordering of elements:

Natural ordering (elements are comparative): the class to which the element belongs implements the comparable interface.

Comparator Ordering (collection is comparative): Let the collection receive a comparator implementation class object.

3. Common methods and traversal of collections

Collection Common methods:

Collection C =NewArrayList (); C.add ("Hello");//Add an element to the collectionC.add (" World"); C.clear ();//clears all elements in the collectionC.remove (" World");//removes the specified element from the collectionC.contains ("Hello");//determines whether the specified element is contained in the collectionC.isempty ();//determines whether the collection is emptyC.size ();//gets the number of elements in the collectionCollection C2=NewArrayList (); C2.add ("Android"); C.addall (C2);//add an element of a collectionC.removeall (C2);//removes a collection of elements, as long as one is removed to return trueC2.containsall (c);//Determines whether the collection contains the specified collection element and only contains all elements to return trueC2.retainall (c);//the intersection of the set, A and B two sets, A.retainall (b), the final result is stored in the A collection, and B sets unchanged. Returns a Boolen value indicating whether a collection has changed

Enhanced for Traversal:

 list<string> C = new  arraylist<string>(); C.add (  hello   " ); C.add (  world   " );  //  enhanced for traversal it is better to check if the next collection is NULL, or NULL to give an error  if  (c! = null   for   (String str:c) { System.  out             .println (str); }        }

Iterator traversal:

        New ArrayList ();        C.add ("hello");        C.add ("World");         = c.iterator ();          while (It.hasnext ()) {            = It.next ();   Gets the element and moves to the location of the next element of the collection            System.  out. println (Obj.tostring ());        }

List collection common for traversal:

        New Arraylist<string>();        C.add ("hello");        C.add ("World");          for (int0; i < c.size (); i++) {            = c.Get(i);            System.  out . println (str);        }

Map Common methods:

Map<integer, string> m =NewHashmap<integer, string>(); M.put (2,"Hello");//adds an element that, if the key is the first store, stores the element directly, returns NULL, and if the key is not stored for the first time, the value replaces the previous value and returns the previous value. M.put (3," World"); M.put (4,"Android"); M.clear ();//removes all key-value pairs from the collectionM.remove (2);//deletes the key-value pair element according to the key, and returns the valueM.containskey (3);//Determines whether the collection contains the specified keyM.isempty ();//determines whether the collection is emptyM.size ();//gets the number of collection key-value pairsString value = M.Get(2);//get values by keySet<integer>Set= M.keyset ();//gets the collection of all the keys in the collectioncollection<string> con = m.values ();//gets the collection of all values in the collection

To search for values by key traversal:

Map<integer, string> m =NewHashmap<integer, string>(); M.put (2,"Hello"); M.put (3," World"); M.put (4,"Android"); Set<Integer> key = M.keyset ();//gets the collection of all the keys in the collection         for(intI:key) {String value= M.Get(i);//get key Pair response valueSystem. out. println ("Key:"+ i +"---Value:"+value); }

Find the keys and values for the objects based on their key values:

Map<integer, string> m =NewHashmap<integer, string>(); M.put (2,"Hello"); M.put (3," World"); M.put (4,"Android"); Set<map.entry<integer, string>>Set= M.entryset ();//gets all key-value pairs of object pairs combined//iterate through the collection of key-value pairs of objects, getting each key-value pair         for(Map.entry<integer, string> me:Set) {            intKey = Me.getkey ();//Get keyString value = Me.getvalue ();//Get ValueSystem. out. println ("Key:"+ key +"---Value:"+value); }

4.Collections (tool class for operation under collection)

list<integer> list =NewArraylist<integer>(); List.add (Ten); List.add ( -); List.add ( -); List.add ( -); List.add (Ten); Collections.sort (list);//sort, default by natural sort, requires type implementation comparable interface        intindex = Collections.binarysearch (list, -);//binary Lookup, no return value found:-Maximum index plus 1        intMaxvalue=collections.max (list);//find the maximum value. Collections.synchronizedlist (list);//wraps a non-thread-safe list as thread-safe. 

Java Collection Learning Notes

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.