Map中的HashMap集合

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=29
Lily=23//第二次输入相同键,覆盖了21 岁的Lily
Maria=32
Sily=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=29
Lily=23
Maria=32
Sily=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=29
Lily=23
Maria=32
Sily=22
//v.get(object key);//根据键获取值
//Set<k>keySet();//获取集合中所有键的集合
Sandy----29
Lily----23
Maria----32
Sily----22
//Collection <v> values();//获取集合中所有值的集合
29
23
32
22
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值