java7新特性之Diamond syntax

Java 7 also introduces a change that means less typing for you when dealing with

generics. One of the problems with generics is that the definitions and setup of

instances can be really verbose. Let’s suppose that you have some users, whom you

identify by userid (which is an integer), and each user has one or more lookup tables

specific to that user. What would that look like in code?

Map<Integer, Map<String, String>> usersLists =
new HashMap<Integer, Map<String, String>>();

That’s quite a mouthful, and almost half of it is duplicated characters. Wouldn’t it be

better if you could write something like this,

Map<Integer, Map<String, String>> usersLists = new HashMap<>();

and have the compiler work out the type information on the right side? Thanks to the

magic of Project Coin, you can. In Java 7, the shortened form for declarations like that

is entirely legal. It’s backwards compatible as well, so when you find yourself revisiting

old code, you can cut the older, more verbose declaration and start using the new

type-inferred syntax to save a few pixels.

We should point out that the compiler is using a new form of type inference for

this feature. It’s working out the correct type for the expression on the right side, and

isn’t just substituting in the text that defines the full type.

The “diamond syntax” name

This form is called “diamond syntax” because, well, the shortened type information

looks like a diamond. The proper name in the proposal is “Improved Type Inference

for Generic Instance Creation,” which is a real mouthful and has ITIGIC as an acronym, which sounds stupid, so diamond syntax it is.

读书笔记:The Well-Grounded Java Develope

java7新特性之Diamond syntax的更多相关文章

  1. java7新特性之Try-with-resources (TWR)

    java7新特性之Try-with-resources (TWR) This change is easy to explain, but it has proved to have hidden s ...

  2. java7 新特性 总结版

    Java7语法新特性: 前言,这是大部分的特性,但还有一些没有写进去,比如多核 并行计算的支持加强 fork join 框架:这方面并没有真正写过和了解.也就不写进来了. 1. switch中增加对S ...

  3. java7新特性 java8新特性

    Java 7 的7个新特性 Java7语法新特性 JAVA8 十大新特性详解 http://www.jb51.net/article/48304.htm

  4. Java7 新特性 数值文本表示法

    今天和大家分享下 java7中新特性-数值文本表示法 首先,在原来jdk1.6中 如果需要将一个二进制的数值转换成十进制的话,一般情况下都会以下面的代码方式去实现. public static voi ...

  5. Java7新特性

    ① 新增了switch对字符串的支持,也就是说可以在switch之后直接使用字符串来进行判断,语法基本与Java7之前支持的语法一样. ② 对数值字面量的增强支持,首先是可以在源代码中直接使用二进制数 ...

  6. Java7 新特性: try-with-resources

    Try-with-resources是java7中一个新的异常处理机制,它能够很容易地关闭在try-catch语句块中使用的资源. 利用Try-Catch-Finally管理资源(旧的代码风格)在ja ...

  7. Java7 新特性 —— java.nio.file 文件操作

    本文部分摘自 On Java 8 自 Java7 开始,Java 终于简化了文件读写的基本操作,新增了 java.nio.file 库,通过与 Java8 新增的 stream 结合可以使得文件操作变 ...

  8. Java7 新特性 switch 可以使用String

    今天和大家分享下 在java7中可以使用String 作为switch 中的参数. 原来在java7之前,switch只能去接收一个 byte.char.short.int 类型 现在在java7中 ...

  9. Java之Java7新特性之try资源句式

    一.原来写法: static String readFirstLineFromFile(String path) throws IOException { BufferedReader br = nu ...

随机推荐

  1. js编码处理(转)

    1.       使用 JS 中的 encodeURIComponent 或 encodeURI 方法. 说明: encodeURIComponent(String) 对传递参数进行设置.不编码字符有 ...

  2. JS、CSS、Image预加载

    Image预加载 <div class="hidden"> <script type="text/javascript"> var im ...

  3. HDOJ 1846 Brave Game - 博弈入门

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1846 经典基础博弈,首先面对(m+1)的人一定会输,依次往后推即可: #include<iost ...

  4. Yii 2.0 query模式语法

    项目使用Yii 2.0版本开发,个人一直喜好使用(new \yii\db\Query())模式操作数据,把增.删.查.改这4种情况的写法整理出来,方便查阅和记忆. 增加 - insert use Yi ...

  5. 零基础入门学习Python(18)--函数:灵活即强大

    前言 上一节课我们基本介绍Python函数的用法,这一节课我们主要针对函数的参数进行进一步的深入学习. 知识点 形参(parameter)和实参(argument) >>> def ...

  6. Kvm:通过 libvirt 远程管理虚拟机

    1.通过qemu+ssh方式 2.通过qemu+tcp方式 主控端需要安装相关工具包: #yum groupinstall "Virtualization" #yum instal ...

  7. 利用类装饰器自定制property实现延迟计算

    class LazyProperty: ''' hello,我是非数据描述符(没有定义__set__,不然是大哥数据描述符了--!) ''' def __init__(self, func): pri ...

  8. linux文件及目录的权限管理

    一.文件的权限 1.文件权限的查看 命令:ls -l 可以使用ll命令代替 ls -l 2.ls -l 所包含的信息 (1)权限信息 (-rw-r--r-- ) 一共有10位 a.第一位:表示文件信息 ...

  9. python基础示例

    7.写代码 (1)实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败! _name = "seven" _pwd = " ...

  10. DP 简单题目练习

    ZOJ 1234 这道题目我表示也还不是特别能理解....还是太菜了T T 从后往前思考,因为只要后面有多的数在,那么C肯定是存在的,只要考虑是否把前两个数加在一起作为badness值这样两种情况来考 ...