HashMap常用方法

package com.chenny.Test;

import java.util.HashMap;

public class helloWorld {
	public static void main(String[] args) {
		HashMap<Integer,String> hashMap = new HashMap<Integer,String>();
		//HashMap存值:
		/*
		 * key值是唯一的, Value值可以不唯一
		 * 重复输入Key值,之前的会被覆盖
		 * */
		hashMap.put(1,"One");
		hashMap.put(2,"Two");
		hashMap.put(3,"Three");
		hashMap.put(4,"Four");
		hashMap.put(5,"Five");
		hashMap.put(5,"Six");
		hashMap.put(6,"Five");
		hashMap.put(7, "Six");
		
		System.out.println("HashMap取值(根据key取值):");
		System.out.println(hashMap.get(5));
		System.out.println(hashMap);
		System.out.println("=============================================");
		
		System.out.println("判断HashMap是否为空:");
		System.out.println(hashMap.isEmpty());
		System.out.println(hashMap);
		System.out.println("=============================================");
		
		System.out.println("Hashmap判断是否含有key:");
		System.out.println(hashMap.containsKey(3));
		System.out.println(hashMap);
		System.out.println("=============================================");
		
		System.out.println("Hashmap判断是否含有value:");
		System.out.println(hashMap.containsValue("One"));
		System.out.println(hashMap);
		System.out.println("=============================================");
		
		System.out.println("Hashmap删除操作");
		System.out.println(hashMap.remove(3));
		System.out.println(hashMap.remove("Four"));
		System.out.println(hashMap);
		System.out.println("=============================================");
		
		System.out.println("获取HashMap中的 value 的集合");
		System.out.println(hashMap.values());
		System.out.println(hashMap);
		System.out.println("=============================================");
		
		System.out.println("获取HashMap中的key的集合");
		System.out.println(hashMap.keySet());
		System.out.println(hashMap);
		System.out.println("=============================================");
		
		System.out.println("Hashmap显示所有的key和value");
		System.out.println(hashMap.entrySet());
		System.out.println("=============================================");
		
		System.out.println(hashMap);
		System.out.println("=============================================");
		
		
		System.out.println("HashMap元素个数");
		System.out.println(hashMap.size());
		
	
		System.out.println("HashMap的遍历");		
		for(Entry<Integer,Stirng> entry : hashMap.entrySet()){
			system.out.println(map);
			system.out.println("key=" + map.getKey() + " value = " + map.getValue());
		}
	}
}


返回Map目录

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值