2012-11-23 4 views

risposta

6

forse sarebbe illuminante per guardare l'attuazione:

private static class SetFromMap<E> extends AbstractSet<E> 
    implements Set<E>, Serializable 
{ 
    private final Map<E, Boolean> m; // The backing map 
    private transient Set<E> s;  // Its keySet 

    SetFromMap(Map<E, Boolean> map) { 
     if (!map.isEmpty()) 
      throw new IllegalArgumentException("Map is non-empty"); 
     m = map; 
     s = map.keySet(); 
    } 

    public void clear()    {  m.clear(); } 
    public int size()     { return m.size(); } 
    public boolean isEmpty()   { return m.isEmpty(); } 
    public boolean contains(Object o) { return m.containsKey(o); } 
    public boolean remove(Object o) { return m.remove(o) != null; } 
    public boolean add(E e) { return m.put(e, Boolean.TRUE) == null; } 
    public Iterator<E> iterator()  { return s.iterator(); } 
    public Object[] toArray()   { return s.toArray(); } 
    public <T> T[] toArray(T[] a)  { return s.toArray(a); } 
    public String toString()   { return s.toString(); } 
    public int hashCode()    { return s.hashCode(); } 
    public boolean equals(Object o) { return o == this || s.equals(o); } 
    public boolean containsAll(Collection<?> c) {return s.containsAll(c);} 
    public boolean removeAll(Collection<?> c) {return s.removeAll(c);} 
    public boolean retainAll(Collection<?> c) {return s.retainAll(c);} 
    // addAll is the only inherited implementation 

    private static final long serialVersionUID = 2454657854757543876L; 

    private void readObject(java.io.ObjectInputStream stream) 
     throws IOException, ClassNotFoundException 
    { 
     stream.defaultReadObject(); 
     s = m.keySet(); 
    } 
} 

Edit - spiegazione aggiunto:

La mappa che fornite viene utilizzato come campo m in questo oggetto.

Quando si aggiunge un elemento e all'insieme, viene aggiunta una voce e -> true alla mappa.

public boolean add(E e) { return m.put(e, Boolean.TRUE) == null; } 

Quindi questa classe trasforma il Map in un oggetto che si comporta come un Set semplicemente ignorando i valori che le cose sono mappati, e usando solo i tasti.

+0

per favore, aggiungere qualche spiegazione –

+0

La mia domanda principale è il backup set e mappare .. Che cosa si intende per loro e un semplice esempio potrebbe essere utile .. – Heggi

+0

Grazie a @ Christopher Martin ... – Heggi

2

Ho appena fatto un codice di esempio per voi

HashMap<String, Boolean> map = new HashMap<String, Boolean>(); 

Set<String> set = Collections.newSetFromMap(map); 

System.out.println(set); 

for (int i = 0; i < 10; i++) 
    map.put("" + i, i % 2 == 0); 

System.out.println(map); 

System.out.println(set); 

e l'uscita

[] 
{3=false, 2=true, 1=false, 0=true, 7=false, 6=true, 5=false, 4=true, 9=false, 8=true} 
[3, 2, 1, 0, 7, 6, 5, 4, 9, 8] 
+0

Da Javadoc: Non è necessario utilizzare questo metodo su un'implementazione Mappa che ha già un'implementazione Set corrispondente (come HashMap o TreeMap). – Jasper