package com.kael;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
/**
*
*
* @Title:SetAndMap
* @Description: Map和Set 装换
* @Copyright:Copyright (c) 2012
* @Date:2012-7-20
* @author feilong.li
*/
public class SetAndMap {
public static void main(String[] args) {
SetMap<String, Integer> scores = new SetMap<String, Integer>();
Map<String, Integer> map = new HashMap<String, Integer>();
scores.put("C", 90);
scores.put("Java", 100);
scores.put("Oracle", 98);
map.put("C", 90);
map.put("Java", 100);
map.put("Oracle", 98);
System.out.println(scores);
System.out.println("scores.size() = " + scores.size());
System.out.println(scores.containsKey("Java"));
System.out.println(scores.containsValue(100));
System.out.println(scores.getValue("C"));
System.out.println(scores.removeEntry("C"));
System.out.println(scores);
scores.clear();
System.out.println(scores);
scores.putAll(map);
System.out.println(scores);
}
}
class SimpleEntry<K,V> implements Map.Entry<K, V> ,Serializable {
/**
*
*/
private static final long serialVersionUID = -5468059496835899481L;
private K key;
private V value;
public SimpleEntry (K key,V value) {
this.key = key;
this.value = value;
}
public SimpleEntry(Map.Entry<? extends K, ? extends V> entry) {
this.key = entry.getKey();
this.value = entry.getValue();
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
V oldValue = this.value;
this.value = value;
return oldValue;
}
@Override
public int hashCode() {
return key == null ? 0 : key.hashCode();
}
@Override
public boolean equals(Object obj) {
if(obj == this) {
return true;
}
if(obj.getClass() == this.getClass()) {
SimpleEntry<K, V> simpleEntry = this;
return simpleEntry.getKey().equals(getKey());
}
return false;
}
@Override
public String toString() {
return key +"="+ value;
}
}
/*
* 定义自己的Map
*/
class SetMap<K, V> extends HashSet<SimpleEntry<K, V>> {
private static final long serialVersionUID = -9050259671066619118L;
/**
* 清空
*/
public void clear() {
super.clear();
}
/**
* 判断是否包含某个key
*/
public boolean containsKey(K key) {
return super.contains(new SimpleEntry<K, V>(key,null));
}
/**
* 判断是否包含某一个value
*/
public boolean containsValue(V v) {
for(SimpleEntry<K, V> entry : this) {
if(entry.getValue().equals(v)) {
return true;
}
}
return false;
}
/**
* 指定key返回value
*/
public V getValue(K key) {
for(SimpleEntry<K, V> entry : this) {
if(entry.getKey().equals(key)) {
return entry.getValue();
}
}
return null;
}
/**
* 将指定key-value放入Map
*/
public V put(K key,V value) {
add(new SimpleEntry<K, V>(key,value));
return value;
}
/**
* 将另一个map的全部key-value对放入这个Map中
*/
public void putAll(Map<? extends K, ? extends V> map) {
for(K key : map.keySet()) {
add(new SimpleEntry<K, V>(key, map.get(key)));
}
}
/**
* 根据key删除指定的key-value
*/
public V removeEntry(K key) {
for(Iterator<SimpleEntry<K, V>> iterator = this.iterator();iterator.hasNext();) {
SimpleEntry<K, V> entry = iterator.next();
if(entry.getKey().equals(key)) {
iterator.remove();
return entry.getValue();
}
}
return null;
}
/**
* 获取这个Map中包含多少对key-value
*/
public int size() {
return super.size();
}
}
输出结果:
[C=90, Oracle=98, Java=100]
scores.size() = 3
true
true
90
90
[Oracle=98, Java=100]
[]
[C=90, Oracle=98, Java=100]