java, map,如何实现当读入的key发生重复时,只保留重复key的第一行?
展开全部
public static void main(String[] args)
{
String[] keys = {"aaaa", "bbbb", "cccc", "bbbb"};
String[] values = {"aaaaValue", "bbbbValue1", "ccccValue", "bbbbValue2"};
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < keys.length; i++)
{
if (!map.containsKey(keys[i]))
{
map.put(keys[i], values[i]);
}
}
for(String key : map.keySet())
{
System.out.println(key+"="+map.get(key));
}
}
bbbb=bbbbValue1
aaaa=aaaaValue
cccc=ccccValue
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询