Set 到 Map的扩展--- 继承set

来源:互联网 发布:淘宝天天特价报名费用 编辑:程序博客网 时间:2024/05/15 23:54
public class Set2MapTest<K, V> extends HashSet<TestSimpleEntry<K,V>>{


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Set2MapTest<String,Integer> map = new Set2MapTest<String, Integer>();
map.put("测试", 100);
int a= map.put("map",50);
int b = map.put("数学", 88);
map.put("数学1", 88);
System.out.println(map.put("计算机", 818)+":位置");
 
System.out.println(map.get("map"));
System.out.println(map.containsValue(50)+",Map大小是:"+map.size());

Set2MapTest<String,Integer> map1 = new Set2MapTest<String, Integer>();
System.out.println(map1.hashCode());
  List ary = new ArrayList(2);
  ary.add(1);
  ary.add(2);
  ary.add(3);
  System.out.println("List的长度:"+ary.isEmpty());
map.removeEntry("map");
System.out.println(map.get("map"));
System.out.println(map.containsValue(50));
System.out.println(map.get("测试"));
System.out.println(map.containsValue(100));
map.clear();
System.out.println(map.get("测试"));
System.out.println(map.containsValue(100));
}

public void clear(){
super.clear();
}

//判断是否包含某个 key
// public boolean containsKey(K key){
// return super.containsKey(new TestSimpleEntry<K, V>( key, null));
// }

public boolean containsValue(Object value){
for(TestSimpleEntry<K, V> s:this){
if(s.getValue().equals(value)){
return true;
}
}
return false;
}

//根据指定 key 获取value
public V get(Object key){
for(TestSimpleEntry<K, V> s:this){
if(s.getKey().equals(key)){
return (V) s.getValue();
}
}
return null;

}
//将制定key -value 放入集合
public V put(K key,V value){
add(new TestSimpleEntry<K, V>(key, value));
return value;
}
//另一个map 的 k-v放入该map
public void putAll(Map< K, V> map){
for(K key:map.keySet()){
add(new TestSimpleEntry<K, V>(key, map.get(key)));
}
}

//根据指定key 删除 key-value
public V removeEntry(Object key){
for(Iterator<TestSimpleEntry<K,V>> it = this.iterator();it.hasNext();){
TestSimpleEntry<K, V> simp = it.next();
if(simp.getKey().equals(key)){
V v = simp.getValue();
it.remove();
return v;
}
}
return null;
}

public int size(){
return super.size();
}



}
0 0
原创粉丝点击