Java中的Map常见使用案例代码
以下是一些Java中Map的常见使用案例和具体代码实现:
Map的遍历
Map<String, Integer> map = new HashMap<>();
map.put(“apple”, 10);
map.put(“banana”, 20);
map.put(“orange”, 30);
// 遍历方式一:使用entrySet()方法遍历
for (Map.Entry<String, 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的排序
Map<String, Integer> map = new HashMap<>();
map.put("apple", 10);
map.put("banana", 20);
map.put("orange", 30);
// 按照键值升序排序
Map<String, Integer> sortedMap = new TreeMap<>(map);
sortedMap.forEach((key, value) -> System.out.println("key: " + key + ", value: " + value));
// 按照键值降序排序
Map<String, 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的过滤
Map<String, Integer> map = new HashMap<>();
map.put("apple", 10);
map.put("banana", 20);
map.put("orange", 30);// 过滤出键值大于20的元素
Map<String, Integer> filteredMap = new HashMap<>();
for (Map.Entry<String, 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中的元素的个数,例如:
Map<String, 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是否为空,例如:
Map<String, Integer> map = new HashMap<>();
boolean isEmpty = map.isEmpty();
System.out.println(isEmpty);
判断Map中是否包含某个键或值
可以使用containsKey()方法判断Map中是否包含某个键,使用containsValue()方法判断Map中是否包含某个值,例如:
Map<String, 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中所有值的集合,例如:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 10);
map.put("banana", 20);
map.put("orange", 30);Set<String> keySet = map.keySet();
Collection<Integer> values = map.values();
System.out.println(keySet);
System.out.println(values);
获取Map中的键值对集合
可以使用entrySet()方法获取Map中所有键值对的集合,例如:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 10);
map.put("banana", 20);
map.put("orange", 30);Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
for (Map.Entry<String, Integer> entry : entrySet) {System.out.println("key: " + entry.getKey() + ", value: " + entry.getValue());
}
删除Map中的元素
可以使用remove()方法删除Map中的元素,例如:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 10);
map.put("banana", 20);
map.put("orange", 30);map.remove("banana");
System.out.println(map);
以上是Map中一些常见的操作及其代码示例,掌握这些操作可以帮助我们更好地处理键值对数据,并提高程序的效率和可读性。