- /*
- 题目:已知:
- 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");
- 复制代码
- 求:
- ①将map集合的key值转变为key+(int)value,并输出转变后的map
- ②在第①步的基础上(map已经是新map了),删除map中50<key+(int)value<150的元素,并输出删除数据后的map
- */
- import java.util.*;
- class Test1207
- {
- public static void main(String[] args)
- {
- 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");
- Map<Integer, String> mapp = new HashMap<Integer, String>();
- Set<Map.Entry<Integer, String>> s =map.entrySet();
- Iterator<Map.Entry<Integer, String>> it =s.iterator();
- while(it.hasNext())
- {
- Map.Entry<Integer, String> me=it.next();
- mapp.put(me.getKey()+Integer.parseInt(me.getValue()),me.getValue());
- }
- System.out.println(mapp);//输出新创建的集合
- Set<Map.Entry<Integer, String>> ss =mapp.entrySet();
- Iterator<Map.Entry<Integer, String>> itt =ss.iterator();
- while(itt.hasNext())
- {
- Map.Entry<Integer, String> me=itt.next();
- if(50<me.getKey()+Integer.parseInt(me.getValue())&&me.getKey()+Integer.parseInt(me.getValue())<150)
- itt.remove();
- }
- System.out.println(mapp);//输出剔除元素后的集合
- }
- }
复制代码 |