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).

Effective Java 27 Favor generic methods的更多相关文章

  1. Effective Java 26 Favor generic types

    Use generic types to replace the object declaration Add one or more type parameters to its declarati ...

  2. Effective Java 16 Favor composition over inheritance

    Inheritance disadvantage Unlike method invocation, inheritance violates encapsulation. Since you don ...

  3. Effective Java 54 Use native methods judiciously

    Java Native Interface(JNI) allows Java applications to call native methods, which are special method ...

  4. Effective Java 76 Write readObject methods defensively

    Principle readObject method is effectively another public constructor, and it demands all of the sam ...

  5. Effective Java 60 Favor the use of standard exceptions

    Benefits to reuse preexisting exceptions It makes your API easier to learn and use. Programs using y ...

  6. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  7. 《Effective Java》读书笔记 - 5.泛型

    Chapter 5 Generics Item 23: Don't use raw types in new code 虽然你可以把一个List<String>传给一个List类型(raw ...

  8. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  9. Effective Java 第三版——27. 消除非检查警告

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

随机推荐

  1. Android的Activity生命周期

    Android的Activity就相当于Windows Form中的Form,它的创建和销毁也是有一个生命周期的.主要经过这么7个阶段:   创建Activity:onCreate() 启动Activ ...

  2. win8.1注册表-修改资源管理器的默认路径regedit

    默认情况下,激活Win8的资源管理器都是直接打开库,对于习惯了以前版本的Windows用户来说,还要重新选择“计算机”才能进入相应的磁盘寻找文件,操作起来很是不便.为此,可以进行设置,让资源管理器默认 ...

  3. 【iOS】单例模式

    单例模式在软件开发中经常用到,在iOS系统framework也很多地方用到单例模式,例如 [NSUserDefaults standardUserDefaults], [NSBundle mainBu ...

  4. 四项技术 助你提高SQL Server的性能

    有时,为了让应用程序运行得更快,所做的全部工作就是在这里或那里做一些很小调整.但关键在于确定如何进行调整!迟早您会遇到这种情况:应用程序中的 SQL 查询不能按照您想要的方式进行响应.它要么不返回数据 ...

  5. 为C1Chart for WPF添加自定义标题、坐标轴单位标签以及旋转坐标轴注释

    对于图表控件C1Chart for WPF,我们在添加数据,选择图表类型这些基本可视化数据展示后,经常需要通过标题.坐标轴单位标签等信息辅助说明图表对实际场景的意义.C1Chart for WPF并没 ...

  6. 用linux遇到的一个死循环

    1. 公司的服务器centos,需要通过vpn拨上去: 2. 然后ftp启用了tls加密: 3. 然后ubuntu 12.04 上libgnutls的版本比较新,装的filezilla 3.5.3,怎 ...

  7. 回文串--- Girls' research

    HDU   3294 Problem Description One day, sailormoon girls are so delighted that they intend to resear ...

  8. ORACLE 中ROWNUM用法总结!

    ORACLE 中ROWNUM用法总结! 对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=) ...

  9. JS控制HTML元素的显示和隐藏

    JS控制HTML元素的显示和隐藏 利用来JS控制页面控件显示和隐藏有两种方法,两种方法分别利用HTML的style中的两个属性,两种方法的不同之处在于控件隐藏后是否还在页面上占空位. 方法一: 1 2 ...

  10. SQLServer处理行转列和列转行

    掌握SQL Server 行转列和列转行 1.列转行 数据经过计算加工后会直接生成前端图表需要的数据源,但是程序里又需要把该数据经过列转行写入中间表中,下次再查询该数据时直接从中间表查询数据. 1.1 ...