Java notes: Java class set Summary (ii, map interface and related)

Source: Internet
Author: User

The map interface differs from the collection interface in that the map interface holds objects that are pairs, similar to Key-value. Common subclasses of the map interface:

Take HashMap as an example using map:
Import java.util.HashMap; import java.util.Map;p ublic class hashmapdemo01{public static void Main (String args[]) {Map <String,String> map = null; Declares the map object, where key and value are of type Stringmap = new hashmap<string,string> (); Map.put ("Mldn", "www.mldn.cn");// Add Content Map.put ("Zhinangtuan", "www.zhinangtuan.net.cn");//Add Content Map.put ("Mldnjava", "www.mldnjava.cn");//Add content string val = Map.get ("mldn");//The value is fetched from key System.out.println ("Out of the content is:" + val);}};

The difference between HashMap and Hashtable:

The output of the map interface does not directly use the iterative (Iterator,foreach) output for the map interface, because a pair of values is stored in the map, and if the iteration output is to be used at this point, the following steps must be followed: 1. Instance of map EntrySet () The method becomes the set interface object; 2. Instantiate to iterator by the set interface 3. With iterator iteration output, each content is Map.entry object 4. Key-value Separation by Map.entry
Import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.Iterator;p ublic class Iteratordemo04{public static void Main (String args[]) {map<string,string> map = NULL;//Declare Map object, Where key and value are of type Stringmap = new hashmap<string,string> (), Map.put ("Mldn", "www.mldn.cn");//Add Content Map.put (" Zhinangtuan "," www.zhinangtuan.net.cn ");//Add Content Map.put (" Mldnjava "," www.mldnjava.cn ");//Add Content set<map.entry< String,string>> allset = null; Allset = Map.entryset ();iterator<map.entry<string,string>> iter = null ; iter = Allset.iterator (); while (Iter.hasnext ()) {map.entry<string,string> me = Iter.next (); System.out.println (Me.getkey () +--+ me.getvalue ());}};


Identityhashmap class when using HashMap, the content of key can not be duplicated, meaning the same key is the same as the storage address, if you want to reuse, you want to use the Identityhashmap class.
Import java.util.IdentityHashMap; import java.util.Set; import java.util.Iterator; import Java.util.Map; Class person{ private string name;p rivate int;p ublic person (String Name,int age) {this.name = name; this.age = age;} public boolean equals (Object obj) {if (this==obj) {return true;} if (! ( obj instanceof person) {return false;} Person P = (person) obj; if (this.name.equals (p.name) &&this.age==p.age) {return true;} Else{return false;}} public int hashcode () {return This.name.hashCode () * this.age;} Public String toString () {return "name:" + THIS.name + ", Age:" + This.age;}}; public class Identityhashmapdemo02{public static void Main (String args[]) {map<person,string> map = NULL;//Declare Map object Map = new identityhashmap<person,string> (), Map.put (New person ("Zhang San", "+"), "zhangsan_1");//Add content map.put (new person ("Zhang San"), "zhangsan_2");//Add content Map.put (new person ("John Doe", "Lisi");//Add Content set<map.entry<person,string> > allset = NULL;//prepare to use set to receive all content Allset = Map.entryset (); IteraTor<map.entry<person,string>> iter = null; iter = Allset.iterator (); while (Iter.hasnext ()) {map.entry< person,string> me = Iter.next (); System.out.println (Me.getkey () +--+ me.getvalue ());}};

SortedMap interface This interface can be used to sort. Instance:
Import java.util.Map; import java.util.SortedMap; import java.util.TreeMap;p ublic class Sortedmapdemo{public static void Main (String args[]) {sortedmap<string,string> map = null; map = new treemap<string,string> ();//Sub-class instantiation Port Object Map.put ("D, Jiangker", "http://www.jiangker.com/"), Map.put ("A, Mldn", "www.mldn.cn"), Map.put ("C, Zhinangtuan", " Www.zhinangtuan.net.cn "); Map.put (" B, Mldnjava "," www.mldnjava.cn "); System.out.print ("Key for the content of the first element:" + Map.firstkey ()); System.out.println (": Corresponding value:" + map.get (Map.firstkey ())); System.out.print ("Key of the last element's content:" + map.lastkey ()); System.out.println (": Corresponding value:" + map.get (Map.lastkey ())); System.out.println ("Returns a collection less than the specified range:"); for (map.entry<string,string> Me:map.headMap ("B, Mldnjava"). EntrySet ()) { System.out.println ("\t|-" + me.getkey () + "-" + me.getvalue ()); System.out.println ("Returns a collection greater than the specified range:"); for (map.entry<string,string> Me:map.tailMap ("B, Mldnjava"). EntrySet ()) { System.out.println ("\t|-" + me.getkey () + "--" + me.getvalue ());}system.out.println ("Partial collection:"); for (map.entry<string,string> Me:map.subMap ("A, Mldn", "C, Zhinangtuan"). EntrySet ()) {System.out.println ("\t|-" + me.getkey () + "--" + Me.getvalue ())}};

Stack class stack is the operation of the Stack class, the use of the stack is not much to say, LIFO. The Stack class is a subclass of the vector class. Instance:
Import Java.util.Stack;p ublic class stackdemo{public static void Main (String args[]) {stack<string> s = new stack< ; String> (); S.push ("A");//Into the stack s.push ("B");//Into the stack s.push ("C");//Into the stack System.out.print (s.pop () + ","); System.out.print (S.pop () + ","); System.out.println (S.pop () + ","); System.out.println (S.pop ());};

The Properties Class property is a common form of development, and the properties class is a class of operations that are specifically designed to complete the property. Instance:
Import Java.util.properties;public class Propertiesdemo01{public static void Main (String args[]) {Properties Pro = new Pro Perties ();//Create Properties Object Pro.setproperty ("BJ", "Beijing");//Set attribute Pro.setproperty ("TJ", "Tianjin"); Pro.setproperty ("NJ", "Nanjing"); System.out.println ("1, BJ Attribute exists:" + pro.getproperty ("BJ")); System.out.println ("2, SC attribute does not exist:" + pro.getproperty ("SC")); System.out.println ("3, the SC attribute does not exist, and the default value of the display is set:" + Pro.getproperty ("SC", "no Discovery");}};







Java notes: Java class set Summary (ii, map interface and related)

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.