zoukankan      html  css  js  c++  java
  • 11.4 容器的打印

    11.4 容器的打印
    一些基本类型的容器

    //: Holding/PrintingContainers.java
    // Containers print themselves automatically.
    
    import java.util.*;
    
    public class PrintContainers {
        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) {
            System.out.println(fill(new ArrayList<String>()));
            System.out.println(fill(new LinkedList<String>()));
            System.out.println(fill(new HashSet<String>()));
            System.out.println(fill(new TreeSet<String>()));
            System.out.println(fill(new LinkedHashSet<String>()));
            System.out.println(fill(new HashMap<String, String>()));
            System.out.println(fill(new TreeMap<String, String>()));
            System.out.println(fill(new LinkedHashMap<String, String>()));
        }
    }/*
    [rat, cat, dog, dog]
    [rat, cat, dog, dog]
    [rat, cat, dog]
    [cat, dog, rat]
    [rat, cat, dog]
    {rat=Fuzzy, cat=Rags, dog=spot}
    {cat=Rags, dog=spot, rat=Fuzzy}
    {rat=Fuzzy, cat=Rags, dog=spot}
    *///~
    

    collection打印出来的内容用方括号括住,每个元素由逗号分隔开。Map则用大括号括住,键与值由等号联系。

    HashSet、TreeSet和LinkedHashSet都是Set类型,输出显示在Set中,每个相同的项只有保存一次,但是输出也显示了不同的Set实现储存元素的方式不同。HashSet使用的是相当复杂的方式来存储元素的,这种方式在17章中学习。

    你不必指定(或考虑)Map的尺寸,因为它自己会自动调整尺寸。Map还知道如何打印自己,它会显示相关联的键和值。键和值在Map中的保存熟悉怒不是他们的插入顺序,因为HashMap实现使用的是一种非常快的算法来控制顺序。

  • 相关阅读:
    Visual C++2005的IntelliSense问题
    R调用数据库的方式汇总
    Loading local data is disabled; this must be enabled on both the client and server sides 
    R的transform函数
    R连接mysql数据库(ODBC)
    mysql实例代码
    随笔20130105
    开端
    MeteoInfo
    火车软硬卧铺分布图
  • 原文地址:https://www.cnblogs.com/cgy-home/p/11179451.html
Copyright © 2011-2022 走看看