地图的大小限制为3个元素

时间:2014-02-26 23:56:01

标签: java spring spring-mvc

我正在尝试用Java填充Map,但由于某种原因它不会让地图有超过3个元素,即使我想要更多。我该如何解决这个问题?

这是java代码,恰好位于spring mvc应用程序的控制器方法中:

@ModelAttribute("addLinkOptions") 
public Map loadLinkOptions(){ 
    LinkedHashMap<String, String> addLinks = new LinkedHashMap<String,String>(); 
    addLinks.put("/calendar?day=2014-01-10","New this"); 
    System.out.println(" ......1 size of addlinks is: "+addLinks.size());
    addLinks.put("/","New that"); 
    System.out.println(" ......2 size of addlinks is: "+addLinks.size());
    addLinks.put("/otherlink","New some other thing"); 
    System.out.println(" ......3 size of addlinks is: "+addLinks.size());
    addLinks.put("/","New dog"); 
    System.out.println(" ......4 size of addlinks is: "+addLinks.size());
    addLinks.put("/","New cat"); 
    System.out.println(" ......5 size of addlinks is: "+addLinks.size());
    addLinks.put("/","New Car"); 
    System.out.println(" ......6 size of addlinks is: "+addLinks.size());
    addLinks.put("/","New lizard"); 
    System.out.println(" ......7 size of addlinks is: "+addLinks.size());
    addLinks.put("/","New bike"); 
    System.out.println(" ......8 size of addlinks is: "+addLinks.size());
    addLinks.put("/","New motorcycle"); 
    System.out.println(" ......9 size of addlinks is: "+addLinks.size());
    addLinks.put("/","New frog"); 
    System.out.println(" ......10 size of addlinks is: "+addLinks.size());
    return addLinks;
}  

 ......1 size of addlinks is: 1
 ......2 size of addlinks is: 2
 ......3 size of addlinks is: 3
 ......4 size of addlinks is: 3
 ......5 size of addlinks is: 3
 ......6 size of addlinks is: 3
 ......7 size of addlinks is: 3
 ......8 size of addlinks is: 3
 ......9 size of addlinks is: 3
 ......10 size of addlinks is: 3
 ......10 size of addlinks is: 3

2 个答案:

答案 0 :(得分:4)

地图是键/值存储,每个键都是唯一的,使用相同的键添加值将覆盖该键的上一个值。

答案 1 :(得分:2)

Map每个键只有一个元素。所以当你打电话时

addLinks.put("/","New dog"); 

删除 /的旧条目,并将其替换为"New dog"

我不知道你真正想要的是什么:也许来自像番石榴这样的第三方图书馆的Map<String, List<String>>ListMultimap<String, String>