天天看點

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; 

    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; 

      } 

  public V get(Object paramObject) 

      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(); 

        Object localObject = localSimpleEntry.getValue(); 

        localIterator.remove(); 

        return localObject; 

  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); 

 本文轉自二郎三郎部落格園部落格,原文連結:http://www.cnblogs.com/haore147/p/4214803.html,如需轉載請自行聯系原作者