Friday 1 July 2011

Collection classifier


Collection classifier

import java.util.*;

    public class CollectionClassifier2 {
         public static String classify(Collection c) {
         return (c instanceof Set ? "Set" :
         (c instanceof List ? "List" : "Unknown Collection"));
     }
  
         public static void main(String[] args) {
             Collection[] tests = new Collection[] {
             new HashSet(), // A Set
             new ArrayList(), // A List
             new HashMap().values() // Neither Set nor List
         };
      
         for (int i = 0; i < tests.length; i++)
         System.out.println(classify(tests[i]));
     }
}

People who read this post also read :



No comments:

Post a Comment