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. @import和link的区别

    @import和link的区别 1.link语法结构    <link href="CSSurl路径" rel="stylesheet" type=&qu ...

  2. Angular系列------AngularJS入门教程:导言和准备(转载)

    学习AngularJS的一个好方法是逐步完成本教程,它将引导您构建一个完整的AngularJS web应用程序. 该web应用是一个Android设备清单的目录列表,您可以筛选列表以便查看您感兴趣的设 ...

  3. linux内核更新前后配置文件的比较

    说明:这里先给出一个比较的结果,作为记录,后续会给出内核配置差异的详细解释. [root@xiaolyu linux-4.7.2]# diff .config .config_bak  3c3< ...

  4. SecureCRT连接linux设置vim显示颜色

    只需要两个步骤: 1) 选项 --> 会话选项 --> 终端 --> 仿真 -->  勾选“ANSI 颜色”. 2)  在.bashrc中添加:export TERM=xter ...

  5. [ASP.NET]谈谈IIS与ASP.NET管道

    作为一个Asp.Net平台开发者,非常有必要了解IIS和Asp.Net是如何结合,执行我们的托管代码,以及Asp.Net管道事件的. 本节目录 IIS 5.X IIS 6 IIS 7+ 集成模式 As ...

  6. [JS] JavaScript框架(2) D3

    D3(Data-Driven Documents)是一个用于网页作图.生成互动图形的JavaScript函数库. 官网:http://d3js.org/ 下载: cdn:<script src= ...

  7. html5人物图片360度立体旋转

    体验效果:http://hovertree.com/texiao/html5/10.htm 下载:http://hovertree.com/hvtart/bjae/t16oddyt.htm 代码如下: ...

  8. Winfrom中ListBox绑定List数据源更新问题

    Winfrom中ListBox绑定List数据源更新问题 摘自:http://xiaocai.info/2010/09/winform-listbox-datasource-update/ Winfr ...

  9. asp.net.mvc 的单文件上传和多文件上传的简单例子

    首先打开vs2012,创建空的mvc4项目,名称为MVCStudy,选择基本模板

  10. 35 Gallery – Ajax Slide

    http://html5up.net/overflow (PC,Mobile,Table) http://bridge.net/ https://github.com/bridgedotnet/Bri ...