• 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实现使用的是一种非常快的算法来控制顺序。

  • 相关阅读:
    SaltStack(六) 案例练习
    SaltStack(五) SaltStack与ZeroMQ
    SaltStack(四) 配置管理
    SaltStack(三) 远程执行
    js 阳历、阴历互转
    把一个服务器的数据库导入到另一台服务器中
    vue项目 px自动转vw
    oracle创建自增序列和触发器
    svn 无法clean up的解决方案
    vue 后台获取路由表,addRouters动态路由
  • 原文地址:https://www.cnblogs.com/cgy-home/p/11179451.html
Copyright © 2020-2023  润新知