Note the use of the Map interface

1, Map interface object itself can not be used directly iteration output. Since each position map is stored in a pair of values.

The iterator can only find a value. If you must iteration output, using the following steps. :

However, Map interfaces use only as a look, after all, belong to a small number of output operations .

Copy the code
package clusters;
 Import the java.util.HashMap;
 Import the java.util.Iterator;
 Import a java.util.Map;
 Import java.util.Set;
 Import java.util.WeakHashMap;
 public  class test1 {
     public  static  void main (String args []) { 
        Map <String, String> Map = null ; // declare Map object, wherein the key and value of type String 
        Map = new new the HashMap <String, String> (); 
        map.put ( "MLDN", "WWW .mldn.cn ");     // add content
        map.put("zhinangtuan","www.zhinangtuan.net.cn") ;    // 增加内容
        map.put("mldnjava","www.mldnjava.cn") ;    // 增加内容
        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()) ;
        }
    }
};
Copy the code

Output:

mldn --> www.mldn.cn
zhinangtuan --> www.zhinangtuan.net.cn
mldnjava --> www.mldnjava.cn

This output is output in the final form of collection, only to Map.Entry type as the operation content .

 

Map of the type and value of the key can also be used non-system class!

Copy the code
package 类集;
import java.util.HashMap;
import java.util.Map;
class Person{
    private String name ;
    private int age ;
    public Person(String name,int age){
        this.name = name ;
        this.age = age ;
    }
    public String toString(){
        return "姓名:" + this.name + ";年龄:" + this.age ;
    }
};
public class test1{
    public static void main(String args[]){
        Map<String,Person> map = null ;
        map = new HashMap<String,Person>() ;
        map.put("zhangsan",new Person("张三",30));    // 增加内容
        System.out.println(map.get("zhangsan")) ;      //getKey()方法
    }
};
Copy the code

Output:

Name: Joe Smith; Age: 30

 

However, if non-system class as the type of key it?

Copy the code
package 类集;
import java.util.HashMap;
import java.util.Map;
class Person{
    private String name ;
    private int age ;
    public Person(String name,int age){
        this.name = name ;
        this.age = age ;
    }
    public String toString(){
        return "姓名:" + this.name + ";年龄:" + this.age ;
    }
};
public class test1{
    public static void main(String args[]){
        Map<Person,String> map = null ;
        map = new HashMap<Person,String>() ;
        map.put(new Person("张三",30),"zhangsan");    // 增加内容
        System.out.println(map.get(new Person("张三",30))) ;
    }
};
Copy the code

Output:

null

At this time, only when the custom class as a key, returns a value of null . So why can it as a value of type time?

For this matching process, there is a feature that, subject to the same content can check out .

Put the object above method and get inside the new Person () method to get anonymous is not the same .

 

Example:

Copy the code
package 类集;
import java.util.HashMap;
import java.util.Map;
class Person{
    private String name ;
    private int age ;
    public Person(String name,int age){
        this.name = name ;
        this.age = age ;
    }
    public String toString(){
        return "姓名:" + this.name + ";年龄:" + this.age ;
    }
};
public class test1{
    public static void main(String args[]){
        Map<Person,String> map = null ;
        map = new HashMap<Person,String>() ;
        Person per = new Person("张三",30) ;
        map.put(per,"zhangsan");    // 增加内容
        System.out.println(map.get(per)) ;
    }
};
Copy the code

Output:

zhangsan

But this is not the best solution, because it is not per person and objects with a walk, should be like String, you can use an anonymous form of objects to find content.

Then the time required in accordance with the judgment of repeating elements form Set interface as overwrite method .

 

To use as a non-system-based Key, then such must override the following two methods Object class:

hashCode()

equals()

Copy the code
package 类集;
import java.util.HashMap;
import java.util.Map;
class Person{
    private String name ;
    private int age ;
    public Person(String name,int age){
        this.name = name ;
        this.age = age ;
    }
    public String toString(){
        return "姓名:" + this.name + ";年龄:" + this.age ;
    }
    public boolean equals(Object obj){        //覆写equals()
        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(){                //覆写hashCode()
        return this.name.hashCode() * this.age ;  //自定义hashCode。
    }
};
public class test1{
    public static void main(String args[]){
        Map<Person,String> map = null ;
        map = new HashMap<Person,String>() ;
        map.put(new Person("张三",30),"zhangsan");    // 增加内容
        System.out.println(map.get(new Person("张三",30))) ;
    }
};
Copy the code

Output:

zhangsan

We found that the object can be removed at this time using anonymous content. As the object when in fact rely hashCode () and equals () to determine whether an object is equal to that inside the system automatically.

 

Summary: Map can use an iterative output:

      map->entrySet->Set->Iterator->Map.Entry->key和value。

  Non-system classes as a key, the guarantee must override equals () and hashCode () method, otherwise invalid.

Original Address: https: //www.cnblogs.com/alsf/p/6230956.html

1, Map interface object itself can not be used directly iteration output. Since each position map is stored in a pair of values.

The iterator can only find a value. If you must iteration output, using the following steps. :

However, Map interfaces use only as a look, after all, belong to a small number of output operations .

Copy the code
package 类集;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
public class test1{
    public static void main(String args[]){
        Map<String,String> map = null; // 声明Map对象,其中key和value的类型为String
        map = new HashMap<String,String>() ;
        map.put("mldn","www.mldn.cn") ;    // 增加内容
        map.put("zhinangtuan","www.zhinangtuan.net.cn") ;    // 增加内容
        map.put("mldnjava","www.mldnjava.cn") ;    // 增加内容
        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()) ;
        }
    }
};
Copy the code

输出结果:

mldn --> www.mldn.cn
zhinangtuan --> www.zhinangtuan.net.cn
mldnjava --> www.mldnjava.cn

这种输出方式最终还是以collection形式输出,只是以Map.Entry作为内容的操作类型

 

Map的key和value的类型也可以用非系统类!

Copy the code
package 类集;
import java.util.HashMap;
import java.util.Map;
class Person{
    private String name ;
    private int age ;
    public Person(String name,int age){
        this.name = name ;
        this.age = age ;
    }
    public String toString(){
        return "姓名:" + this.name + ";年龄:" + this.age ;
    }
};
public class test1{
    public static void main(String args[]){
        Map<String,Person> map = null ;
        map = new HashMap<String,Person>() ;
        map.put("zhangsan",new Person("张三",30));    // 增加内容
        System.out.println(map.get("zhangsan")) ;      //getKey()方法
    }
};
Copy the code

输出结果:

姓名:张三;年龄:30

 

但是如果非系统类作为key的类型呢?

Copy the code
package 类集;
import java.util.HashMap;
import java.util.Map;
class Person{
    private String name ;
    private int age ;
    public Person(String name,int age){
        this.name = name ;
        this.age = age ;
    }
    public String toString(){
        return "姓名:" + this.name + ";年龄:" + this.age ;
    }
};
public class test1{
    public static void main(String args[]){
        Map<Person,String> map = null ;
        map = new HashMap<Person,String>() ;
        map.put(new Person("张三",30),"zhangsan");    // 增加内容
        System.out.println(map.get(new Person("张三",30))) ;
    }
};
Copy the code

输出结果:

null

此时只是将自定义类作为key的时候,返回结果为空。那么为什么作为value的类型时候可以呢?

对于这种匹配过程,有一个特点,即,对象要一样才可以将内容查询出来

以上的Put和get方法里面的new Person()匿名方法得到的对象是不一样的

 

实例:

Copy the code
package 类集;
import java.util.HashMap;
import java.util.Map;
class Person{
    private String name ;
    private int age ;
    public Person(String name,int age){
        this.name = name ;
        this.age = age ;
    }
    public String toString(){
        return "姓名:" + this.name + ";年龄:" + this.age ;
    }
};
public class test1{
    public static void main(String args[]){
        Map<Person,String> map = null ;
        map = new HashMap<Person,String>() ;
        Person per = new Person("张三",30) ;
        map.put(per,"zhangsan");    // 增加内容
        System.out.println(map.get(per)) ;
    }
};
Copy the code

输出结果:

zhangsan

可是这不是解决方法的最好,因为不能 将person和per对象带着走,应该像String一样,可以使用匿名对象形式找到内容。

那么此时需要按照与Set接口中判断重复元素的形式一样,进行方法的覆写

 

如果要使用非系统类作为Key,则此类必须覆写Object类的以下两个方法:

hashCode()

equals()

Copy the code
package 类集;
import java.util.HashMap;
import java.util.Map;
class Person{
    private String name ;
    private int age ;
    public Person(String name,int age){
        this.name = name ;
        this.age = age ;
    }
    public String toString(){
        return "姓名:" + this.name + ";年龄:" + this.age ;
    }
    public boolean equals(Object obj){        //覆写equals()
        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(){                //覆写hashCode()
        return this.name.hashCode() * this.age ;  //自定义hashCode。
    }
};
public class test1{
    public static void main(String args[]){
        Map<Person,String> map = null ;
        map = new HashMap<Person,String>() ;
        map.put(new Person("张三",30),"zhangsan");    // 增加内容
        System.out.println(map.get(new Person("张三",30))) ;
    }
};
Copy the code

输出结果:

zhangsan

发现此时使用匿名对象能够取出内容。作为对象的时候,实际上是依靠hashCode()和equals()来判断对象是否相等,这一点由系统内部自动完成。

 

总结:Map可以使用迭代输出:

      map->entrySet->Set->Iterator->Map.Entry->key和value。

  Non-system classes as a key, the guarantee must override equals () and hashCode () method, otherwise invalid.

Original Address: https: //www.cnblogs.com/alsf/p/6230956.html

Guess you like

Origin www.cnblogs.com/jpfss/p/11124715.html
Recommended