Java Collections and Mapping tables

Source: Internet
Author: User

Collection

Collections can be created using the collection's three concrete classes HashSet, Linkedhashset, TreeSet

HashSet class

Load factor

When the number of elements exceeds the product of the capacity and load factor, the capacity is automatically doubled

The HashSet class can be used to store any element that is not equal. Considering the efficiency factor, the objects added to the hash set must implement the Hashcode method in a way that correctly disperses the hash code.

If two objects are equal, the hash code for the two objects must be the same. Two unequal objects may have the same hash code

Inherit the collection interface, so all the methods in the collection can be used

Example:

Linkedhashset class

Linkedhashset extends the HashSet class with a list implementation that supports ordering elements within the collection

Elements in HashSet are not sorted, while elements in Linkedhashset can be extracted in the order in which they are inserted into the collection.

Example:

Linkedhashset preserves the order in which elements are inserted.

TreeSet class

Example:

 Public classTexttreeset { Public Static voidmain (String [] args) {Set<String> set =NewHashset<string>(); Set.add ("London"); Set.add ("Paris"); Set.add ("New York"); Set.add ("Zon"); TreeSet<String> TreeSet =NewTreeset<string>(set); System.out.println ("Sorted Tree set:" +TreeSet); System.out.println ("First:" +Treeset.first ()); System.out.println ("Last:" +treeset.last ()); System.out.println (Treeset.headset ("New York");//output data prior to new YorkSystem.out.println (Treeset.tailset ("New York");//data after the output of New YorkSystem.out.println ("Less than:" + treeset.lower ("Paris"));//returns a less than the given elementSYSTEM.OUT.PRINTLN ("Greater than:" + treeset.higher ("New York"));//returns a value greater than the given elementSystem.out.println ("Less than or equal to:" + Treeset.floor ("P"));//returns a less than or equal to the given elementSYSTEM.OUT.PRINTLN ("greater than or equal to:" + treeset.ceiling ("P"));//returns a value greater than or equal to the given elementSystem.out.println ("Pollfirst:" +Treeset.pollfirst ()); System.out.println ("Polllast:" +treeset.polllast ()); System.out.println ("New Tree set:" +TreeSet); }}

Example:

What's the difference between Hashset,linkedhashset and TreeSet?

HashSet is implemented using a hash table, and the elements are unordered. Add, delete operation time complexity is O (1). TreeSet internal structure is a tree structure (red black tree), elements are ordered, add, delete operation time complexity is O (log (n)), and provides the first (), Last (), HeadSet (), Tailset () and other methods to handle an ordered set. Linkedhashset is between HashSet and TreeSet, the interior is a doubly linked list structure, so its insertion is orderly and the time complexity is O (1).

Mapping table

The update methods include clear, put, putall, and remove, and the method clear () Removes all entries from the mapping table.

Method Put (K,V) adds an entry for the specified key and value in the mapping table, if the mapping table originally contains an entry for the key, the original value will be replaced by the new value, and the original value associated with the key is returned

Query methods include ContainsKey, Containsvalue, IsEmpty, and size.

The Linkedhashmap class is implemented with a linked list to extend the HashMap class, and he supports the ordering of entries in the mapping table. HashMap have no order, and linkedhashmap, elements can be sorted sequentially in the order in which they were inserted, or in the order they were last accessed, from the earliest to the latest.

The TreeMap class is efficient when traversing a sorted key. Keys can be sorted by using the comparable interface or the comparator interface

Example:

Linkedhashmap if the order is sorted by access, then the access is placed at the end of the mapping table

Usage of three mapping tables

Examples of statistical word occurrences:

 Public classCountoccurentofwords { Public Static voidmain (string [] args) {string text= "Good morning. Have a good class. Have a good visits. Have fun!. "; Map<string, integer> map =NewTreemap<>(); String [] Words= Text.split ("[\n\t\r.,;:?! (){}]");  for(inti=0; i<words.length; i++) {String key=words[i].tolowercase (); if(Key.length () > 0) {                if(!Map.containskey (Key)) {Map.put (Key,1); }Else {                    intValue =Map.get (key); Value++;                Map.put (key, value); } }} Set<map.entry<string, integer>> wordset =Map.entryset ();//Call the EntrySet () method to return a set set for(Map.entry<string, integer>S:wordset) {System.out.println (S.getkey ()+ "  " +S.getvalue ()); }    }}

Single-element and immutable collections and mapping tables

Java Collections and Mapping tables

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.