zoukankan      html  css  js  c++  java
  • Effective Java 27 Favor generic methods

    Static utility methods are particularly good candidates for generification.

       

    The type parameter list, which declares the type parameter, goes between the method's modifiers and its return type.

       

    // 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;

    }

       

    To eliminate redundancy of the type declaration of generic constructor write a generic static factory method corresponding to each constructor that you want to use.

    class HashMap<K, V>{

    // Generic static factory method

    public static <K,V> HashMap<K,V> newHashMap() {

    return new HashMap<K,V>();

    }

       

    Public static int main(String[] args){

    // Parameterized type instance creation with static factory

    Map<String, List<String>> anagrams = newHashMap();

    }

    }

       

    Generic singleton factory pattern

    public interface UnaryFunction<T> {

    T apply(T arg);

    }

       

    // 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;

    }

       

    Recursive type bound

    // Using a recursive type bound to express mutual comparability

    public static <T extends Comparable<T>> T max(List<T> list) {...}

       

    // Returns the maximum value in a list - uses recursive type bound

    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;

    }

    Summary

    Generic methods, like generic types, are safer and easier to use than methods that require their clients to cast input parameters and return values. Like types, you should make sure that your new methods can be used without casts, which will often mean making them generic. And like types, you should generify your existing methods to make life easier for new users without breaking existing clients (Item 23).

       

    作者:小郝
    出处:http://www.cnblogs.com/haokaibo/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Lua获取当前时间
    标准库
    Cocos2d-x retain和release倒底怎么玩?
    lua 中处理cocos2dx 的button 事件
    探讨把一个元素从它所在的div 拖动到另一个div内的实现方法
    从 ie10浏览器下Symbol 未定义的问题 探索vue项目如何兼容ie低版本浏览器(ie9, ie10, ie 11 )
    setTimeout里无法调用鼠标事件的event
    浅谈HTTP缓存以及后端,前端如何具体实现HTTP缓存
    window7电脑git设置快捷命令
    从获取点击事件根元素谈 target和currentTarget
  • 原文地址:https://www.cnblogs.com/haokaibo/p/favor-generic-methods.html
Copyright © 2011-2022 走看看