A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小马范 中级黑马   /  2014-12-10 08:31  /  1040 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. 题目:已知:

  2.     Map<Integer, String> map = new HashMap<Integer, String>();
  3.                     map.put(1, "90");
  4.                     map.put(2, "80");
  5.                     map.put(3, "70");
  6.                     map.put(4, "60");
  7.                     map.put(5, "50");
  8.                     map.put(6, "40");
  9.                     map.put(7, "30");
  10.                     map.put(8, "20");
  11.                     map.put(9, "10");

  12. 复制代码

  13. 求:
  14.         ①将map集合的key值转变为key+(int)value,并输出转变后的map
  15.         ②在第①步的基础上(map已经是新map了),删除map中50<key+(int)value<150的元素,并输出删除数据后的map
复制代码
答案如下:
  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3. import java.util.Map;
  4. import java.util.Set;

  5. public class MapTest {

  6.         public static void main(String[] args) {
  7.                 Map<Integer, String> map = new HashMap<Integer, String>();
  8.                 map.put(61, "90");
  9.                 map.put(2, "80");
  10.                 map.put(3, "70");
  11.                 map.put(4, "60");
  12.                 map.put(5, "50");
  13.                 map.put(6, "40");
  14.                 map.put(7, "30");
  15.                 map.put(8, "20");
  16.                 map.put(9, "10");
  17.                 Set<Integer> keySet = map.keySet();
  18.                 Iterator<Integer> i = keySet.iterator();
  19.                 HashMap<Integer, String> map1 = new HashMap<Integer, String>();
  20.                 System.out.println("第一题结果:");

  21.                 while (i.hasNext()) {
  22.                         Integer key = i.next();
  23.                         String value = map.get(key);
  24.                         int add = new Integer(value);
  25.                         Integer newKey = key + add;
  26.                         map1.put(newKey, value);
  27.                         System.out.println(newKey + " " + value);
  28.                 }

  29.                 Set<Integer> keySet1 = map1.keySet();
  30.                 HashMap<Integer, String> map2 = new HashMap<Integer, String>();
  31.                 System.out.println("第二题结果:");
  32.                 for (Iterator<Integer> i1 = keySet1.iterator(); i1.hasNext();) {
  33.                         Integer key = i1.next();
  34.                         while (key.intValue() < 50 || key.intValue() > 150) {
  35.                                 String value = map1.get(key);
  36.                                 map2.put(key, value);
  37.                                 System.out.println(key + " " + value);
  38.                                 break;
  39.                         }

  40.                 }

  41.         }

  42. }
复制代码



5 个回复

倒序浏览
怎么不写一个不写一个 temp的Map 集合  第一次用完了 clear  第二次继续使用 ~~:lol
回复 使用道具 举报
第10行代码是不是写错了                map.put(61, "90");
回复 使用道具 举报
天天小志 发表于 2014-12-13 20:32
第10行代码是不是写错了                map.put(61, "90");

题目抄错了,应该是map.put(1,"90")
回复 使用道具 举报
superjiejie 发表于 2014-12-10 17:26
怎么不写一个不写一个 temp的Map 集合  第一次用完了 clear  第二次继续使用 ~~ ...

第二题不是要在原来的基础上操作的嘛,   
回复 使用道具 举报
加油啊 加油
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马