java interface to the map

One. The difference between collection and map collection

1.collection collection elements exist in isolation, map set budget in pairs (key --vlaue key value)

2.collection single set is set, map the set of double-row set

3.collection duplicate set of elements, the elements may not repeated, map the set of key values ​​can not be repeated, but the value may be repeated

4.Map commonly used set is set HashMap (order no storage), LinkedHashMap set (stored order)

5. Follow hashSet key, can not be repeated, memory disorder. linkedMap, sequential.

two. If the stored key is the custom data types must override key hashCode () method, the equals () methods.

The person object

 1 package com.oracle.demo01;
 2 
 3 public class Person {
 4  private String name;
 5  private Integer age;
 6 public String getName() {
 7     return name;
 8 }
 9 public void setName(String name) {
10     this.name = name;
11 }
12 public Integer getAge() {
13     return age;
14 }
15 public void setAge(Integer age) {
16     this.age = age;
17 }
18 public Person(String name, Integer age) {
19     super();
20     this.name = name;
21     this.age = age;
22 }
23 public Person() {
24     super();
25 }
26 @Override
27 public String toString() {
28     return "Person [name=" + name + ", age=" + age + "]";
29 }
30  @Override
31 public int hashCode() {
32     final int prime = 31;
33     int result = 1;
34     result = prime * result + ((age == null) ? 0 : age.hashCode());
35     result = prime * result + ((name == null) ? 0 : name.hashCode());
36     return result;
37 }
38  @Override
39 public boolean equals(Object obj) {
40     if (this == obj)
41         return true;
42     if (obj == null)
43         return false;
44     if (getClass() != obj.getClass())
45         return false;
46     Person other = (Person) obj;
47     if (age == null) {
48         if (other.age != null)
49             return false;
50     } else if (!age.equals(other.age))
51         return false;
52     if (name == null) {
53         if (other.name != null)
54             return false;
55     } else if (!name.equals(other.name))
56         return false;
57     return true;
58 }
59 }

 

. 1  Package com.oracle.demo01;
 2  
. 3  Import the java.util.HashMap;
 . 4  Import java.util.Set;
 . 5  
. 6  // custom key set type Map 
. 7  public  class Demo04 {
 . 8  public  static  void main (String [] args) {
 . 9      the HashMap <the Person, String> Map = new new the HashMap <the Person, String> ();
 10      // custom object class uses stored values 
. 11      map.put ( new new the Person ( "a", 18) , "Yi" );
 12 is      map.put ( new new the Person ( "two", 18), "er");
13     map.put(new Person("三",18),"san");
14     map.put(new Person("一",18),"wu");
15     //遍历
16     Set<Person> set=map.keySet();
17     for(Person p:set){
18         System.out.println(p+"---"+map.get(p));
19     }
20     //重写hashcode(),equals()   source___  hashcode()and equals()  
21 }
22 }

 

three. Use put remove get method

 1 package com.oracle.demo01;
 2 
 3 import java.util.HashMap;
 4 import java.util.HashSet;
 5 
 6 public class Demo01 {
 7     public static void main(String[] args) {
 8         HashMap<String, String> map = new HashMap<String, String>();
 9         // put存储键值对
10         map.put("yi", "123");
11         map.put("er", "456");
12         map.put("yi", "456");
13         map.put ( "San", "789" );
 14          // delete 
15          System.out.println (map.remove ( "Yi" ));
 16          // Value Value is obtained the same key value, value value overwrites the contents of the foregoing, the absence of the key value, return null 
. 17          System.out.println (as map.get ( "Yi" ));
 18 is      }
 . 19 }

four. map set value by a key value query four methods

keyset + iterator or enhanced for

 1 package com.oracle.demo01;
 2 
 3 import java.util.HashMap;
 4 import java.util.Iterator;
 5 import java.util.Set;
 6 
 7 public class Demo02 {
 8     public static void main(String[] args) {
 9         HashMap<String, String> map = new HashMap<String, String>();
10         // put存储键值对,返回Value值
11         map.put("yi", "123");
12         map.put("er", "456");
13         map.put ( "Yi", "456" );
 14          map.put ( "San", "789" );
 15          // keySet + enhancement for, a key is obtained
 16          // guide packet set, 
. 17          / * 
18 is           * the Set <String> ss = map.keySet () ; // iterate for (String s: ss) { // S corresponding to the key value is obtained
 . 19           * System.out.println (as map.get (S)); // incoming s value value value obtained}
 20 is           * / 
21 is          // keySet iterator +
 22          // get set set single set 
23 is          the set <String> S1 = map.keySet ();
 24          // create iterators 
25          the iterator <String> S2 = s1.iterator ();
26         //Traversing 
27          the while (s2.hasNext ()) {
 28              // in this case two Next ()
 29              // System.out.println (ssss.next () + "," + as map.get (ssss.next () ));
 30              // procedure preferably occurs once Next (). 
31 is              String A = s2.next ();
 32              System.out.println ( "key is the key" + a + ", value is" + Map .get (A));
 33 is          }
 34 is  
35      }
 36  
37 [ }

enteyset + or enhanced for loop iterator ()

1. First key object obtained map.entry <E, E> 2. reuse getkey / getvalue

. 1  Package com.oracle.demo01;
 2  
. 3  Import the java.util.HashMap;
 . 4  Import the java.util.Iterator;
 . 5  Import a java.util.Map;
 . 6  Import java.util.Set;
 . 7  
. 8  public  class Demo03 {
 . 9      public  static  void main (String [] args) {
 10          // inner class called internal class name external name class. methods / properties or create an object
 . 11          // the Entry of the set of key packages called objects (overall) the Entry <E, E>,
 12 is          // then getKey and GetValue, obtain key derived from the value 
13 is          the HashMap <String, String> Map = new newThe HashMap <String, String> ();
 14          // PUT store the key value pairs returned Value is 
15          map.put ( "Yi", "123" );
 16          map.put ( "ER", "456" );
 . 17          map.put ( "Yi", "456" );
 18 is          map.put ( "San", "789" );
 . 19          // entry + enhancement for
 20 is          // obtained Map.Entry <String, String> set collection of objects
 21          // generic data types may be, objects, collections 
22 is          the set <of Map.Entry <String, String >> sET = EnumMap.entrySet ();
 23 is          / * for (of Map.Entry <String, String> S:
set) {// set to obtain a set of objects each of 24              String s.getKey S1 = ();
25              String S2 = s.getValue ();
 26 is              System.out.println (S1 + "---" + S2);
 27          } * / 
28          // Get iterator 
29          the Iterator <of Map.Entry <String, String >> I = set.iterator ();
 30      the while (i.hasNext ()) {
 31 is          // get every entry object 
32          of Map.Entry <String, String> = entry i.next ();
 33 is          String S1 = entry.getKey ( );
 34 is          String S2 = entry.getValue ();
 35          System.out.println (entry); // called by default in entry toString () method 
36         System.out.println(s1+"/"+s2);
37     }
38     }
39 }

Guess you like

Origin www.cnblogs.com/mlf19920916/p/12147263.html