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(队列)
  1. 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,可以通过键来查找值,是一种“键-值"对的容器
  1. HashMap是无序的,具有最快的查找速度。
  2. TreeMap是有序的,按照比较键的结果的升序进行保存
  3. LinkedHashMap是有序的,按照插元素的顺序进行保存,同时也保留了HashMap的查询速度。

JAVA编程思想(第四版)学习笔记----11.4 容器的打印的更多相关文章

  1. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十三)之Strings

    Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation fo ...

  2. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(四)之Operators

    At the lowest level, data in Java is manipulated using operators Using Java Operators An operator ta ...

  3. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(三)之Everything Is an Object

    ---恢复内容开始--- Both C++ and Java are hybird languages. A hybird language allow multiple programming st ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(九)之Interfaces

    Interfaces and abstract classes provide more structured way to separate interface from implementatio ...

  10. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(八)之Polymorphism

    Polymorphism is the third essential feature of an object-oriented programming language,after data ab ...

随机推荐

  1. 抛弃jQuery:Why?

    原文链接:http://blog.garstasio.com/you-dont-need-jquery/ 我的Blog:http://cabbit.me/you-dont-need-jquery/wh ...

  2. MongoDB学习笔记~数据模型属性为集合时应该为它初始化

    回到目录 今天要说一下技术点,我们在设计mongodb的数据模型时,如果属性是数组或者集合类型,我们在模型初始化时,需要为它们初始化一下,否则在数据库里将会被存储为NULL,当被存储为NULL时,我们 ...

  3. Javascript的“上下文”(context)

    一:JavaScript中的“上下文“指的是什么 百科中这样定义: 上下文是从英文context翻译过来,指的是一种环境. 在软件工程中,上下文是一种属性的有序序列,它们为驻留在环境内的对象定义环境. ...

  4. z-index--记录七

    1.z-index用来控制元素重叠时堆叠顺序. 适用于:已经定位的元素(即position:relative/absolute/fixed). 2.不使用z-index的时候,堆叠顺序如下(从下到上) ...

  5. IEEE浮点标准

    原文地址:http://www.math.byu.edu/~schow/work/IEEEFloatingPoint.htm Floating point system Floating point ...

  6. ASP.NET Core管道深度剖析(3):管道是如何处理HTTP请求的?

    我们知道ASP.NET Core请求处理管道由一个服务器和一组有序的中间件组成,所以从总体设计来讲是非常简单的,但是就具体的实现来说,由于其中涉及很多对象的交互,我想很少人能够地把它弄清楚.为了让读者 ...

  7. ASP.NET Core 阶段性总结

    自从年前用 ASP.NET 5 磕磕绊绊重写了一个项目后 (2015.12),就没怎么关注 ASP.NET 5 相关内容了,为啥?因为实际应用问题太多,而且不是正式版本,变化实在太快,可能你今天了解的 ...

  8. Geotrellis系列文章链接

    本文存放了我在博客园中撰写的Geotrellis系列文章链接,方便查阅! 一.geotrellis使用初探 二.geotrellis使用(二)geotrellis-chatta-demo以及geotr ...

  9. ENGLISH抠脚童鞋的福利--GitHub汉化插件

    今天在某前端群看到一个插件,激动万分啊!我就把插件使用实现的步骤分享一下! 打开chrome浏览器输入地址:chrome://extensions/ : 跳转到其他页面,点击左上角--扩展程序: 将T ...

  10. 由css reset想到的深入理解margin及em的含义

    由css reset想到的深入理解margin及em的含义 原文地址:http://www.ymblog.net/content_189.html 经常看到这样语句,*{ margin:0px;pad ...