个性化阅读
专注于IT技术分析

Java Map接口

点击下载

本文概述

映射包含基于键的值, 即键和值对。每个键和值对称为一个条目。映射包含唯一键。

如果你必须根据关键字搜索, 更新或删除元素, 则“地图”很有用。

Java Map层次结构

有两个用于在Java中实现Map的接口:Map和SortedMap, 以及三个类:HashMap, LinkedHashMap和TreeMap。 Java Map的层次结构如下:

Java Map层次结构

映射不允许重复的键, 但是你可以有重复的值。 HashMap和LinkedHashMap允许空键和值, 但是TreeMap不允许任何空键或值。

不能遍历Map, 因此你需要使用keySet()或entrySet()方法将其转换为Set。

描述
HashMap HashMap是Map的实现, 但不维护任何顺序。
LinkedHashMap LinkedHashMap是Map的实现。它继承了HashMap类。它保持插入顺序。
TreeMap TreeMap是Map和SortedMap的实现。它保持升序。

Map界面的有用方法

方法 描述
V put(Object key, Object value) 它用于在地图中插入一个条目。
void putAll(Map map) 用于在地图中插入指定的地图。
V putIfAbsent(K key, V value) 仅当尚未指定时, 才将指定值和指定键插入映射中。
V remove(Object key) 用于删除指定键的条目。
boolean remove(Object key, Object value) 它从映射中删除具有关联的指定键的指定值。
Set keySet() 它返回包含所有键的Set视图。
Set <Map.Entry <K, V >> entrySet() 它返回包含所有键和值的Set视图。
void clear() 用于重置地图。
V计算(K键, BiFunction <?超级K , ?超级V , ?扩展V> remappingFunction) 它用于计算指定键及其当前映射值的映射(如果没有当前映射, 则为null)。
VulateIfAbsent(K键, Function <?超级K , ?扩展V> mappingFunction) 如果指定的键尚未与值关联(或映射为null), 则使用给定的映射函数来计算其值, 除非为null, 否则将其输入此映射。
VcomputeIfPresent(K键, BiFunction <?super K, ?super V, ?extended V> remappingFunction) 如果指定键的值存在且不为空, 则用于给定键及其当前映射值的情况下计算新映射。
boolean containsValue(Object value) 如果映射中存在等于该值的值, 则此方法返回true, 否则返回false。
boolean containsKey(Object key) 如果映射中存在等于该键的某个键, 则此方法返回true, 否则返回false。
boolean equals(Object o) 用于将指定的Object与Map进行比较。
void forEach(BiConsumer <?super K, ?super V>操作) 它对映射中的每个条目执行给定的操作, 直到所有条目都已处理或该操作引发异常为止。
V get(Object key) 此方法返回包含与键关联的值的对象。
V getOrDefault(Object key, V defaultValue) 它返回指定键映射到的值, 如果映射不包含键的映射, 则返回defaultValue。
int hashCode() 它返回Map的哈希码值
boolean isEmpty() 如果映射为空, 则此方法返回true;否则, 返回false。如果包含至少一个密钥, 则返回false。
V merge(K键, V值, BiFunction <?super V, ?super V, ?extended V> remappingFunction) 如果指定的键尚未与值关联或与null关联, 请将其与给定的非null值关联。
V replace(K key, V value) 它将指定的值替换为指定的键。
boolean replace(K key, V oldValue, V newValue) 它用指定键的新值替换旧值。
void replaceAll(BiFunction <?super K, ?super V, ?extended V>函数) 它用在该条目上调用给定函数的结果替换每个条目的值, 直到处理完所有条目或该函数引发异常为止。
Collection values() 它返回映射中包含的值的集合视图。
int size() 此方法返回映射中的条目数。

Map.Entry接口

Entry是Map的子接口。因此, 将通过Map.Entry名称对其进行访问。它返回地图的集合视图, 其元素属于此类。它提供了获取关键和价值的方法。

Map.Entry接口的方法

方法 描述
K getKey() 它用于获取密钥。
V getValue() 它用于获取价值。
int hashCode() 它用于获取hashCode。
V setValue(V value) 用于用指定值替换与该条目对应的值。
boolean equals(Object o) 它用于将指定对象与其他现有对象进行比较。
静态<K扩展Comparable <?超级K>, V>比较器<Map.Entry <K, V >> compareByKey() 它返回一个比较器, 该比较器以键上的自然顺序比较对象。
静态<K, V>比较器<Map.Entry <K, V >> compareByKey(Comparator <?super K> cmp) 它返回一个比较器, 该比较器使用给定的Comparator通过键比较对象。
静态<K, V扩展了Comparable <?超级V >>比较器<Map.Entry <K, V >> compareByValue() 它返回一个比较器, 该比较器以自然顺序对值进行比较。
静态<K, V> Comparator <Map.Entry <K, V >> compareByValue(Comparator <?super V> cmp) 它返回一个比较器, 该比较器使用给定的Comparator通过值比较对象。

Java Map示例:非泛型(旧样式)

//Non-generic
import java.util.*;
public class MapExample1 {
public static void main(String[] args) {
	Map map=new HashMap();
	//Adding elements to map
	map.put(1, "Amit");
	map.put(5, "Rahul");
	map.put(2, "Jai");
	map.put(6, "Amit");
	//Traversing Map
	Set set=map.entrySet();//Converting to Set so that we can traverse
	Iterator itr=set.iterator();
	while(itr.hasNext()){
		//Converting to Map.Entry so that we can get key and value separately
		Map.Entry entry=(Map.Entry)itr.next();
		System.out.println(entry.getKey()+" "+entry.getValue());
	}
}
}

输出:

1 Amit
2 Jai
5 Rahul
6 Amit

Java Map示例:通用(新样式)

import java.util.*;
class MapExample2{
 public static void main(String args[]){
  Map<Integer, String> map=new HashMap<Integer, String>();
  map.put(100, "Amit");
  map.put(101, "Vijay");
  map.put(102, "Rahul");
  //Elements can traverse in any order
  for(Map.Entry m:map.entrySet()){
   System.out.println(m.getKey()+" "+m.getValue());
  }
 }
}

输出:

102 Rahul
100 Amit
101 Vijay

Java Map示例:compareByKey()

import java.util.*;
class MapExample3{
 public static void main(String args[]){
Map<Integer, String> map=new HashMap<Integer, String>();  	  
	  map.put(100, "Amit");  
	  map.put(101, "Vijay");  
	  map.put(102, "Rahul"); 
      //Returns a Set view of the mappings contained in this map  	  
      map.entrySet()
	  //Returns a sequential Stream with this collection as its source
	  .stream()
	  //Sorted according to the provided Comparator
	  .sorted(Map.Entry.comparingByKey())
	  //Performs an action for each element of this stream
	  .forEach(System.out::println);
 }
}

输出:

100=Amit
101=Vijay
102=Rahul

Java Map示例:降序比对

import java.util.*;
class MapExample4{
 public static void main(String args[]){
Map<Integer, String> map=new HashMap<Integer, String>();  	  
	  map.put(100, "Amit");  
	  map.put(101, "Vijay");  
	  map.put(102, "Rahul");  
      //Returns a Set view of the mappings contained in this map  
	  map.entrySet()
	  //Returns a sequential Stream with this collection as its source
	  .stream()
	  //Sorted according to the provided Comparator
	  .sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
	  //Performs an action for each element of this stream
	  .forEach(System.out::println);
 }
}

输出:

102=Rahul
101=Vijay
100=Amit

Java Map示例:compareByValue()

import java.util.*;
class MapExample5{
 public static void main(String args[]){
Map<Integer, String> map=new HashMap<Integer, String>();  	  
	  map.put(100, "Amit");  
	  map.put(101, "Vijay");  
	  map.put(102, "Rahul");  
      //Returns a Set view of the mappings contained in this map  
	  map.entrySet()
	  //Returns a sequential Stream with this collection as its source
	  .stream()
	  //Sorted according to the provided Comparator
	  .sorted(Map.Entry.comparingByValue())
	  //Performs an action for each element of this stream
	  .forEach(System.out::println);
 }
}

输出:

100=Amit
102=Rahul
101=Vijay

Java Map示例:降序比较

import java.util.*;
class MapExample6{
 public static void main(String args[]){
Map<Integer, String> map=new HashMap<Integer, String>();  	  
	  map.put(100, "Amit");  
	  map.put(101, "Vijay");  
	  map.put(102, "Rahul");  
     //Returns a Set view of the mappings contained in this map  
	 map.entrySet()
	 //Returns a sequential Stream with this collection as its source
	 .stream()
	 //Sorted according to the provided Comparator
	 .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
	 //Performs an action for each element of this stream
	 .forEach(System.out::println);
 }
}

输出:

101=Vijay
102=Rahul
100=Amit
赞(0)
未经允许不得转载:srcmini » Java Map接口

评论 抢沙发

评论前必须登录!