2012-11-23 3 views

Antwort

6

Vielleicht wäre es an der Umsetzung aussehen wird beleuchtet:

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

Bearbeiten - hinzugefügt Erklärung:

Die Karte, die Sie zur Verfügung gestellten als m Feld in diesem Objekt verwendet wird. Wenn Sie dem Set ein Element e hinzufügen, fügt es der Karte einen Eintrag e -> true hinzu.

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

So diese Klasse verwandelt Ihr Map in ein Objekt, das einfach wie ein Set verhält, indem die Werte ignorieren, die Dinge abgebildet werden, und nur die Tasten.

+0

Bitte fügen Sie eine Erklärung –

+0

Meine Hauptfrage Satz ist die Sicherung und Karte .. Was meinst du mit ihnen und ein einfaches Beispiel wäre nützlich. – Heggi

+0

Dank @Christopher Martin ... – Heggi

2

Ich habe gerade ein Beispiel-Code für Sie

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

und der Ausgang

[] 
{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

Von der Javadoc: Es ist nicht notwendig, diese Methode für eine Map-Implementierung, die bereits eine entsprechende Set-Implementierung (wie HashMap oder TreeMap) hat. – Jasper

Verwandte Themen