最简单的修改HashMap value值的方法

41 篇文章 10 订阅
28 篇文章 0 订阅

说到遍历,首先应该想到for循环,然而map集合的遍历通常情况下是要这样在的,先要获得一个迭代器。

Map<Integer,String> map = new HashMap<>();   
  
    Iterator it = map.entrySet().iterator();   
  
    while (it.hasNext()) {   
  
        Map.Entry entry = (Map.Entry) it.next();   
  
        Object key = entry.getKey();   
  
        Object value = entry.getValue();  

实际上一个foreach循环也是可以的,很简洁吧~

for(Map.Entry<Integer,Integer> m:map.entrySet())
				{
					if(arr[i]==(int)m.getKey())
						map.put((int)m.getKey(),(int)m.getValue()+1);
				}

附上一个完整的小程序例子。

随机生成长度为100的数组,数组元素为1到10,统计出现次数最多和最少的元素

import java.util.*;
class	Count 
{
	public void count(int[] arr)
	{
		int num=0;
		Map<Integer,Integer> map=new HashMap<Integer,Integer>();
		for(int i=1;i<=10;i++)
		{
			map.put(i,num);
		}
		for(int i=0;i<arr.length;i++)
		{
			/*Iterator it = map.entrySet().iterator();    
			while(it.hasNext())
				{     
					Map.Entry m=(Map.Entry)it.next();
					if(arr[i]==(int)m.getKey())
						map.put((int)m.getKey(),(int)m.getValue()+1);
				}*/
				for(Map.Entry<Integer,Integer> m:map.entrySet())
				{
					if(arr[i]==(int)m.getKey())
						map.put((int)m.getKey(),(int)m.getValue()+1);
				}
		}
		for(Map.Entry<Integer,Integer> m:map.entrySet())
		{
			System.out.println(""+m.getKey()+"出现的次数为:"+m.getValue()+"次");
		}
	}
	
	public static void main(String[] args) 
	{
		Random rd=new Random();
		int[] arr=new int[100];
		for(int i=0;i<100;i++)
		{
			arr[i]=rd.nextInt(10)+1;
		}
		new Count().count(arr);
	}
}

供大家参考。

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,根据您的要求,我为您编写了两个类的代码,如下所示: ```java // People类 public class People { private String name; // 姓名 private String phone; // 电话号码 public People(String name, String phone) { this.name = name; this.phone = phone; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } } // PeopleManage类 import java.util.HashMap; public class PeopleManage { private HashMap<String, People> peopleMap; // 存储People对象的HashMap public PeopleManage() { peopleMap = new HashMap<String, People>(); } // 添加People对象 public void addPeople(People people) { peopleMap.put(people.getName(), people); } // 根据姓名查询People对象 public People searchPeople(String name) { return peopleMap.get(name); } // 修改People对象的电话号码 public void modifyPeople(String name, String phone) { People people = peopleMap.get(name); if (people != null) { people.setPhone(phone); } } // 根据姓名删除People对象 public void deletePeople(String name) { peopleMap.remove(name); } } // Test类 public class Test { public static void main(String[] args) { // 创建PeopleManage对象 PeopleManage peopleManage = new PeopleManage(); // 添加People对象 People p1 = new People("张三", "123456789"); People p2 = new People("李四", "987654321"); peopleManage.addPeople(p1); peopleManage.addPeople(p2); // 根据姓名查询People对象 People p3 = peopleManage.searchPeople("张三"); System.out.println(p3.getName() + "的电话号码是:" + p3.getPhone()); // 修改People对象的电话号码 peopleManage.modifyPeople("张三", "111111111"); System.out.println("修改后," + p3.getName() + "的电话号码是:" + p3.getPhone()); // 根据姓名删除People对象 peopleManage.deletePeople("李四"); System.out.println("删除后,李四的信息是否存在:" + (peopleManage.searchPeople("李四") == null)); } } ``` 以上代码实现了一个简单的通信录系统,可以进行输入、按名字查询、修改和删除功能。在Test的main方法中,我们创建了一个PeopleManage对象,添加了两个People对象,然后根据姓名查询了一个People对象,修改了其中一个People对象的电话号码,最后删除了一个People对象。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值