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. P2P实现的原理

    为了项目的后期IM应用,最近在研究libjingle,中间看了也收集了很多资料,感慨网上很多资料要么太过于纠结协议(如STUN.ICE等)实现细节,要么中间有很多纰漏.最后去伪存真,归纳总结了一下,希 ...

  2. mycat——未完成

    https://www.cnblogs.com/joylee/p/7513038.html https://blog.csdn.net/wrs120/article/details/80417345 ...

  3. 杭电1722 Cake (分蛋糕)

    #include<cstdio> int f(int m,int n) { ) return n; else return f(n,m%n); } int main() { int m,n ...

  4. poj 2186 强连通分量

    poj 2186 强连通分量 传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 33414 Acc ...

  5. flash-热风焊盘的制作

    计算部分: 热风焊盘的内径(ID)等于钻孔直径+20mil, 外径(OD)等于Anti-pad的直径,通常是比焊盘的直径大20mil. 开口宽度等于(OD-ID)/2+10mil,保留整数位. 如果焊 ...

  6. UART整理

    通用异步收发器简称UART,英文全称"Universal Asynchronous Receiver Transmitter".UART使用标准的TTL/CMOS逻辑电平(0~5V ...

  7. 创建私有 Gems 源

    1.安装依赖 yum install gem -y gem install builder   2.安装.配置nginx的文件列表 添加/etc/nginx/default.d/mirrors.con ...

  8. 【01染色法判断二分匹配+匈牙利算法求最大匹配】HDU The Accomodation of Students

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 [DFS染色] #include<iostream> #include<cstdio&g ...

  9. POJ 2186 tarjan+缩点 基础题

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 37111   Accepted: 15124 De ...

  10. codevs——1009 产生数

    1009 产生数 2002年NOIP全国联赛普及组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 给出一个 ...