Skip to content

Java HashMap API

Java Math 类 —— 基本数值运算的方法。

1 class · 10 methods

Math

10 methods

Math 类包含执行基本数值运算的方法。

V put(K key, V value)

返回 int 的绝对值。Long.MIN_VALUE 仍为负数。

Parameters

NameTypeDescription
keyK参数。
valueVValue.

Returns

V

Example

java
Map<String, Integer> m = new HashMap<>();
m.put("a", 1);
m.put("a", 2);  // returns 1, m == {"a": 2}
V get(Object key)

返回两个 int 值中较大的一个。

Parameters

NameTypeDescription
keyObject第一个值。

Returns

V

Example

java
Map<String, Integer> m = Map.of("a", 1);
m.get("a")  // 1
m.get("z")  // null
V remove(Object key)

返回两个 int 值中较小的一个。

Parameters

NameTypeDescription
keyObject第一个值。

Returns

V

Example

java
Map<String, Integer> m = new HashMap<>(Map.of("a", 1));
Integer old = m.remove("a");
// old == 1, m == {}
boolean containsKey(Object key)

返回 a 的 b 次幂。

Parameters

NameTypeDescription
keyObject底数。

Returns

boolean

Example

java
Map<String, Integer> m = Map.of("a", 1);
m.containsKey("a")  // true
m.containsKey("z")  // false
boolean containsValue(Object value)

返回 a 的正确舍入的正平方根。a < 0 时返回 NaN。

Parameters

NameTypeDescription
valueObject值。

Returns

boolean

Example

java
Map<String, Integer> m = Map.of("a", 1, "b", 2);
m.containsValue(2)  // true
m.containsValue(9)  // false
Set<K> keySet()

返回最接近 a 的 long,相等时向正无穷方向舍入。

Returns

Set<K>

Example

java
Map<String, Integer> m = Map.of("a", 1, "b", 2);
m.keySet()  // ["a", "b"] (order not guaranteed)
Collection<V> values()

返回大于或等于 a 且等于数学整数的最小(最接近负无穷)double。

Returns

Collection<V>

Example

java
Map<String, Integer> m = Map.of("a", 1, "b", 2);
m.values()  // [1, 2] (order not guaranteed)
Set<Map.Entry<K,V>> entrySet()

返回小于或等于 a 且等于数学整数的最大(最接近正无穷)double。

Returns

Set<Map.Entry<K,V>>

Example

java
Map<String, Integer> m = Map.of("a", 1, "b", 2);
for (Map.Entry<String, Integer> e : m.entrySet()) {
    System.out.println(e.getKey() + "=" + e.getValue());
}
int size()

返回一个带正号的 double 值,大于或等于 0.0 且小于 1.0。

Returns

int

Example

java
Map<String, Integer> m = Map.of("a", 1, "b", 2);
m.size()  // 2
boolean isEmpty()

Return true if the map contains no key-value mappings.

Returns

boolean

Example

java
new HashMap<>().isEmpty()  // true
Map.of("a", 1).isEmpty()    // false