java程序:set改造成map

逻辑:

      set是无序不重复数据元素的集合。

      map是另一种set,如果将<key,value>看成一个整体的话,其实就是set。在map中,若用map的keyset()方法将key提取出来,便构成了一个set集合。

      所以,就定义一个整体SimpleEntry<K,V>作为元素存入set。

代码:

/*

*SimpleEntry<K,V>作为map对象的存储元素

*/

class SimpleEntry<K, V>
  implements Map.Entry<K, V>, Serializable
{

// map中的key不可变
  private final K key;
  private V value;

  public SimpleEntry(K paramK, V paramV)
  {
    this.key = paramK;
    this.value = paramV;
  }

 

public SimpleEntry(Map.Entry<? extends K, ? extends V> paramEntry)
  {
    this.key = paramEntry.getKey();
    this.value = paramEntry.getValue();
  }

  public K getKey()
  {
    return this.key;
  }

  public V getValue()
  {
    return this.value;
  }

  public V setValue(V paramV)
  {
    Object localObject = this.value;
    this.value = paramV;
    return localObject;
  }

  public boolean equals(Object paramObject)
  {
    if (paramObject == this)
    {
      return true;
    }
    if (paramObject.getClass() == SimpleEntry.class)
    {
      SimpleEntry localSimpleEntry = (SimpleEntry)paramObject;
      return localSimpleEntry.getKey().equals(getKey());
    }
    return false;
  }

  public int hashCode()
  {
    return this.key == null ? 0 : this.key.hashCode();
  }

  public String toString()
  {
    return this.key + "=" + this.value;
  }
}

 

/*

*Set2Map<K, V>继承自hashset,底层是利用hashset来存储,但存储的是<K,V>这样的二元组,所以可以看成是一个map

*/

public class Set2Map<K, V> extends HashSet<SimpleEntry<K, V>>
{
  public void clear()
  {
    super.clear();
  }

  public boolean containsKey(K paramK)
  {
    return super.contains(new SimpleEntry(paramK, null));
  }

  boolean containsValue(Object paramObject)
  {
    for (SimpleEntry localSimpleEntry : this)
    {
      if (localSimpleEntry.getValue().equals(paramObject))
      {
        return true;
      }
    }
    return false;
  }

  public V get(Object paramObject)
  {
    for (SimpleEntry localSimpleEntry : this)
    {
      if (localSimpleEntry.getKey().equals(paramObject))
      {
        return localSimpleEntry.getValue();
      }
    }
    return null;
  }

  public V put(K paramK, V paramV)
  {
    add(new SimpleEntry(paramK, paramV));
    return paramV;
  }

 

//内部用了迭代器实现

  public void putAll(Map<? extends K, ? extends V> paramMap)
  {
    for (Iterator localIterator = paramMap.keySet().iterator(); localIterator.hasNext(); ) { Object localObject = localIterator.next();

      add(new SimpleEntry(localObject, paramMap.get(localObject)));
    }
  }

//内部用了迭代器实现

  public V removeEntry(Object paramObject)
  {
    Iterator localIterator = iterator();
    while (localIterator.hasNext())
    {
      SimpleEntry localSimpleEntry = (SimpleEntry)localIterator.next();
      if (localSimpleEntry.getKey().equals(paramObject))
      {
        Object localObject = localSimpleEntry.getValue();
        localIterator.remove();
        return localObject;
      }
    }
    return null;
  }

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

 

测试程序:

public class Set2MapTest
{
  public static void main(String[] paramArrayOfString)
  {
    Set2Map localSet2Map = new Set2Map();

    localSet2Map.put("语文", Integer.valueOf(89));
    localSet2Map.put("数学", Integer.valueOf(83));
    localSet2Map.put("英文", Integer.valueOf(80));
    System.out.println(localSet2Map);

    System.out.println(localSet2Map.size());
    localSet2Map.removeEntry("数学");
    System.out.println("删除key为"数学"的Entry之后:" + localSet2Map);

    System.out.println("语文成绩:" + localSet2Map.get("语文"));

    System.out.println("是否包含"英文"key :" + localSet2Map.containsKey("英文"));

    System.out.println("是否包含 82 value :" + localSet2Map.containsValue(Integer.valueOf(82)));

    localSet2Map.clear();
    System.out.println("执行clear()方法之后的集合:" + localSet2Map);
  }
}

 

 

 

 

 

 

 

 

 

reference:

       1. 《疯狂java,突破程序员基本功的16课》 李刚。

原文地址:https://www.cnblogs.com/haore147/p/4214803.html