Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6264576

(coll) Provide a mapping framework for Sets

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 5.0
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description

      A DESCRIPTION OF THE REQUEST :
      The frequent process of creating Maps from Sets could be simplified by the introduction of a mapping framework.

      The first part of the solution is an interface that specifies any given mapping:

         interface Mapping<E, K, V> {
            K getKey(E o);
            V getValue(E o);
         }

      The interface describes for any given element of a Set what key and what value to use in the creation of a Map.

      The second part of the solution is to add a constructor to the concrete implementations of Map, as shown in the following example:

         public <E> HashMap(Set<E> s, Mapping<E, K, V> m)

      The constructor creates its map by mapping each of the elements of the set based upon the provided Mapping.

      A MappingAdaptor that returns o as the default value may be a useful shortcut for the common task of indexing a Set:

         abstract class MappingAdaptor<E, K> implements Mapping<E, K, E> {
            public E getValue(E o) { return o;}
         }

      JUSTIFICATION :
      Often classes return Sets that need to be accessed in an indexed manner by their clients. Without changing the classes behaviour (and thus coupling it to its clients), client end up having to bulid their Maps programmatically. A more OO solution would look like this:

         class NameIndexMapping extends MappingAdaptor<Person, String> {
            public String getKey(Person p) { return p.name;}
         }

         Map<String, String> nameIndexedBlackBook =
            new HashMap<String, Person>(littleBlackBook,
            new NameIndexMapping());
      ###@###.### 2005-05-03 09:52:07 GMT

        Attachments

          Activity

            People

            • Assignee:
              smarks Stuart Marks
              Reporter:
              ndcosta Nelson Dcosta (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: