zoukankan      html  css  js  c++  java
  • Java——集合类

    1、容器的打印

    import java.util.ArrayList;
    import java.util.Arrays;
    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.List;
    import java.util.Map;
    import java.util.TreeMap;
    import java.util.TreeSet;
    
    public class Tree1 {
        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}

    输出证明:

    a、前五个是Collection打印出来的内容是用方括号括住的,Map则由大括号括住。

    b、Collection在每个槽只能保存一个元素,Set的元素不能重复

    c、HashSet、TreeSet、LinkedHashSet三者的区别就是顺序,这是因为存储元素的方式不同,HashSet采用很复杂的方式存储元素,TreeSet通过比较结果的升序保存对象,LinkedHashSet按照被添加的顺序保存对象

    d、HashMap、TreeMap、LinkedHashMap三者的区别同样是顺序,HashMap没有什么明显的顺序,TreeMap比较结果的升序保存键,LinkedHashMap按照插入的顺序。

    2、PriorityQueue

      按照自然顺序进行弹出的队列

    import java.util.*;
    
    public class Tree1 {
    
        static void print(Queue queue) {
             while(queue.peek() != null) {
                 System.out.print(queue.remove() + " ");
             }
             System.out.println();
        }
        public static void main (String[] args) {
            List<Integer> ints = Arrays.asList(1,2,3,5,9,8,4,3,8,2,89,5,8,5,8);
            PriorityQueue<Integer> preiorityQueue = 
                    new PriorityQueue<Integer>(ints);
            print(preiorityQueue);
            preiorityQueue = 
                    new PriorityQueue<Integer>(ints.size(),Collections.reverseOrder());
            preiorityQueue.addAll(ints);
            print(preiorityQueue);
            String stringPq = "what do you want to do";
            List<String> stringPqs = Arrays.asList(stringPq.split(""));
            PriorityQueue<String> stringPQ = 
                    new PriorityQueue<String>(stringPqs);
            print(stringPQ);
            stringPQ = 
                    new PriorityQueue<String>(stringPqs.size(),Collections.reverseOrder());
            stringPQ.addAll(stringPqs);
            print(stringPQ);
        }
    }

    输出:

    1 2 2 3 3 4 5 5 5 8 8 8 8 9 89 
    89 9 8 8 8 8 5 5 5 4 3 3 2 2 1 
              a a d d h n o o o o t t t u w w y 
    y w w u t t t o o o o n h d d a a           

     Integer的优先级是按照大小排列的,String中空格的优先级高于字母的

  • 相关阅读:
    一致性哈希算法
    Discourse 的标签(Tag)只能是小写的原因
    JIRA 链接 bitbucket 提示错误 Invalid OAuth credentials
    JIRA 如何连接到云平台的 bitbucket
    Apache Druid 能够支持即席查询
    如何在 Discourse 中配置使用 GitHub 登录和创建用户
    Apache Druid 是什么
    Xshell 如何导入 PuTTYgen 生成的 key
    windows下配置Nginx支持php
    laravel连接数据库提示mysql_connect() :Connection refused...
  • 原文地址:https://www.cnblogs.com/xxbbtt/p/7631753.html
Copyright © 2011-2022 走看看