Map details and example code in java

  • 2020-06-19 10:24:46
  • OfStack

Map interface

Map is similar to y(x)=x; Such a function (key for x, value for y)

Map exists side by side with Collection. Used to hold data with mapping relationships: ES16en-ES17en

Both key and value in Map can be data of any reference type

key in Map is stored with Set and no duplication is allowed, i.e. the class corresponding to 1 Map object must override hashCode() and equals() methods.

The String class is commonly used as the "key" for Map.

There is a one-way one-to-one relationship between key and value, that is, only one and certain value can always be found through the specified key.

Map interface


 丨 ---------HashMap : Map The main implementation class 


 丨 ---------TreeMap : As per add Map Element of key Is required to: key Must be with 1 A class of objects! for key : Natural ordering  vs  Custom sorting 

 丨 ---------Hashtable: is 1 An old implementation class, thread-safe, and HashMap It's different. It's not allowed null As a key and value (Not recommended) ( How old! Look at the naming, you don't even use the hump naming! Ha ha ha ha ha! )

 丨 ---------Properties: Often used to deal with properties files. Both the key and the value are zero String Type. (Here is the configuration file you usually use.) 


public class TestMap{

/*
 *
 *Object put(Object key,Object value): to Map add 1 An element 
 *
 *Object remove(Object key) : As specified key Remove this key-value right 
 *
 *void putAll(Map t) : 
 *
 *void clear() Empty: 
 *
 *Object get(Object key) : Get the specified key the value value , Without this key It returns null
 *
 *boolean containsKey(Object key):
 *
 *boolean containsValue(Object value)
 *
 *int size()
 *   
 *boolean isEmpty()
 *
 *boolean equals(Object obj)
 *
 *HashMap : 
 *1.key Is to use Set To store, not to be repeated, value Is to use Collection For storage, repeatable 
 * 1 a key-value Yes, it's 1 a Entry And all the Entry Is to use Set Stored, is also not repeatable. 
 *2. to HashMap Is called when an element is added to key In which part of the class equals () Method, judge two key Is it the same? If it is the same, it can only be added 1 a , You can only add the element that follows 
 *
 */

// two put the key Again, the output is the one that was added, so the output is value=87

  public void test1(){

    Map map = new HashMap();
    map.put("AA",213);
    map.put("BB",456);
    map.put("BB",45);// With the above key So the same map Is this 
    map.put(123,"CC");
    map.put(null,null);
    map.put(new Person("xx",21),89);
    map.put(new Person("xx",21),87);// With the above key So the same map Is this 
    System.out.println(map.size());
    map.remove("BB");
    System.out.println(map);
    Object value = map.get(123);
    System.out.println(value);

  }
  /*

    How to traverse the Map
   Set keySet (a) 
   Collection values()
   Set entrySet()

  */

  @Test
  public void test2(){

    Map map = new HashMap();
    map.put("AA",213);
    map.put("BB",45);
    map.put(123,"CC");
    map.put(null,null);
    map.put(new Person("xx",21),89);


    //1. traverse key set . At this point print out  null,AA,Person    //[name=DD,age=23],BB,123
    Set set = map.keySet();
    for(Object obj : set){
      System.out.println(obj);
    }
    //2. traverse value set . At this point print out null,213,89,45,CC
    Collection values = map.values();
    Iterator i = values.iterator();
    while(i.hasNext()){
      System.out.println(i.next());
    }
    //3. How to traverse the key-value right 
    // way 1
    Set set1 = map.keySet();
    for(Object obj1 : set1){
      System.out.println(obj + "----->" + map.get(obj));

    }
    // way 2
    Set set2 = map.entrySet();
    for(Object obj : set2 ){
      Map.Entry entry = (Map.Entry)obj;
      System.out.println(entry.getKey() + "---->" + entry.getValue());
    }
  }


  @Test
  public void testLinkedHashMap(){

    Map map = new LinkedHashMap();
    map.put("AA",213);
    map.put("BB",45);
    map.put(123,"CC");
    map.put(null,null);
    map.put(new Person("xx",21),89);

    Set set1 = map.keySet();
    for(Object obj1 : set1){
      System.out.println(obj + "----->" + map.get(obj));
    }
  }

  @Test
  public void testTreeMap(){


    // Natural ordering 
    Map map = new TreeMap();
    map.put(new Person("AA",23),89);
    map.put(new Person("MM",22),79);
    map.put(new Person("BB",23),99);
    map.put(new Person("CC",13),69);

    Set set1 = map.keySet();
    for(Object obj1 : set1){
      System.out.println(obj + "----->" + map.get(obj));
    }
  }


  @Test
  public void testProperties(){

    Properties pros = new Properties();
    pros.load(new FileInputStream(new File(jdbc.properties )));
    String user = pros.getProperty("user");
    String password = pros.getProperty("password");

  }
}

< The following highlights: >


/*

   operation Collection As well as Map Tools: Collections

   Interview question: Distinguish Collection and Collections

  reverse(List): reverse List The location of the element 
  shuffle(List): right List The collection elements are randomly sorted 
  sort(List):
  sort(List,Comparator):
  swap(List,int,int):

 

 */

 

List list = new ArrayList();
list.add(123);
list.add(23);
list.add(33);
list.add(43);
list.add(53);

List list2 = new ArraysList();// If use list2 when src To copy list The set will report an error, and the array will be out of bounds, assuming that the newly defined set length is 0 And the source set length is 5 It won't fit in 

List list1 = Arrays.asList(new Object[list.size()]);// Take an array as list Direct fetch of length 

Collections.copy(list1,list);// Invoking a utility class does not return a value 


----------------- Synchronization control 

synchronized () method 

  // This is guaranteed in the following ways list Thread safety. High security but low efficiency! 

  List list2 = Collections.synchronizedList(list);

  System.out.println(list2);

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: