java7新特性之Diamond syntax
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的更多相关文章
- java7新特性之Try-with-resources (TWR)
java7新特性之Try-with-resources (TWR) This change is easy to explain, but it has proved to have hidden s ...
- java7 新特性 总结版
Java7语法新特性: 前言,这是大部分的特性,但还有一些没有写进去,比如多核 并行计算的支持加强 fork join 框架:这方面并没有真正写过和了解.也就不写进来了. 1. switch中增加对S ...
- java7新特性 java8新特性
Java 7 的7个新特性 Java7语法新特性 JAVA8 十大新特性详解 http://www.jb51.net/article/48304.htm
- Java7 新特性 数值文本表示法
今天和大家分享下 java7中新特性-数值文本表示法 首先,在原来jdk1.6中 如果需要将一个二进制的数值转换成十进制的话,一般情况下都会以下面的代码方式去实现. public static voi ...
- Java7新特性
① 新增了switch对字符串的支持,也就是说可以在switch之后直接使用字符串来进行判断,语法基本与Java7之前支持的语法一样. ② 对数值字面量的增强支持,首先是可以在源代码中直接使用二进制数 ...
- Java7 新特性: try-with-resources
Try-with-resources是java7中一个新的异常处理机制,它能够很容易地关闭在try-catch语句块中使用的资源. 利用Try-Catch-Finally管理资源(旧的代码风格)在ja ...
- Java7 新特性 —— java.nio.file 文件操作
本文部分摘自 On Java 8 自 Java7 开始,Java 终于简化了文件读写的基本操作,新增了 java.nio.file 库,通过与 Java8 新增的 stream 结合可以使得文件操作变 ...
- Java7 新特性 switch 可以使用String
今天和大家分享下 在java7中可以使用String 作为switch 中的参数. 原来在java7之前,switch只能去接收一个 byte.char.short.int 类型 现在在java7中 ...
- Java之Java7新特性之try资源句式
一.原来写法: static String readFirstLineFromFile(String path) throws IOException { BufferedReader br = nu ...
随机推荐
- linux远程开机
它需要wakeonlan这个软件, 从何处得到它? 它的官方站是:http://sourceforge.net/projects/wake-on-lan/ 如果使用rpm包可以 ...
- 第2节 hive基本操作:12、hive当中的hql语法
3.2. hive查询语法 3.2.1.SELECT https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Select 基本 ...
- 第3节 mapreduce高级:8、9、自定义分区实现分组求取top1
自定义GroupingComparator求取topN GroupingComparator是mapreduce当中reduce端的一个功能组件,主要的作用是决定哪些数据作为一组,调用一次reduce ...
- hdfs深入:09、获取分布式文件系统客户端的几种方式
FileSystem是一个抽象类:获取一个抽象类有两种方式:第一种:看这个抽象类有没有提供什么方法返回他本身第二种:找子类 具体代码如下: /** * 通过url注册的方式访问hdfs,了解,不会用到 ...
- IIS添加更改默认页面
服务器中打开IIS管理器,选择默认文档,双击即可进入编辑:
- 浏览器通知js授权
// 获得权限 Notification.requestPermission(); // 点击按钮 document.querySelector('#button').addEventListener ...
- H5实现调用本地摄像头实现实时视频以及拍照功能
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 51node 1134 最长递增子序列 (数据结构)
题意: 最长递增子序列 思路: 普通的$O(n^2)$的会超时.. 然后在网上找到了另一种不是dp的写法,膜拜一下,自己写了一下解释 来自:https://blog.csdn.net/Adusts/a ...
- 201621123079《Java程序设计》第1周学习总结
第1周-Java基本概念 1.本周学习总结 第一次上课接触java,了解了java的由来和历史,还有JCP,JSP的概念,并学会如何建立一个java文件和运行过程.感觉java比之前学习的数据结构更高 ...
- GPIO——端口位设置/清除寄存器BSRR,端口位清除寄存器BRR
端口位设置/复位寄存器BSRR: 注:如果同时设置了BSy和BRy的对应位,BSy位起作用. 位31:16 BRy: 清除端口x的位y (y = 0…15) 这些位只能写入并只能以字(16 ...