Review of the Java collection

Source: Internet
Author: User
Tags set set

Since the exams are approaching, it is not familiar to feel the Java collection, but in practice, the collections in Java are very common. So spend a few days reviewing the collections in Java. First, according to a set of understanding of the order of ideas.

Some collection classes are available in the Java.util package, often with List,set,map. Where list and set implements the collection parent class interface. Map is a collection of mappings for key-value pairs.

The common method in the list interface is the ArrayList class method, which is convenient for querying, but is slow to insert and delete. Each time it is inserted or deleted, it will affect the object behind the operation, and the final completion will be very inefficient. In contrast, the subsequent LinkedList class method is easy to insert and delete, but the efficiency of random access is very low.

For example, ArrayList class, convenient query:

1  PackageJihe;2 3 Importjava.awt.List;4 Importjava.util.ArrayList;5 ImportJava.util.Iterator;6 7  Public classStudy {8      Public Static voidMain (string[] args) {9ArrayList Al =NewArrayList ();//Create a new ArrayListTen          for(inti=0; i<10; i++ ) { OneAl.add (NewInteger (i));//Add Items to the array list A             } -          for(inti=0; I<al.size (); i++ ) { -SYSTEM.OUT.PRINTLN (i + "=" +Al.get (i)); the             } -Al.remove (5 ); -Al.set (5,NewInteger (66 ) ); -          for(Iterator i=al.iterator (); I.hasnext ();) { +Integer integer =(Integer) I.next (); - System.out.println (integer); +             } A         } at}

Then look at the LinkedList class, which makes it easy to insert and delete elements into the collection:

1  PackageJihe;2 3 Importjava.util.LinkedList;4 5  Public classExample {6 7      Public Static voidMain (string[] args) {8String a = "a", B = "B", C = "C", test = "Test";9linkedlist<string> list =NewLinkedlist<string>();TenList.add (a);//index position is 0 OneList.add (b);//index position is 1 AList.add (c);//index position is 2 -System.out.println (List.getfirst ());//gets and outputs the object at the beginning of the list -List.addfirst (test);//add an object to the beginning of the list theSystem.out.println (List.getfirst ());//gets and outputs the object at the beginning of the list -List.removefirst ();//remove an object from the beginning of the list -System.out.println (List.getfirst ());//gets and outputs the object at the beginning of the list -     } +}
View Code

Operation Result:

Because the set collection is unique, the advantage of a set set implemented by the HashSet class is the ability to quickly locate elements in the collection. The HashSet class needs to re-implement the Equals () method and the Hashcode () method.

The map collection is a mapping type, with each object being paired. Each object has key and value, which gets the value by key.

The common implementation classes of the map interface are HashMap and Treemap,hashmap, which are quickly searched by the hash code for their internal mapping relationships, and treemap are in a certain order to implement comparable interface. If you traverse unordered, it is more efficient to delete and add with HashMap, otherwise the treemap effect is better.

The Map collection allows the value object to be null, with no number limits, only one in the set, and cannot be duplicated. When the Get () method returns a value of NULL, there are two cases: value is null; The ContainsKey () method is used to determine whether a key exists.

Use of the HashMap class:

Implementing the Map collection using HASHMAP requires rewriting the Hashcode () method, which has two principles:

1. Not unique principle: do not give each object to generate a unique hash code, as long as the hash code generated through the Hashcode () method can take advantage of the Get () method to get and put () method to add the mapping relationship can be;

2. Dispersion principle: decentralized.

1  PackageJihe;2 3 Importjava.util.Collection;4 ImportJava.util.HashMap;5 ImportJava.util.Iterator;6 Importjava.util.LinkedList;7 ImportJava.util.Map;8 ImportJava.util.Set;9 Ten  Public classExample { One  A      Public Static voidMain (string[] args) { -Map<string,string> map=NewHashmap<string,string>(); -Map.put ("1", "Monday");//Add theMap.put ("2", "Tuesday"); -Map.put ("3", "Wensday"); -  -Set KeySet =Map.keyset (); +Collection Valuecol =map.values (); -Set x=Map.entryset (); +Iterator i =keyset.iterator (); A          while(I.hasnext ()) { at System.out.println (I.next ()); -         } -I=valuecol.iterator (); -          while(I.hasnext ()) { - System.out.println (I.next ()); -         } inI=x.iterator (); -          while(I.hasnext ()) { toMap.entry en=(Map.entry) I.next (); +String key=(String) En.getkey (); -String value=(String) en.getvalue (); theSystem.out.println (key+ "" +value); *         } $     }Panax Notoginseng}

TreeMap performance is worse than HashMap in adding, deleting, and locating mapping relationships. Applies to ordered collections.

The above is a preliminary understanding of the map collection, I will continue to supplement the actual use of code.

Review of the Java collection

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.