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中空格的优先级高于字母的

  • 相关阅读:
    c#队列的实现
    c#队列的实现
    C# 自定义控件制作和使用实例(winform)
    常见的位运算
    Clock()函数简单使用(C库函数)
    Python全局变量的简单使用
    PyQt5+Caffe+Opencv搭建人脸识别登录界面
    python3+pyqt5+opencv3简单使用
    OpenCV实现人脸检测
    opencv 截图并保存
  • 原文地址:https://www.cnblogs.com/xxbbtt/p/7631753.html
Copyright © 2011-2022 走看看