简单实现Set To Map_java
2017-12-13 16:41
302 查看
简单实现Set To Map (就那么不经意)
package cn.limbo.collection; import java.util.HashSet; import java.util.Iterator; import java.util.Map; /** * Created by lhh on 2017/12/13. */ public class SimpleEntry<K, V> implements Map.Entry<K, V>, java.io.Serializable { private final K key;//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() {//获取key return key; } @Override public V getValue() { return value; } //改变key-value对应的value的值 @Override public V setValue(V value) { V oldValue = this.value; this.value = value; return oldValue; } //根据key判断value是否相等 public boolean equals(Object o) { if (o == this) { return true; } if (o.getClass() == SimpleEntry.class) { SimpleEntry se = (SimpleEntry) o; return se.getKey().equals(getKey()); } return false; } //根据key计算对应的hashcode public int hashCode() { return key == null ? 0 : key.hashCode(); } @Override public String toString() { return key + "=" + value; } } class Set2Map<K, V> extends HashSet<SimpleEntr 4000 y<K, V>> { public void clear() { super.clear(); } //判断是否包含某个key public boolean containsKey(K key) { return super.contains( new SimpleEntry<K, V>(key, null)); } //判断是否包含某个value boolean containsValue(Object value) { for (SimpleEntry<K, V> se : this) { if (se.getValue().equals(value)) { return true; } } return false; } //根据指定key取出对应的value public V get(Object key) { for (SimpleEntry<K, V> se : this) { if (se.getKey().equals(key)) { return se.getValue(); } } return null; } //将指定的key-value放入到集合 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> m) { for (K key : m.keySet()) { add(new SimpleEntry<K, V>(key, m.get(key))); } } //根据key删除指定key-value对 public V removeEntry(Object key) { for (Iterator<SimpleEntry<K, V>> it = this.iterator(); it.hasNext(); ) { SimpleEntry<K, V> en = it.next(); if (en.getKey().equals(key)) { V v = en.getValue(); it.remove(); return v; } } return null; } //获取key-value对 public int size() { return super.size(); } } //编写测试类 class Set2MapTest { public static void main(String[] args) { Set2Map<String, Integer> scores = new Set2Map<>(); //把key-value对放到该集合 scores.put("语文", 70); scores.put("数学", 95); scores.put("英语", 80); System.out.println("scores = " + scores); //访问Map System.out.println("scores.get(\"语文\") = " + scores.get("语文")); System.out.println(scores.size()); scores.removeEntry("数学"); System.out.println(scores.size()); System.out.println(scores.containsKey("数学")); System.out.println(scores.containsValue(80)); scores.clear(); System.out.println("清除之后的map:[]scores = " + scores); } }
相关文章推荐
- JAVA类集框架 List、Set、Iterator、Map的简单理解
- JAVA简单理解累计框架 List、Set、Iterator、Map
- java的Set、Map、Deque集合;实现排序
- Map to bean的一个java实现
- Java中,简单的List转Set实现
- java4Android(20):类集框架->Set/Map/List->ArrayList简单实例
- java基础之对List,Map,Set等集合键值对的简单认识
- java中实现list或set转map的方法
- Map实现java缓存机制的简单实例
- Java中List Set Map 区别简单分析
- 简单组合java.util.Map<K,V>实现Map<K,P,V>
- Java二维数组实现简单Map(梁健-原创)
- 搞笑的java的list,set,map的继承关系(实现关系)
- 简单组合java.util.Map<K,V>实现Map<K,P,V>
- java中set的交集、差集、并集的简单实现
- Java中集合框架,Collection接口、Set接口、List接口、Map接口,已经常用的它们的实现类,简单的JDK源码分析底层实现
- Java中的Map、Set、List及其实现
- 简单组合java.util.Map<K,V>实现Map<K,P,V>
- JAVA绝对干货——List、Set、Map并发数据结构对比实现
- 使用js简单实现javaMap