<p>Lasty, I tried to implements an hybrid structure in Java, something that looks like:</p>

<pre><code>public class MapOfSet&lt;K, V extends HasKey&lt;K&gt;&gt; implements Set&lt;V&gt;, Map&lt;K, Set&lt;V&gt;&gt;
</code></pre>

<p>Where HasKey is the following interface:</p>

<pre><code>public interface HasKey&lt;K&gt; {
    public K getKey();
}
</code></pre>

<p>Unfortunately, there are some conflicts between methos signature of the Set interface and the Map interface in Java. I've finally chosen to implements only the Set interface and to add the Map method without implementing this interface.</p>

<p>Do you see a nicer solution?</p>

<p>In response to the first comments, here is my goal:</p>

<blockquote>
  <p>Have a set structure and be able to efficiently access to a subset of values of this set, corresponding to a given key value.
  At the beginning I instantiated a map and a set, but I tried to joined the two structures to optimize performances.</p>
</blockquote>

[{'answer_id': 70760, 'author': 'Avi', 'author_id': 1605, 'author_profile': 'https://Stackoverflow.com/users/1605', 'pm_score': 0, 'selected': False, 'text': '<p>I would say that something that is meant to be sometimes used as a Map and sometimes as a Set should implement Map, since that can be viewed as a set of keys or values as well as a mapping between keys and values. That is what the Map.containsKey() and Map.containsValue() methods are for.</p>\n'}, {'answer_id': 70772, 'author': 'Hank Gay', 'author_id': 4203, 'author_profile': 'https://Stackoverflow.com/users/4203', 'pm_score': 2, 'selected': False, 'text': '<p>What are you trying to accomplish? <code>Map</code> already exposes its keys as a <code>Set</code> via its [keySet()](<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#keySet())" rel="nofollow noreferrer">http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#keySet())</a> method. If you want a reliable iteratior order, there\'s <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedHashMap.html" rel="nofollow noreferrer">LinkedHashMap</a> and <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/TreeMap.html" rel="nofollow noreferrer">TreeMap</a>.</p>\n\n<p>UPDATE: If you want to ensure that a value has only been inserted once, you can extend one of the classes I mentioned above to create something like a <code>SingleEntryMap</code> and override the implementation of <code>put(K key, V value)</code> to do a uniqueness check and throw an Exception when the value has already been inserted.</p>\n\n<p>UPDATE: Will something like this work? (I don\'t have my editor up, so this may not compile)</p>\n\n<pre><code>public final class KeyedSets&lt;K, V&gt; implements Map&lt;K,Set&lt;V&gt;&gt; {\n    private final Map&lt;K, Set&lt;V&gt;&gt; internalMap = new TreeMap&lt;K, Set&lt;V&gt;&gt;;\n    // delegate methods go here\n    public Set&lt;V&gt; getSortedSuperset() {\n        final Set&lt;V&gt; superset = new TreeSet&lt;V&gt;();\n        for (final Map.Entry&lt;K, V&gt; entry : internalMap.entrySet()) {\n            superset.addAll(entry.getValue());\n        }\n        return superset;\n    }\n}\n</code></pre>\n'}, {'answer_id': 70962, 'author': 'jrudolph', 'author_id': 7647, 'author_profile': 'https://Stackoverflow.com/users/7647', 'pm_score': 2, 'selected': True, 'text': '<p>Perhaps you could add more information which operations do you really want. I guess you want to create a set which automatically groups their elements by a key, right? The question is which operations do you want to be able to have? How are elements added to the Set? Can elements be deleted by removing them from a grouped view? My proposal would be an interface like that:</p>\n\n<pre><code>public interface GroupedSet&lt;K, V extends HasKey&lt;K&gt;&gt; extends Set&lt;V&gt;{\n    Set&lt;V&gt; havingKey(K k);\n}\n</code></pre>\n\n<p>If you want to be able to use the Set as map you can add another method</p>\n\n<pre><code>Map&lt;K,Set&lt;V&gt;&gt; asMap();\n</code></pre>\n\n<p>That avoids the use of multiple interface inheritance and the resulting problems.</p>\n'}]
['https://Stackoverflow.com/questions/70732', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1730/']