网站建设 报价,邢台网站123,专业型网站和个人网站,主题网页设计以下是一些Java中Map的常见使用案例和具体代码实现#xff1a;
Map的遍历 MapString, Integer map new HashMap(); map.put(“apple”, 10); map.put(“banana”, 20); map.put(“orange”, 30);
// 遍历方式一#xff1a;使用entrySet()方法遍历
for (M…以下是一些Java中Map的常见使用案例和具体代码实现
Map的遍历 MapString, Integer map new HashMap(); map.put(“apple”, 10); map.put(“banana”, 20); map.put(“orange”, 30);
// 遍历方式一使用entrySet()方法遍历
for (Map.EntryString, Integer entry : map.entrySet()) {System.out.println(key: entry.getKey() , value: entry.getValue());
}// 遍历方式二使用keySet()方法遍历
for (String key : map.keySet()) {System.out.println(key: key , value: map.get(key));
}// 遍历方式三使用forEach()方法遍历 map.forEach((key, value) - System.out.println(key: key , value: value)); 在这个代码中我们首先创建了一个HashMap对象并向其中添加了三个键值对。然后我们演示了三种不同的遍历方式分别是使用entrySet()方法、keySet()方法和forEach()方法。这三种方式都可以遍历Map中的键值对但是使用entrySet()方法的效率最高因为它只需要遍历一次Map中的元素。
Map的排序
MapString, Integer map new HashMap();
map.put(apple, 10);
map.put(banana, 20);
map.put(orange, 30);// 按照键值升序排序
MapString, Integer sortedMap new TreeMap(map);
sortedMap.forEach((key, value) - System.out.println(key: key , value: value));// 按照键值降序排序
MapString, Integer reverseSortedMap new TreeMap(Collections.reverseOrder());
reverseSortedMap.putAll(map);
reverseSortedMap.forEach((key, value) - System.out.println(key: key , value: value));在这个代码中我们首先创建了一个HashMap对象并向其中添加了三个键值对。然后我们演示了两种不同的排序方式分别是按照键值升序排序和按照键值降序排序。我们使用TreeMap对象来进行排序并通过forEach()方法遍历排序后的Map对象。
Map的过滤
MapString, Integer map new HashMap();
map.put(apple, 10);
map.put(banana, 20);
map.put(orange, 30);// 过滤出键值大于20的元素
MapString, Integer filteredMap new HashMap();
for (Map.EntryString, Integer entry : map.entrySet()) {if (entry.getValue() 20) {filteredMap.put(entry.getKey(), entry.getValue());}
}
filteredMap.forEach((key, value) - System.out.println(key: key , value: value));在这个代码中我们首先创建了一个HashMap对象并向其中添加了三个键值对。然后我们通过遍历map对象中的键值对过滤出其中值大于20的元素并将过滤结果添加到一个新的HashMap对象中。最后我们使用forEach()方法遍历过滤后的Map对象并打印出每个键值对。
总之Map是Java中常用的一种数据结构可以用来存储键值对并提供了丰富的方法和功能例如遍历、排序、过滤等。掌握Map的基本使用方法和常见操作可以帮助我们更好地处理键值对数据并提高程序的效率和可读性。
除了上面提到的去重、遍历、排序、过滤等操作Map还有一些其他常用的操作以下是一些常见的操作及其代码示例
获取Map中的元素个数 可以使用size()方法获取Map中的元素的个数例如
MapString, Integer map new HashMap();
map.put(apple, 10);
map.put(banana, 20);
map.put(orange, 30);int size map.size();
System.out.println(size);判断Map是否为空 可以使用isEmpty()方法判断Map是否为空例如
MapString, Integer map new HashMap();
boolean isEmpty map.isEmpty();
System.out.println(isEmpty);判断Map中是否包含某个键或值 可以使用containsKey()方法判断Map中是否包含某个键使用containsValue()方法判断Map中是否包含某个值例如
MapString, Integer map new HashMap();
map.put(apple, 10);
map.put(banana, 20);
map.put(orange, 30);boolean containsKey map.containsKey(apple);
boolean containsValue map.containsValue(20);
System.out.println(containsKey);
System.out.println(containsValue);获取Map中的键集合或值集合 可以使用keySet()方法获取Map中所有键的集合使用values()方法获取Map中所有值的集合例如
MapString, Integer map new HashMap();
map.put(apple, 10);
map.put(banana, 20);
map.put(orange, 30);SetString keySet map.keySet();
CollectionInteger values map.values();
System.out.println(keySet);
System.out.println(values);获取Map中的键值对集合 可以使用entrySet()方法获取Map中所有键值对的集合例如
MapString, Integer map new HashMap();
map.put(apple, 10);
map.put(banana, 20);
map.put(orange, 30);SetMap.EntryString, Integer entrySet map.entrySet();
for (Map.EntryString, Integer entry : entrySet) {System.out.println(key: entry.getKey() , value: entry.getValue());
}删除Map中的元素 可以使用remove()方法删除Map中的元素例如
MapString, Integer map new HashMap();
map.put(apple, 10);
map.put(banana, 20);
map.put(orange, 30);map.remove(banana);
System.out.println(map);以上是Map中一些常见的操作及其代码示例掌握这些操作可以帮助我们更好地处理键值对数据并提高程序的效率和可读性。