zoukankan      html  css  js  c++  java
  • 【Effective Java】7、优先考虑泛型方法

    package cn.xf.cp.ch02.item27;
    
    import java.util.HashSet;
    import java.util.Set;
    
    public class Union
    {
        /**
         * 这个方法就会有警告
         * @param s1
         * @param s2
         * @return
         */
        public static Set union1(Set s1, Set s2)
        {
            Set result = new HashSet(s1);
            result.addAll(s2);
            return result;
        }
        
        /**
         * 这里使用泛型就是安全没有警告的
         * @param s1
         * @param s2
         * @return
         */
        public static <E> Set<E> union(Set<E> s1, Set<E> s2)
        {
            Set<E> result = new HashSet<E>(s1);
            result.addAll(s2);
            return result;
        }
        
    }

    但是有的时候,我们发现使用泛型的时候,在调用构造器的时候要明确泛型的类型,这样书写很麻烦

    这里可以使用一个泛型静态方法,利用泛型的类型推导

    package cn.xf.cp.ch02.item27;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class GenericStaticFactory
    {
        public static <K, V> HashMap<K, V> newHashMap()
        {
            return new HashMap<K, V>();
        }
    
        public static void main(String[] args)
        {
            //这里创建对象的时候,就会根据前面map中的String和List<String>自动转换
            Map<String, List<String>> anagrams = newHashMap();
        }
    }

    关于泛型单利工厂的实现

    package cn.xf.cp.ch02.item27;
    
    public class GenericSingletonFactory
    {
        //先创建object对象,暂时替代T对象
        private static UnaryFunction<Object> IDENTITY_FUNCTION = new UnaryFunction<Object>()
        {
            public Object apply(Object arg)
            {
                return arg;
            }
        };
        
        //根据T,吧IDENTITY_FUNCTION转换为相应的类型,由于IDENTITY_FUNCTION返回未被修改的object类型参数,所以下面类型转换时安全的
        @SuppressWarnings("unchecked")
        public static <T> UnaryFunction<T> identityFunction()
        {
            return (UnaryFunction<T>) IDENTITY_FUNCTION;
        }
        
        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();
            //这里用来判断是否真的实现了单例
            Object t1 = sameString;
            Object t2 = sameNumber;
            
            if(t1 == t2)
            {
                System.out.println("相同引用");
            }
            
            String s1 = "123456";
            String s2 = new String("123456");
            
            if(s1.equals(s2))
                System.out.println("相同内容");
            
            
            for (Number n : numbers)
                System.out.println(sameNumber.apply(n));
        }
    }

    显示结果:

  • 相关阅读:
    JVM体系结构之五:本地方法栈
    netty中的ByteBuf
    AtomicLong和LongAdder的区别
    Python介绍
    netty支持的协议
    spring扩展点之四:Spring Aware容器感知技术,BeanNameAware和BeanFactoryAware接口,springboot中的EnvironmentAware
    Netty组件
    《深入Java虚拟机学习笔记》- 第9章 垃圾收集
    《深入Java虚拟机学习笔记》- 第8章 连接模型
    netty中的EventLoop和EventLoopGroup
  • 原文地址:https://www.cnblogs.com/cutter-point/p/5883279.html
Copyright © 2011-2022 走看看