Why use interface type to declare a collectio

The following program prints out all distinct words in its argument list. Two versions of this program are provided. The first uses JDK 8 aggregate operations. The second uses the for-each construct.

Using JDK 8 Aggregate Operations:

import java.util.*;
import java.util.stream.*;

public class FindDups {
public static void main(String[] args) {
Set<String> distinctWords = Arrays.asList(args).stream()
.collect(Collectors.toSet());
System.out.println(distinctWords.size()+
" distinct words: " +
distinctWords);
}
}
Using the for-each Construct:

import java.util.*;

public class FindDups {
public static void main(String[] args) {
Set<String> s = new HashSet<String>();
for (String a : args)
s.add(a);
System.out.println(s.size() + " distinct words: " + s);
}
}

Now run either version of the program.

java FindDups i came i saw i left
The following output is produced:

4 distinct words: [left, came, saw, i]
Note that the code always refers to the Collection by its interface type (Set) rather than by its implementation type. This is a strongly recommended programming practice because it gives you the flexibility to change implementations merely by changing the constructor. If either of the variables used to store a collection or the parameters used to pass it around are declared to be of the Collection‘s implementation type rather than its interface type, all such variables and parameters must be changed in order to change its implementation type.

Furthermore, there‘s no guarantee that the resulting program will work. If the program uses any nonstandard operations present in the original implementation type but not in the new one, the program will fail. Referring to collections only by their interface prevents you from using any nonstandard operations.

时间: 2024-10-14 12:03:25

Why use interface type to declare a collectio的相关文章

■[iOS] Interface type cannot be statically allocated の原因と対応

iOSでの開発をしていると.表題のエラーが起こる場合があります. 原因 変数の型が静的割り当てになっていることが原因. 対応 変数の型をポインタ型にすると.エラーがなくなります.(変数の前に*をつけること)

Java语法之内部接口的学习 —— What Is Inner Interface in Java?

What is Inner Interface in Java? Inner interface is also called nested interface, which means declare an interface inside of another interface. For example, the Entry interface is declared in the Map interface. public interface Map { interface Entry{

ORACLE中 %TYPE 和 %ROWTYPE 的使用

%TYPE 用在变量的声明里,用于取得表中的字段类型: %ROWTYPE 用于声明基于某个表的行类型: 示例: %ROWTYPE 使用 DECLARE      CURSOR pdct_cur      IS SELECT * FROM PRODUCTINFO; cur_prodcrd productinfo%ROWTYPE; BEGIN         OPEN pdct_cur;          FETCH pdct_cur INTO cur_prodrcd;          DBMS_

IPMI (Intelligent Platform Management Interface)

4.3. ipmitool - utility for controlling IPMI-enabled devices 4.3.1. ipmitool 4.3.1.1. ubuntu 确定硬件是否支持 IPMI [email protected]:~$ sudo dmidecode |grep -C 5 IPMI [sudo] password for neo: Handle 0x2000, DMI type 32, 11 bytes System Boot Information Statu

Golang-interface(四 反射)

github:https://github.com/ZhangzheBJUT/blog/blob/master/reflect.md 一 反射的规则 反射是程序运行时检查其所拥有的结构,尤其是类型的一种能力:这是元编程的一种形式.它同时也是造成混淆的重要来源. 每个语言的反射模型都不同(同时许多语言根本不支持反射).本节将试图明确解释在 Go 中的反射是如何工作的. 1. 从接口值到反射对象的反射 在基本的层面上,反射只是一个检查存储在接口变量中的类型和值的算法.在 reflect 包中有两个类

WebADI_配置设定2_设定接口WebADI Interface(案例)

webadi 这是建立一个新的Integrator的第二步,也是最关键的一步! Interface Name: 随便写,反正oracle 会生成一个interface code, 而这个才是我们需要的,而我们需要的,oracle 偏偏不让我们看到,而且还加东西...生成的code 应该是integrator code + INTF1之类的 Interface Type: 主要是Table 和 API Procedure. 那么什么时候用Table ,什么时候用Procedure呢? 下面是我个人

Unity Interface Serialization-Expose Interface field In Inspector

Unity has some quirks about their inspector, so as a preface they are listed here: If you add a [Serializable] attribute to a class, Unity's Inspector will attempt to show all public fields inside that class. Any class extending Monobehaviour automat

interface变量存储类型

我们知道interface的变量里面可以存储任意类型的数值(该类型实现了interface).那么我们怎么反向知道这个变量里面实际保存了的是哪个类型的对象呢?目前常用的有两种方法: Comma-ok断言 Go语言里面有一个语法,可以直接判断是否是该类型的变量: value, ok = element.(T),这里value就是变量的值,ok是一个bool类型,element是interface变量,T是断言的类型. 如果element里面确实存储了T类型的数值,那么ok返回true,否则返回fa

go语言中的接口interface

package main; import "fmt" //接口interface //接口是一个或多个方法签名的集合 //只要某个类型拥有该接口的所有方法签名,即算实现该接口. //接口只有方法声明,没有实现,没有数据字段 //接口可以匿名嵌入其它接口,或嵌入到结构中. //GO语言中的所有类型都实现了空接口 type Empty interface { } type Inf interface { getName() string; setName(name string); } t