Map 里面的方法

本文深入解析Java集合框架中的关键API,包括remove、isEmpty、size、containsKey、containsValue、put、remove、putAll、clear、keySet、values、entrySet等方法的使用场景与实现原理,旨在帮助开发者更熟练地掌握集合操作。

V remove(Object key);

 Removes the mapping for a key from this map if it is present.

boolean isEmpty();

Returns <tt>true</tt> if this map contains no key-value mappings.

 int size();

Returns the number of key-value mappings in this map.  If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

 boolean containsKey(Object key);

Returns true if this map contains a mapping for the specified key.

 boolean containsValue(Object value);

 Returns true if this map maps one or more keys to the specified value.

 V put(K key, V value);

Associates the specified value with the specified key in this map (optional operation).  If the map previously contained a mapping for the key, the old value is replaced by the specified value.

 V remove(Object key);

Removes the mapping for a key from this map if it is present (optional operation).

The map will not contain a mapping for the specified key once the call returns.

void putAll(Map<? extends K, ? extends V> m);

 Copies all of the mappings from the specified map to this map (optional operation).

void clear();

Removes all of the mappings from this map (optional operation).
The map will be empty after this call returns.

Set<K> keySet();

@return a set view of the keys contained in this map

Collection<V> values();

@return a collection view of the values contained in this map

 Set<Map.Entry<K, V>> entrySet();

@return a set view of the mappings contained in this map



### JavaScript 中 `Array.prototype.map()` 方法的用法 `Array.prototype.map()` 是一种用于遍历数组并返回新数组的方法。它不会修改原始数组,而是基于回调函数的结果创建一个新的数组[^1]。 #### 基本语法 以下是 `map()` 的基本语法: ```javascript const newArray = array.map(callback(currentValue[, index[, array]]) { // 返回处理后的值 }[, thisArg]); ``` - **callback**: 对原数组中的每个元素执行的函数。 - **currentValue**: 当前正在处理的元素。 - **index**(可选): 正在处理的当前索引。 - **array**(可选): 调用了 `map` 的原数组本身。 - **thisArg**(可选): 执行 callback 时作为其 `this` 关键字使用的值。 #### 使用示例 下面是一个简单的例子,展示如何使用 `map()` 将数组中的每个数值加倍: ```javascript const numbers = [1, 2, 3, 4]; const doubledNumbers = numbers.map(function(num) { return num * 2; }); console.log(doubledNumbers); // 输出: [2, 4, 6, 8] ``` 如果使用箭头函数,则可以简化为如下形式: ```javascript const numbers = [1, 2, 3, 4]; const doubledNumbers = numbers.map(num => num * 2); console.log(doubledNumbers); // 输出: [2, 4, 6, 8] ``` #### 结合其他操作 还可以利用 `map()` 进行更复杂的转换,比如对消息列表进行加密[^3]: ```javascript function caesar(str) { return str.split('').map(char => String.fromCharCode((char.charCodeAt(0) - 97 + 3) % 26 + 97)).join(''); } const messages = ["hello", "world"]; const encryptedMessages = messages.map(caesar); console.log(encryptedMessages); // 输出: ['khoor', 'zruog'] ``` 此代码片段展示了如何通过自定义逻辑(凯撒密码算法)来映射字符串数组到另一个经过变换的新数组。 --- ### 注意事项 虽然 `map()` 不会更改调用它的数组,但在某些情况下可能会与其他库配合使用以实现变异效果。例如,在 lodash 库中可以通过 `_._pull()` 或者 `_._without()` 来移除指定项[^2]。然而这些方法的行为不同于标准的 `map()` 函数。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值