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

(coll) Provide a subSet() capability for non-sorted sets

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Won't Fix
    • Affects Version/s: 5.0
    • Fix Version/s: 8
    • Component/s: core-libs
    • Labels:

      Description

      A DESCRIPTION OF THE REQUEST :
      Introduce an interface "java.util.SetFilter<E>", with a single method "boolean include(E o)".

      Add to the interface "Set", the method "Set<E> subSet(SetFilter<E> f)". Borrowing liberally from the existing subSet() method javadoc:

      Returns a view of the portion of this set whose elements are satisfied by the SetFilter. (If no elements are satisfied by the SetFilter, the returned set is empty.) The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional Set operations.

      The set returned by this method will throw an IllegalArgumentException if the user attempts to insert an element that is not satisfied by the SetFilter.

      If the Set is a SortedSet, the returned Set is also a SortedSet, has the same Comparator, and as a result maintains the original ordering.

      JUSTIFICATION :
      In my work every week I encounter code that goes something like this:

         SortedSet<String> shortNames = new TreeSet<String>();
         for(String name: names) {
           if(name.length() < 4) {
               shortNames.add(name);
            }
         }
         System.out.println("short names are " + shortNames);

      It would seem more OO to look like this:

         static class ShortNamesSetFilter implements SetFilter<String> {
            public boolean filter(String name) {
               return name.length() < 4;
           }
         }

         Set<String> shortNames = names.subSet(new ShortNamesSetFilter());
         System.out.println("short names are " + shortNames);

      Similarly you could chain filters, and parameterise them.

         Set<String> shortNamesStartingWithVowels =
            names.subSet(new ShortNamesSetFilter(6)).
            subSet(new BeginsWithVowelSetFilter());

      Then you could remove them all from the underlying list with a single statement:

        shortNamesStartingWithVowels.clear();
      ###@###.### 2005-03-22 22:51:55 GMT

        Attachments

          Activity

            People

            • Assignee:
              mduigou Mike Duigou
              Reporter:
              gmanwanisunw Girish Manwani (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: