Java 在HashMap中递增一个整数
我必须先归还物品,然后再放入一个新的吗?或者我可以直接增加吗Java 在HashMap中递增一个整数,java,pointers,reference,hashmap,Java,Pointers,Reference,Hashmap,我必须先归还物品,然后再放入一个新的吗?或者我可以直接增加吗 Integer temp = myMap.get(key); temp++; myMap.put(key, temp); // If the key you want to add does not exist then add it as a new key // And make the value 1 if (map.get(key) == null) {
Integer temp = myMap.get(key);
temp++;
myMap.put(key, temp);
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
没有办法做到这一点(这不起作用):
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
由于
Integer
是不可变的,所以必须这样做。
如果你真的想直接增加它,你必须编写你自己的可变类。你不能直接增加它,因为它是可变的。您必须增加它并将新对象放回
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
自动装箱在这里也起到了干扰作用。事实上,所发生的事情类似于:
Integer i1 = getFromMap();
i1 = Integer.valueOf(++ i1.intValue());
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
这里你的参照点指向一个新的物体。你必须把那个物体放回地图,在同一个键下
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
我必须先归还物品,然后再放入一个新的吗
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
只要使用
Integer
包装类,就可以使用yes,因为它是不可变的。您可以使用可变包装器类,甚至是具有increment()
方法的包装器类。但是,您将失去对值使用自动装箱和自动取消装箱的功能。首先:注意取消装箱:临时值是从整数类型开始的。但是操作++是为int实现的。因此temp被取消绑定为int类型。这意味着如果temp为null,则在NullPointerException中运行
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
你必须按照你在第一个代码块中描述的那样去做。这是完成这项工作的最短代码
myMap.put(key, myMap.get(key) + 1)
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
我认为它不会太长。您可以使用可变整数,例如AtomicInteger
Map<Key, AtomicInteger> myMap = new HashMap<Key, AtomicInteger>();
myMap.get(key).incrementAndGet();
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
Map myMap=newhashmap();
myMap.get(key.incrementAndGet();
或者可以使用Trove4j,它支持集合中的原语
TObjectIntHashMap<Key> myMap;
myMap.increment(key);
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
tobjectnthashmap myMap;
myMap.increment(key);
如果您必须执行两次以上的操作,您更愿意创建一个小类,如:
public class MappedCounter {
private Map<String, Integer> map = new HashMap<String, Integer>();
public void addInt(String k, int v) {
if (!map.containsKey(k)) map.put(k, v);
else map.put(k, map.get(k) + v);
}
public int getInt(String k) {
return map.containsKey(k) ? map.get(k) : 0;
}
public Set<String> getKeys() {
return map.keySet();
}
}
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
公共类映射计数器{
私有映射映射=新的HashMap();
公共void addInt(字符串k,int v){
如果(!map.containsKey(k))map.put(k,v);
else-map.put(k,map.get(k)+v);
}
公共整数getInt(字符串k){
返回map.containsKey(k)?map.get(k):0;
}
公共设置getKeys(){
返回map.keySet();
}
}
在Java 8中,Map
上有一些新的方法,可以与lambdas一起使用来解决这个问题。第一种选择:
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
请注意,这仅在为所有可能的键初始化哈希时有效
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
如果不能保证这一点,您可以将上述代码更改为:
a.compute(key, (k, v) -> v == null ? 1 : v + 1);
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
或者使用以下方法(我更喜欢):
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
也许我不知道还有更多基于lambda的方法 我使用下面的代码,它可以工作,但在开始时,您需要定义一个
BiFunction
来描述操作递增1
public static Map<String, Integer> strInt = new HashMap<String, Integer>();
public static void main(String[] args) {
BiFunction<Integer, Integer, Integer> bi = (x,y) -> {
if(x == null)
return y;
return x+y;
};
strInt.put("abc", 0);
strInt.merge("abc", 1, bi);
strInt.merge("abc", 1, bi);
strInt.merge("abc", 1, bi);
strInt.merge("abcd", 1, bi);
System.out.println(strInt.get("abc"));
System.out.println(strInt.get("abcd"));
}
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
这应该行得通
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
为了完整起见,Java8中有一个longAdder,它与AtomicInteger()相比带来了一些好处
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
final Map result=new HashMap();
result.get(无论什么).increment();
发现这是避免NPE的最佳方法
// If the key you want to add does not exist then add it as a new key
// And make the value 1
if (map.get(key) == null) {
map.put(key, 1);
} else {
// If the key does exist then replace the key's value with it's
// Original value plus one
map.put(key, map.get(key) + 1);
}
Map<Integer, Integer> map = new HashMap<>();
map.put(5, map.getOrDefault(5, 0) + 1);
System.out.println(map.get(5));
Output:
1
Map Map=newhashmap();
map.put(5,map.getOrDefault(5,0)+1);
System.out.println(map.get(5));
输出:
1.
或者使用,比如说,AtomicInteger
。它应该如何工作?Put方法有两个参数,一个是键,一个是新值。+1,依我看,这应该是公认的答案:简单明了-不需要不可变的包装器(在非MT场景中放置原子构造会让人困惑)。正如Piotr所观察到的,关键应该是第一个arg thoughPerformance?2个搜索操作:一个是put(),一个是get()@IgriZdes,因为OP在Map中存储不变的对象,所以无法避免双重搜索。唯一可以避免这种情况的解决方案是使用具有此解决方案所有优点和缺点的可变对象。请注意,如果没有为所有可能的键初始化值,此解决方案将导致空指针异常。问题:当您说使用incrementAndGet时,您的实际意思是:myMap.put(键,新原子长(myMap.get)(键).incrementandget())代码>对吗?要在映射中插入新值或递增,并且Get也会更新映射中的值,映射将保存引用,而不是对象本身。你不需要在映射中放回对可变对象的引用,因为你改变了底层对象而不是引用。这就是我想要的。根据java8FWIW,这是最好的答案。如果使用Java 11,你可以使用lambda函数a.merge(key,1,Integer::sum);因为问题是“我可以在HashMap中直接递增一个整数吗?”我认为你的答案基本上是“否”。问题是->“在HashMap中递增一个整数”。问题是“我必须返回对象然后放入一个新对象吗?或者我可以直接递增吗?”。我很抱歉你发现我的评论粗鲁,但我认为当答案不解决问题时,它是非常无益的。