- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Set;
- public class MapDemo2
- {
- public static void main(String[] args)
- {
- //初始化map
- Map<Integer, String> map = new HashMap<Integer, String>();
- map.put(1, "90");
- map.put(2, "80");
- map.put(3, "70");
- map.put(4, "60");
- map.put(5, "50");
- map.put(6, "40");
- map.put(7, "30");
- map.put(8, "20");
- map.put(9, "10");
-
- Set<Entry<Integer,String>> entry=map.entrySet();//得到键值对的集合
- Iterator<Entry<Integer,String>> iter=entry.iterator();//得到迭代器;
- Map<Integer, String> map2 = new HashMap<Integer, String>();//作为map数值改变的中间媒介;
- while(iter.hasNext())
- {
- //得到map的键和值;
- Entry<Integer,String> ent=iter.next();
- Integer key=ent.getKey();
- String value=ent.getValue();
-
- //增加map的key值
- key+=Integer.parseInt(value);
- map2.put(key,value);
-
- //删除map2里新key(50,150)之间的元素
- if(key>50&&key<150)
- map2.remove(key,value);
-
- //重新构造map
- map=new HashMap<Integer,String>(map2);
- }
- System.out.println(map);
- }
- }
复制代码 |