黑马程序员技术交流社区

标题: Map集合问题 [打印本页]

作者: lcl    时间: 2014-10-16 20:13
标题: Map集合问题
Map<String,String> map = new HashMap<String,String>();
System.out.println(map.put("01","abc"));
为啥输出为null,01对应的不是abc吗?
作者: 朴日男    时间: 2014-10-16 20:25
V put(K key, V value)
返回:
以前与 key 关联的值,如果没有针对 key 的映射关系,则返回 null
map.put("01","abc")  因为 "01" 之前与 null 对应, 就返回 null咯.
  1. package com.itheima.game;

  2. import java.util.HashMap;
  3. import java.util.Map;

  4. public class Game8
  5. {
  6.         public static void main(String[] args)
  7.         {

  8.                 Map<String,String> map = new HashMap<String,String>();
  9.                 map.put("01","bbb");
  10.                 System.out.println(map.put("01","abc")); //返回 bbb
  11.         }
  12. }
复制代码

作者: lcl    时间: 2014-10-16 20:28
朴日男 发表于 2014-10-16 20:25
V put(K key, V value)
返回:
以前与 key 关联的值,如果没有针对 key 的映射关系,则返回 null

01为啥与null对应呢,我用put添加的不是“01”,“abc”,那他不是与abc对应吗
作者: 朴日男    时间: 2014-10-16 20:49
http://bbs.itheima.com/thread-27232-1-1.html
你要的答案在这里, 有图解. 去看吧.
作者: lcl    时间: 2014-10-16 20:50
朴日男 发表于 2014-10-16 20:49
http://bbs.itheima.com/thread-27232-1-1.html
你要的答案在这里, 有图解. 去看吧.

看过了,多谢了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2