java进阶05 集合类HashMap

  关于Vector和Stack其实用法也差不到哪去。先重点介绍下HashMap之后在说下这些集合类的区别

  HashMap

  hashmap了解过的都知道它是以键值对的形式储存数据的,而其中的键key,是不允许重复的

  它能唯一标识某一事物的值,是为了区分不同事物而单独添加的属性,一般不要用事物本身的属性来做这个Key

  它类似于数据库中的主键。

  

package ArrayList;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class HashMap1 {
    public static void main(String[] args){
        HashMap<String,Student> hash=new HashMap<String, Student>();
        
        Student s1=new Student("Joker", "boy", "25");
        Student s2=new Student("Alice", "girl", "18");
        Student s3=new Student("Rem", "boy", "21");
        
        hash.put("1", s1);
        hash.put("2", s2);
        hash.put("3", s3);
        
        
        if(hash.containsKey("3")){//如果存在这个key
            System.out.println("姓名->"+hash.get("3").getName());
            System.out.println("性别->"+hash.get("3").getSex());
            System.out.println("年龄->"+hash.get("3").getAge());
        }
        
    
    }
}

输出结果:

姓名->Rem
性别->boy
年龄->21

如果在插入的键相同呢?来看下

package ArrayList;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class HashMap1 {
    public static void main(String[] args){
        HashMap<String,Student> hash=new HashMap<String, Student>();
        
        Student s1=new Student("Joker", "boy", "25");
        Student s2=new Student("Alice", "girl", "18");
        Student s3=new Student("Rem", "boy", "21");
        
        hash.put("1", s1);
        hash.put("2", s2);
        hash.put("3", s3);
        
        hash.put("3", s1);//覆盖了上一个hash的key为3的值
        if(hash.containsKey("3")){//如果存在这个key
            System.out.println("姓名->"+hash.get("3").getName());
            System.out.println("性别->"+hash.get("3").getSex());
            System.out.println("年龄->"+hash.get("3").getAge());
        }
        
    
    }
}

输出结果:

姓名->Joker
性别->boy
年龄->25

看见没? 它就覆盖了上一个键为3的数据。这个可以防止数据的重插。

如何遍历HashMap呢?

package ArrayList;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class HashMap1 {
    public static void main(String[] args){
        HashMap<String,Student> hash=new HashMap<String, Student>();
        
        Student s1=new Student("Joker", "boy", "25");
        Student s2=new Student("Alice", "girl", "18");
        Student s3=new Student("Rem", "boy", "21");
        
        hash.put("1", s1);
        hash.put("2", s2);
        hash.put("3", s3);
        
        //如果要遍历 这种遍历的效率是很低的
        Iterator<String> it=hash.keySet().iterator();
        while(it.hasNext()){
            String key=it.next();
            Student stu=hash.get(key);
            System.out.println("姓名->"+stu.getName());
            System.out.println("性别->"+stu.getSex());
            System.out.println("年龄->"+stu.getAge());
            System.out.println();
            
             /* 输出时逆序输出
             * /
        }
        
    }
}

输出结果:

键->3
姓名->Joker
性别->boy
年龄->25

键->2
姓名->Alice
性别->girl
年龄->18

键->1
姓名->Joker
性别->boy
年龄->25

这里输出的结果与插入的顺序是相反的。上面是获取键从而来获取值,它的效率是很低的。通常我们用另一种方法来遍历

package ArrayList;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class HashMap1 {
    public static void main(String[] args){
        HashMap<String,Student> hash=new HashMap<String, Student>();
        
        Student s1=new Student("Joker", "boy", "25");
        Student s2=new Student("Alice", "girl", "18");
        Student s3=new Student("Rem", "boy", "21");
        
        hash.put("1", s1);
        hash.put("2", s2);
        hash.put("3", s3);        

        //这种遍历的效率很高 推荐使用
        Iterator<Entry<String, Student>> ite=hash.entrySet().iterator();
        while(ite.hasNext()){
            Entry<String, Student> en=ite.next();
            String key=en.getKey();
            Student stu=en.getValue();
            System.out.println("键->"+key);
            System.out.println("姓名->"+stu.getName());
            System.out.println("性别->"+stu.getSex());
            System.out.println("年龄->"+stu.getAge());
            System.out.println();
        }
    }
}

这里用一个Entry对象来获取一对键值对,从而达到高效的遍历。

如果你想与插入顺序相同的输出结果

可以用另一个类 LinkedHashMap

package ArrayList;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

public class LinkedHashMap1 {
    public static void main(String[] args){
        LinkedHashMap<String, Student> linkedhash=new LinkedHashMap<String, Student>();
        
        Student s1=new Student("Joker", "boy", "25");
        Student s2=new Student("Alice", "girl", "18");
        Student s3=new Student("Rem", "boy", "21");
        
        linkedhash.put("1", s1);
        linkedhash.put("2", s2);
        linkedhash.put("3", s3);
        
        //遍历
        Iterator<Entry<String, Student>> it=linkedhash.entrySet().iterator();
        while(it.hasNext()){
            Entry<String, Student> en=it.next();
            String key=en.getKey();
            Student stu=en.getValue();
            System.out.println("键->"+key);
            System.out.println("姓名->"+stu.getName());
            System.out.println("性别->"+stu.getSex());
            System.out.println("年龄->"+stu.getAge());
            System.out.println();
        }
    }
}

输出结果:

键->1
姓名->Joker
性别->boy
年龄->25

键->2
姓名->Alice
性别->girl
年龄->18

键->3
姓名->Rem
性别->boy
年龄->21

如果只遍历key或者Value的话 ,可以用Collection来操作

package ArrayList;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

public class HashMapCollection {
    public static void main(String[] args){
HashMap<String,Student> hash=new HashMap<String, Student>();
        
        Student s1=new Student("Joker", "boy", "25");
        Student s2=new Student("Alice", "girl", "18");
        Student s3=new Student("Rem", "boy", "21");
        
        hash.put("1", s1);
        hash.put("2", s2);
        hash.put("3", s3);
        
        Collection<String> coll=hash.keySet();
        for(Iterator<String> it=coll.iterator();it.hasNext();){
            String key=it.next();
            System.out.println("key->"+key);
        }
    }
}

输出结果:

key->3
key->2
key->1

遍历Value

package ArrayList;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

public class HashMapCollection {
    public static void main(String[] args){
HashMap<String,Student> hash=new HashMap<String, Student>();
        
        Student s1=new Student("Joker", "boy", "25");
        Student s2=new Student("Alice", "girl", "18");
        Student s3=new Student("Rem", "boy", "21");
        
        hash.put("1", s1);
        hash.put("2", s2);
        hash.put("3", s3);
        /*//遍历key
        Collection<String> coll=hash.keySet();
        for(Iterator<String> it=coll.iterator();it.hasNext();){
            String key=it.next();
            System.out.println("key->"+key);
        }*/
        //遍历value
        Collection<Student> colle=hash.values();
        for(Iterator<Student> it=colle.iterator();it.hasNext();){
            Student stu=it.next();
            System.out.println("姓名->"+stu.getName());
            System.out.println("性别->"+stu.getSex());
            System.out.println("年龄->"+stu.getAge());
            System.out.println();
        }
    }
}

输出结果:

姓名->Rem
性别->boy
年龄->21

姓名->Alice
性别->girl
年龄->18

姓名->Joker
性别->boy
年龄->25

同理用collection来遍历整个hashmap也是行的。

java进阶 集合类HashMap笔记 到此结束

 

posted @ 2014-04-25 14:33  未闻罂粟  阅读(181)  评论(0编辑  收藏  举报