JAVA编程思想(第四版)学习笔记----11.4 容器的打印

 1 import static java.lang.System.out;
 2
 3 import java.util.ArrayList;
 4 import java.util.Collection;
 5 import java.util.HashMap;
 6 import java.util.HashSet;
 7 import java.util.LinkedHashMap;
 8 import java.util.LinkedHashSet;
 9 import java.util.LinkedList;
10 import java.util.Map;
11 import java.util.TreeMap;
12 import java.util.TreeSet;
13
14 public class ContainerFramework {
15
16     static Collection fill(Collection<String> collection) {
17         collection.add("rat");
18         collection.add("cat");
19         collection.add("dog");
20         collection.add("dog");
21         return collection;
22     }
23
24     static Map fill(Map<String, String> map) {
25         map.put("rat", "Fuzzy");
26         map.put("cat", "Rags");
27         map.put("dog", "Bosco");
28         map.put("dog", "Spot");
29         return map;
30     }
31     public static void main(String[] args) {
32         out.println(fill(new ArrayList<String>()));
33         out.println(fill(new LinkedList<String>()));
34         out.println(fill(new HashSet<String>()));
35         out.println(fill(new TreeSet<String>()));
36         out.println(fill(new LinkedHashSet<String>()));
37         out.println(fill(new HashMap<String, String>()));
38         out.println(fill(new TreeMap<String, String>()));
39         out.println(fill(new LinkedHashMap<String, String>()));
40     }
41 }

上述代码的运行结果为:

[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的查询速度。
时间: 2024-12-14 13:51:09

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 for the String class, you’ll see that every method in the class that appears to modify a String actually creates and returns a brand new String object c

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

At the lowest level, data in Java is manipulated using operators Using Java Operators An operator takes one or more argument and produces a new value. The arguements are in a different form than ordinary method calls, but the effect is the same. + :

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 tends to look like that machine. 计算机革命起源于机器,因此编程语言的产生也始于对机器的模仿 Computers are mind amplification tools and a different kind of expressive medium. 计算机是大

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十一)之Holding Your Objects

To solve the general programming problem, you need to create any number of objects, anytime, anywhere. So you can't rely on creating a named reference to hold each one of your objects. Java has several ways to hold objects: 1. the compiler-supported

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(六)之Initialization &amp; Cleanup

Two of these safety issues are initialization and cleanup. initialization -> bug cleanup -> running out of resources (most notably, memory) Java adopted the constructor, and in addition has a garbage collector that automoatically releases memory res

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

Polymorphism is the third essential feature of an object-oriented programming language,after data abastraction and inheritance. It provides another dimension of separation of interface from implementation, to decouple what from how. Polymorphism allo

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

The trick is to use the classes without soiling the existing code. 1. composition--simply create objects of your existing class inside the new class. simply reusing the functionality of the code, not its form 2.inheritance--creates a new class as a t

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十四)之Type Information

Runtime type information (RTTI) allow you to discover and use type information while a program is running This take two forms: 1. "traditional" RTTI, which assumes that you have all the types available at compile time, 2. the reflection mechanis

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, not all errors can be detected at compile time. To create a robust system, each component must be robust. By providing a consistent error-reporting

Java编程思想第四版读书笔记——第十三章 字符串

Java编程思想第四版读书笔记--第十三章 字符串 字符串的操作是计算机程序设计中最常见的行为. 关键词: StringBuilder ,StringBuffer,toString(),format转换,正则表达式, 1.不可变String String对象时不可变的.每当把String对象作为方法的参数时,都会复制一份引用.(其实就是对函数中参数列表中参数的操作不会影响外面的原参数) 如下: import static net.mindview.util.Print.*; public cla