Map中的HashMap集合

来源:互联网 发布:数据切片 编辑:程序博客网 时间:2024/05/29 07:26

Map集合:键值对对应的双链集合,分为:HashMap集合, Hashtable集合,TreeMap集合。
HashMap集合:采用接口多态的方式来创建对象。
HashMap集合的方法:

添加功能:
hashMap.put();
特点:第一次添加的数据返回的是Null值;而后面添加键相同的数据的时候会发生键相同值覆盖的现象!

HashMap的输入顺序与输入顺序是没有关系的即无序的。

程序举例:

 package _11.homework;import java.util.HashMap;import java.util.Map.Entry;import java.util.Set;public class HashMapDemo1 {    public static void main(String[] args) {        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();        hashMap.put("Lily", 21);        hashMap.put("Lily", 23);        hashMap.put("Sily", 22);        hashMap.put("Sandy", 29);        hashMap.put("Maria", 32);        // Set<String> hashSet=hashMap.keySet();        Set<Entry<String, Integer>> set = hashMap.entrySet();        for (Entry<String, Integer> hashKey : set) {            System.out.println(hashKey);        }    }

程序结果:

//输出结果与输入的顺序无关Sandy=29Lily=23//第二次输入相同键,覆盖了21 岁的LilyMaria=32Sily=22

删除功能:
方法:

void clear();//移除所有键值对元素V remove(Object key)://根据键删除相对应的值元素,并把该值返回String value=hashMap.remove();//根据键名移除相对应键值对并返回

程序举例:

package _11.homework;import java.util.HashMap;import java.util.Map.Entry;import java.util.Set;public class HashMapDemo1 {    public static void main(String[] args) {        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();        hashMap.put("Lily", 21);        hashMap.put("Lily", 23);        hashMap.put("Sily", 22);        hashMap.put("Sandy", 29);        hashMap.put("Maria", 32);        //hashMap.clear();        // Set<String> hashSet=hashMap.keySet();        Set<Entry<String, Integer>> set = hashMap.entrySet();        for (Entry<String, Integer> hashKey : set) {            System.out.println(hashKey);        }        System.out.println("----------------------------");        System.out.println(hashMap.remove("Sily"));        System.out.println("----------------------------");        Integer value=hashMap.remove("Sandy");        System.out.println(value);    }}运行结果:Sandy=29Lily=23Maria=32Sily=22----------------------------22----------------------------29

判断功能:
方法:

boolean containsKey(object key)//判断集合是否包含指定的键boolean containsValue(object value)//判断集合是否包含该值boolean isEmpty()//判断集合是否为空

程序举例:

package _11.homework;import java.util.HashMap;public class HashMapDemo1 {    public static void main(String[] args) {        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();        hashMap.put("Lily", 23);        hashMap.put("Sily", 22);        hashMap.put("Sandy", 29);        hashMap.put("Maria", 32);        // hashMap.clear();        System.out.println(hashMap.containsKey("Lily"));        System.out.println("-------");        System.out.println(hashMap.containsValue(28));        System.out.println("-------");        System.out.println(hashMap.isEmpty());    }}运行结果:true-------false-------false

获取功能:
方法:

Set<Entry<k,v>>.entrySet();//返回一个键值对Set集合v.get(object key);//根据键获取值Set<k>keySet();//获取集合中所有键的集合Collection <v> values();//获取集合中所有值的集合

程序举例:

package _11.homework;import java.util.Collection;import java.util.HashMap;import java.util.Map.Entry;import java.util.Set;public class HashMapDemo1 {    public static void main(String[] args) {        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();        hashMap.put("Lily", 23);        hashMap.put("Sily", 22);        hashMap.put("Sandy", 29);        hashMap.put("Maria", 32);        Set<Entry<String,Integer>> set=hashMap.entrySet();        for(Entry<String,Integer> setKey:set){            System.out.println(setKey);        }        System.out.println("---------------------");        Set<String> hashSet=hashMap.keySet();        for(String hashKey:hashSet){            System.out.println(hashKey+"----"+hashMap.get(hashKey));        }        System.out.println("---------------------");        Collection<Integer> value=hashMap.values();        for(Integer keyValue:value){            System.out.println(keyValue);        }    }}输出结果://Set<Entry<k,v>>.entrySet();//返回一个键值对Set集合Sandy=29Lily=23Maria=32Sily=22//v.get(object key);//根据键获取值//Set<k>keySet();//获取集合中所有键的集合Sandy----29Lily----23Maria----32Sily----22//Collection <v> values();//获取集合中所有值的集合29233222