Map是java中的接口,Map.Entry是Map的一个内部接口。
Map提供了一些常用方法,如keySet()、entrySet()等方法,keySet()方法返回值是Map中key值的集合;entrySet()的返回值也是返回一个Set集合,此集合的类型为Map.Entry。
Map.Entry是Map声明的一个内部接口,此接口为泛型,定义为Entry
主要作用:降低获取value的复杂度。
Map.Entry使用
你是否已经对每次从Map中取得关键字然后再取得相应的值感觉厌倦?使用Map.Entry类,你可以得到在同一时间得到所有的信息。标准的Map访问方法如下:
Set keys = map.keySet( );
if(keys != null) {
Iterator iterator = keys.iterator( );
while(iterator.hasNext( )) {
Object key = iterator.next( );
Object value = map.get(key);
;....
;}
}
然后,这个方法有一个问题。从Map中取得关键字之后,我们必须每次重复返回到Map中取得相对的值,这是很繁琐和费时的。
幸运的是,这里有一个更加简单的途径。Map类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集。 接着,Map.Entry类提供了一个getKey()方法和一个getValue()方法,因此,上面的代码可以被组织得更符合逻辑。举例如下:
Set entries = map.entrySet( );
if(entries != null) {
Iterator iterator = entries.iterator( );
while(iterator.hasNext( )) {
Map.Entry entry =iterator.next( );
Object key = entry.getKey( );
Object value = entry.getValue();
;....
}
}
尽管增加了一行代码,我们却省略了许多对Map不必要的“get”调用。同时,提供给开发人员一个同时保持了关键字和其对应的值的类。Map.Entry同时也提供了一个setValue()方法,程序员可以使用它修改map里面的值。
package com.hggggc.container;
import java.util.*;
/**
* Created by jd3 on 2019-07-03.
*/
public class HgcMap implements Map {
public static void main(String[] args) {
HgcMap hgcMap = new HgcMap();
hgcMap.put(1,1);
hgcMap.put("asd","211");
Set entrySet = hgcMap.entrySet();
System.out.println(hgcMap);
Data entry = (Data) entrySet.iterator().next();
entry.setKey("123123");
System.out.println(entry);
}
private final static int slot = 997;
private LinkedList[] bucket = new LinkedList[slot];
int size = 0;
class Data implements Entry, Comparable {
Object key, value;
public Data(Object key, Object value) {
this.key = key;
this.value = value;
}
@Override
public int compareTo(Object o) {
Data data = (Data) o;
return ((Comparable) key).compareTo(data.key);
}
@Override
public Object getKey() {
return key;
}
@Override
public Object getValue() {
return value;
}
@Override
public Object setValue(Object value) {
Object result = this.value;
this.value = value;
return result;
}
@Override
public boolean equals(Object obj) {
Data data = (Data) obj;
return this.key.equals(data.key);
}
@Override
public String toString() {
return "[" + key + ":" + value + "]";
}
public void setKey(Object key) {
this.key = key;
}
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public boolean containsKey(Object key) {
boolean found = false;
int index = key.hashCode() % slot;
if(index < 0 )index= -index;
if(bucket[index]==null) return found;
Iterator iterator = bucket[index].listIterator();
while(iterator.hasNext()){
if(iterator.next().equals((new Data(key,null)))) return true;
}
return false;
}
@Override
public boolean containsValue(Object value) {
for(int i = 0 ; i < slot;i++){
if(bucket[i]!=null){
Iterator iterator = bucket[i].listIterator();
while(iterator.hasNext()){
if(((Data)iterator.next()).getValue().equals(value)) return true;
}
}
}
return false;
}
@Override
public Object get(Object key) {
Data getData = new Data(key, null);
int index = key.hashCode() % slot;
if (index < 0) index = -index;
if (bucket[index] == null) return null;
LinkedList linkedList = bucket[index];
ListIterator iterator = linkedList.listIterator();
while (iterator.hasNext()) {
Data data = (Data) iterator.next();
if (data.equals(getData)) {
return data.value;
}
}
return null;
}
@Override
public Object put(Object key, Object value) {
Object result = new Object();
Data putdata = new Data(key, value);
int index = key.hashCode() % 997;//在哪一个槽
if (index < 0) index = -index;//不能是负的
if (bucket[index] == null) bucket[index] = new LinkedList();
LinkedList linkedList = bucket[index];
ListIterator iterator = linkedList.listIterator();
boolean found = false;
while (iterator.hasNext()) {
Data data = (Data) iterator.next();
if (data.equals(putdata)) {
found = true;
result = data.value;
iterator.set(putdata);
}
}
if (!found) {
bucket[index].add(putdata);
size++;
}
return result;
}
@Override
public Object remove(Object key) {
Data removeData = new Data(key, null);
int index = key.hashCode() % slot;
if (index < 0) index = -index;
if (bucket[index] == null) return null;
LinkedList linkedList = bucket[index];
size--;
return linkedList.remove(removeData);
}
@Override
public void putAll(Map m) {
Set set = m.entrySet();
for(Object o : set){
Map.Entry oo = (Map.Entry)o;
put(oo.getKey(),oo.getValue());
}
}
@Override
public void clear() {
for(Object key : keySet()){
remove(key);
}
size=0;
}
@Override
public Set keySet() {
Set set = new HashSet();
for(int i = 0 ; i< slot ;i++){
if(bucket[i]!=null){
Iterator iterator = bucket[i].listIterator();
while(iterator.hasNext()){
set.add(((Data)iterator.next()).getKey());
}
}
}
return set;
}
@Override
public Collection values() {
List list = new ArrayList();
for(int i = 0 ; i< slot ;i++){
if(bucket[i]!=null){
Iterator iterator = bucket[i].listIterator();
while(iterator.hasNext()){
list.add(((Data)iterator.next()).getValue());
}
}
}
return list;
}
@Override
public Set entrySet() {
Set set = new HashSet();
for(int i = 0 ; i< slot ;i++){
if(bucket[i]!=null){
Iterator iterator = bucket[i].listIterator();
while(iterator.hasNext()){
set.add(iterator.next());
}
}
}
return set;
}
@Override
public int hashCode() {
int j = 0;
for (int i = 0; i < slot; i++) {
if (bucket[i] != null) {
Iterator iterator = bucket[i].iterator();
Data data = (Data) iterator.next();
j = j + data.getKey().hashCode();
}
}
return j;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("[").append("\n");
for(int i = 0 ; i < slot;i++){
if(bucket[i]!=null){
Iterator iterator = bucket[i].listIterator();
while(iterator.hasNext()){
Data data = (Data)iterator.next();
builder.append(data.getKey()).append(":").append(data.getValue()).append("\n");
}
}
}
builder.append("]");
return builder.toString();
}
}