美文网首页
第 30 条:优先考虑泛型方法

第 30 条:优先考虑泛型方法

作者: 综合楼 | 来源:发表于2021-05-12 21:06 被阅读0次
    优先考虑泛型方法.jpeg
    类型推导
    public class GenericStaticFactory {
            // Generic static factory method
            public static <K, V> HashMap<K, V> newHashMap() {
                   return new HashMap<K, V>();
           }
     
            public static void main(String[] args) {
                   // Parameterized type instance creation with static factory
                  Map<String, List<String>> anagrams = newHashMap ();    
           }
    }
    
    public class Union {
     
            // Generic method
            public static <E> Set<E> union(Set<E> s1, Set<E> s2) {
                  Set<E> result = new HashSet<E>(s1);
                  result.addAll(s2);
                   return result;
           }
     
            // Simple program to exercise generic method
            public static void main(String[] args) {
                  Set<String> guys = new HashSet<String>(Arrays.asList( "Tom", "Dick" ,
                                "Harry"));
                  Set<String> stooges = new HashSet<String>(Arrays.asList( "Larry", "Moe",
                                "Curly"));
                  Set<String> aflCio = union(guys, stooges);
                  System. out.println(aflCio);
           }
    }
    
    static <T> List<T> emptyList();
    List<String> listOne = Collections.emptyList();
    
    
    void processStringList(List<String> stringList) {
        // process stringList
    }
    processStringList(Collections.emptyList());
    
    泛型单例工厂
    public class GenericSingletonFactory {
            // Generic singleton factory pattern
            private static UnaryFunction<Object> IDENTITY_FUNCTION = new UnaryFunction<Object>() {
                   public Object apply(Object arg) {
                          return arg;
                  }
           };
     
            // IDENTITY_FUNCTION is stateless and its type parameter is
            // unbounded so it's safe to share one instance across all types.
            @SuppressWarnings("unchecked" )
            public static <T> UnaryFunction<T> identityFunction () {
                   return (UnaryFunction<T>) IDENTITY_FUNCTION;
           }
     
            // Sample program to exercise generic singleton
            public static void main(String[] args) {
                  String[] strings = { "jute", "hemp" , "nylon" };
                  UnaryFunction<String> sameString = identityFunction();
                   for (String s : strings)
                         System. out.println(sameString.apply(s));
     
                  Number[] numbers = { 1, 2.0, 3L };
                  UnaryFunction<Number> sameNumber = identityFunction();
                   for (Number n : numbers)
                         System. out.println(sameNumber.apply(n));
           }
    }
    
    
    public interface NumberWrapper<T extends Number> {
         public double square(T num);
    }
     
    public class GenericFactory {
     
         private static NumberWrapper<Number> numWrapper = new NumberWrapper<Number>() {
              @Override
              public double square(Number num) {
                   return num.doubleValue() * num.doubleValue();
              }
         };
     
         @SuppressWarnings("unchecked")
         public static <T extends Number> NumberWrapper<T> getWrapperInstance() {
              return (NumberWrapper<T>) numWrapper;
         }
     
         public static void main(String[] args) {
              NumberWrapper<Integer> integerWrapper = GenericFactory.getWrapperInstance();
              System.out.println(integerWrapper.square(2));
             
              NumberWrapper<Double> doubleWrapper = GenericFactory.getWrapperInstance();
              System.out.println(doubleWrapper.square(0.05));
         }
     
    }
    
    
    递归类型限制
       public static  <T extends Comparable<T>>  T max(List<T> list) {
            Iterator<T> i = list.iterator();
            T result = i.next();
            while(i.hasNext()) {
                T t = i.next();
                if(t.compareTo(result) > 0) {
                    result = t;
                }
            }
            return result;
        }
    

    相关文章

      网友评论

          本文标题:第 30 条:优先考虑泛型方法

          本文链接:https://www.haomeiwen.com/subject/kzijdltx.html