JAVA编程思想(第四版)学习笔记----11.4 容器的打印
import static java.lang.System.out; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet; public class ContainerFramework { static Collection fill(Collection<String> collection) {
collection.add("rat");
collection.add("cat");
collection.add("dog");
collection.add("dog");
return collection;
} static Map fill(Map<String, String> map) {
map.put("rat", "Fuzzy");
map.put("cat", "Rags");
map.put("dog", "Bosco");
map.put("dog", "Spot");
return map;
}
public static void main(String[] args) {
out.println(fill(new ArrayList<String>()));
out.println(fill(new LinkedList<String>()));
out.println(fill(new HashSet<String>()));
out.println(fill(new TreeSet<String>()));
out.println(fill(new LinkedHashSet<String>()));
out.println(fill(new HashMap<String, String>()));
out.println(fill(new TreeMap<String, String>()));
out.println(fill(new LinkedHashMap<String, String>()));
}
}
上述代码的运行结果为:
[rat, cat, dog, dog]
[rat, cat, dog, dog]
[cat, dog, rat]
[cat, dog, rat]
[rat, cat, dog]
{cat=Rags, dog=Spot, rat=Fuzzy}
{cat=Rags, dog=Spot, rat=Fuzzy}
{rat=Fuzzy, cat=Rags, dog=Spot}
经过运行代码,查看结果可以看出,Collection打印出来的内容用方括号[]括住,每个元素由逗号分隔;Map打印出来的内容用大括号{}括住,键与值用等号连接作为一个元素(键=值),每个元素用逗号分隔。
java容器类包括两种:以Collection接口为根的集合类,和以Map为根的关联数组类
- Collection接口有三个重要的子类型:List(列表),Set(集合),Queue(队列)
- List有两个重要的实现,分别为ArrayList和LinkedList
- List接口的所有实现类都保证其元素可以按照插入顺序被保存,所以List是有序的collection。其中ArrayList优点在于可以高效的随机访问其元素,缺点在于在指定位置插入、移除元素的性能比较慢。而LinkedList在随机访问方面比较慢,但是在指定位置插入、移除元素的效率比较高。
2. Set有三个重要的实现,分别为HashSet,TreeSet,LinkedHashSet
- Set接口的所有实现类都保证其元素不会重复。HashSet使用哈希算法来存数集合中的元素,它的元素是无序的,但是获取元素的效率是最快的。TreeSet是有序的集合,它将集合中的元素按照比较结果的升序进行保存。LinkedHashSet也是有序的集合,它按照元素插入的顺序进行保存对象,同时又具有HashSet的查询速度。
3. Queue
- Queue允许在容器的一端进行数据的插入,在另一端进行数据的移除。
- Map接口有三个重要的子类型:HashMap,TreeMap,LinkedHashMap,可以通过键来查找值,是一种“键-值"对的容器
- HashMap是无序的,具有最快的查找速度。
- TreeMap是有序的,按照比较键的结果的升序进行保存
- LinkedHashMap是有序的,按照插元素的顺序进行保存,同时也保留了HashMap的查询速度。
JAVA编程思想(第四版)学习笔记----11.4 容器的打印的更多相关文章
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十三)之Strings
Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation fo ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(四)之Operators
At the lowest level, data in Java is manipulated using operators Using Java Operators An operator ta ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(三)之Everything Is an Object
---恢复内容开始--- Both C++ and Java are hybird languages. A hybird language allow multiple programming st ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(二)之Introduction to Objects
The genesis of the computer revolution was a machine. The genesis of out programming languages thus ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十四)之Type Information
Runtime type information (RTTI) allow you to discover and use type information while a program is ru ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十二)之Error Handling with Exceptions
The ideal time to catch an error is at compile time, before you even try to run the program. However ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十一)之Holding Your Objects
To solve the general programming problem, you need to create any number of objects, anytime, anywher ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十)之Inner Classes
The inner class is a valuable feature because it allows you to group classes that logically belong t ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(九)之Interfaces
Interfaces and abstract classes provide more structured way to separate interface from implementatio ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(八)之Polymorphism
Polymorphism is the third essential feature of an object-oriented programming language,after data ab ...
随机推荐
- .NET里简易实现IoC
.NET里简易实现IoC 前言 在前面的篇幅中对依赖倒置原则和IoC框架的使用只是做了个简单的介绍,并没有很详细的去演示,可能有的朋友还是区分不了依赖倒置.依赖注入.控制反转这几个名词,或许知道的也只 ...
- Entity Framework 6 Recipes 2nd Edition(9-6)译->管理断开时的并发
9-6. 管理断开时的并发 问题 想要确保只接受在WCF客户端并发令牌未被修改的实体. 解决方案 我们有一个如Figure 9-6所示的模型. Figure 9-6订单实体模型 我们想通过WCF服务来 ...
- [译]ZOOKEEPER RECIPES-TWO PHASED COMMIT
两段式提交 两段式提交协议可以让所有分布式系统中的客户端达成协议同时提交或回滚事务. 在ZooKeeper中你可以通过协调者(coordinator)创建一个事务节点来实现两段式提交.例如" ...
- JavaScript 闭包深入浅出
闭包是什么? 闭包是内部函数可以访问外部函数的变量.它可以访问三个作用域:首先可以访问自己的作用域(也就是定义在大括号内的变量),它也能访问外部函数的变量,和它能访问全局变量. 内部函数不仅可以访问外 ...
- (转载)解决“Windows Update一直无法完成”的方法
近日彻底重装系统,启动Win7的"Windows Update",程序显示在检查更新,却一直没有结果. 在排除网络问题,排除杀毒软件和防火墙的问题之后,感觉还是程序或者配置除了问题 ...
- 通过Gradle为APK瘦身
引言:在过去几年中,APK 文件的大小曾急剧增长态势.一般来说,其原因如下:Android开发者获取了更多的依赖库,添加了更多的密度,Apps 增加了更多的功能.但实际上我们应该让APKs 尽可能的小 ...
- 计算机程序的思维逻辑 (30) - 剖析StringBuilder
上节介绍了String,提到如果字符串修改操作比较频繁,应该采用StringBuilder和StringBuffer类,这两个类的方法基本是完全一样的,它们的实现代码也几乎一样,唯一的不同就在于,St ...
- ios 开发需要看的书籍
1.吴航写的<iOS应用逆向工程 第2版> 2.<iOS 应用安全攻防实战> 3.
- 【分布式】Zookeeper的服务器角色
一.前言 前一篇已经详细的讲解了Zookeeper的Leader选举过程,下面接着学习Zookeeper中服务器的各个角色及其细节. 二.服务器角色 2.1 Leader Leader服务器是Zook ...
- PHP环境配置
PHP环境配置 1.Apache的安装 第一步: 1. 双击httpd-2.2.17-win32-x86-no_ssl.msi.出现 Windows 标准的软件安装欢迎界面,直接点“Next”继 ...