美文网首页
容器相关简单运用(一)

容器相关简单运用(一)

作者: 云烟渐成雨 | 来源:发表于2019-08-20 01:46 被阅读0次

    1.1 Java容器

    • Arrays.asList() 方法接受一个数组或是一个用逗号隔开的元素列表(使用可变参数),并将其转化为一个List对象。

    • Arrays.asList()注意事项: 我们可以直接使用Arrays.asList()的输出,将其视为List,但是由于其底层表示的是数组,因此不能使用add()或delete()方法在这种列表中添加或者删除元素,否则运行时会出现UnsupportedOperationException。但可以使用set(index,int)方法来改变下标为index的值。

    • Collections.addAll() 方法接受一个collection对象,以及一个数组或是一个用逗号隔开的元素列表,并将元素添加到Collection中。

    public class AddingGroups {
    
        public static void main(String[] args) {
            Collection<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
            Integer[] mooreInts = {6, 7, 8, 9, 10};
    //        collection.addAll(Arrays.asList(mooreInts));
    //        Collections.addAll(collection, mooreInts);
            Collections.addAll(collection, 6, 7, 8, 9, 10);
            System.out.print("collection: ");
            for (Integer integer : collection) {
                System.out.print(integer + " ");
            }
    
            List<Integer> list = Arrays.asList(11, 12, 13, 14, 15);
            list.set(0, 1);
    //        list.add(16);     Runtime error
            System.out.print("\nlist: ");
            for (Integer integer : list) {
                System.out.print(integer + " ");
            }
        }
    }
    

    运行结果:

    collection: 1 2 3 4 5 6 7 8 9 10 
    list: 1 12 13 14 15
    

    1.2 容器的打印

    public class PrintingContainers {
    
        private static void print(Object o){
            System.out.println(o);
        }
    
        static Collection fill(Collection<String> collection){
            collection.add("rat");
            collection.add("cat");
            collection.add("cat");
            collection.add("dog");
            return collection;
        }
    
        static Map fill(Map<String,String> map){
            map.put("rat","Jerry");
            map.put("cat","TomCat");
            map.put("cat","Tom");
            map.put("dog","Spike");
            return map;
        }
    
        public static void main(String[] args) {
            print(fill(new ArrayList<String>()));
            print(fill(new LinkedList<String>()));
            print(fill(new HashSet<String>()));
            print(fill(new TreeSet<String>()));
            print(fill(new LinkedHashSet<String>()));
            print(fill(new HashMap<String,String>()));
            print(fill(new TreeMap<String,String>()));
            print(fill(new LinkedHashMap<String,String>()));
        }
    }
    

    输出结果:

    [rat, cat, cat, dog]
    [rat, cat, cat, dog]
    [rat, cat, dog]
    [cat, dog, rat]
    [rat, cat, dog]
    {rat=Jerry, cat=Tom, dog=Spike}
    {cat=Tom, dog=Spike, rat=Jerry}
    {rat=Jerry, cat=Tom, dog=Spike}
    

    1.3 List

    List接口在Collection的基础上添加了大量方法,使得List可以在中间插入或移除元素。

    有两种类型的List

    • ArrayList : 适合随机访问元素,但在List的中间插入和移除元素时较慢
    • LinkedList : List的中间插入和移除元素的代价较低,提供了优化的顺序访问。在随机访问相对比较慢。

    1.4 Stack(栈)

    堆栈是一个后进先出的容器
    LinkedList具有能够直接实现栈的所有功能的方法,因此可以直接将它作为栈使用

    class Stack<T> {
        private LinkedList<T> storage = new LinkedList<>();
    
        public void push(T t) {
            storage.addFirst(t);
        }
    
        public T peek() {
            return storage.getFirst();
        }
    
        public T pop() {
            return storage.removeFirst();
        }
    
        public boolean empty() {
            return storage.isEmpty();
        }
    
        public String toString() {
            return storage.toString();
        }
    }
    
    public class StackTest {
        public static void main(String[] args) {
            Stack<String> stack = new Stack<>();
            for (String s : "YOU Love I".split(" ")) {
                stack.push(s);
            }
            while (!stack.empty()) {
                System.out.print(stack.pop() + " ");
            }
        }
    }
    
    • peek()提供栈顶元素,但并不将其从栈顶移除
    • pop()移除并返回栈顶元素

    1.5 Set

    • Set不保存重复的元素。

    • Set中最常被使用的是测试归属性,我们可以很容易地询问某个对象是否在某个Set中(Set是基于对象的值来确定归属性的)因此,查找就成为了Set中最重要地操作,通常会选择一个HashSet的实现,它专门对快速查找进行了优化。

    • Set具有与Collection完全一样的接口,因此没有任何额外的功能(实际上Set就是Collection,只是行为不同)

    public class SetOfInteger {
        public static void main(String[] args) {
            Random rand = new Random(47);
            Set<Integer> intSet = new HashSet<>();
    //        Set<Integer> intSet = new TreeSet<>();
    //        Set<Integer> intSet = new LinkedHashSet<>();
            for (int i = 0; i < 1000; i++) {
                int it = rand.nextInt(30);
                intSet.add(it);
            }
            System.out.println(intSet);
        }
    }
    

    HashSet(JDK8的HashSet实现变了,先前输出结果为乱序)和TreeSet(元素存储在红-黑树数据结构)输出结果:

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
    

    LinkedHashSet(因为查询速度的原因使用了散列)
    输出结果:

    [8, 5, 13, 11, 1, 29, 28, 20, 12, 7, 18, 21, 19, 16, 0, 14, 3, 26, 25, 10, 2, 24, 4, 6, 22, 27, 17, 9, 23, 15]
    

    使用contains()测试Set的归属性

    public class SetOperations {
        public static void main(String[] args) {
            Set<String> set1 = new HashSet<>();
            Collections.addAll(set1, "A B C D E F G H I J K L".split(" "));
            set1.add("M");
            print("H :" + set1.contains("H"));
            print("N :" + set1.contains("N"));
            Set<String> set2 = new HashSet<>();
            Collections.addAll(set2, "H I J K L".split(" "));
            print("set2 in set1 :" + set1.containsAll(set2));
            set1.remove("H");
            print("set1 :" + set1);
            print("set2 in set1 :" + set1.containsAll(set2));
            set1.removeAll(set2);
            print("set2 removed from set1 :" + set1);
            Collections.addAll(set1, "X Y Z".split(" "));
            print("'X Y Z' added to set1 :" + set1);
        }
    
        private static void print(Object o) {
            System.out.println(o);
        }
    }
    

    运行结果:

    H :true
    N :false
    set2 in set1 :true
    set1 :[A, B, C, D, E, F, G, I, J, K, L, M]
    set2 in set1 :false
    set2 removed from set1 :[A, B, C, D, E, F, G, M]
    'X Y Z' added to set1 :[A, B, C, D, E, F, G, M, X, Y, Z]
    

    1.6 Map

    记录随机数0-9在遍历1000次中出现的次数。

    • map.values()返回它的组成的Set
    • map.keySet()返回它的组成的Set
    public class Statistics {
        public static void main(String[] args) {
            Random rand = new Random(47);
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < 1000; i++) {
                int r = rand.nextInt(10);
                Integer count = map.get(r);
                map.put(r, count == null ? 1 : count + 1);
            }
            System.out.println(map);
            System.out.println(map.keySet());
            System.out.println(map.values());
        }
    }
    

    运行结果:

    {0=100, 1=99, 2=101, 3=98, 4=96, 5=103, 6=95, 7=103, 8=97, 9=108}
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    [100, 99, 101, 98, 96, 103, 95, 103, 97, 108]
    

    1.7 Queue(队列)

    队列是一个典型的先进先出的容器。 LinkedList提供了方法以支持队列的行为,并且实现了Queue接口,因此LinkedList可以用作Queue的一种实现。

    public class QueueDemo {
    
        private static void printQ(Queue queue) {
            while (queue.peek() != null) {
                System.out.print(queue.remove() + " ");
            }
            System.out.println();
        }
    
        public static void main(String[] args) {
            Queue<Integer> queue = new LinkedList<>();
            Random rand = new Random(47);
            for (int i = 0; i < 10; i++) {
                queue.offer(rand.nextInt(10));
            }
            printQ(queue);
            Queue<Character> qc = new LinkedList<>();
            for (Character c : "ILoveYou".toCharArray()) {
                qc.offer(c);
            }
            printQ(qc);
        }
    }
    

    运行结果:

    8 5 3 1 1 9 8 0 2 7 
    I L o v e Y o u
    
    • offer方法是Queue相关的方法之一,在允许的情况下,将一个元素插入到队尾,或者返回false
    • peek()和element()都将在不移除的情况下返回队头,但是peek()方法在队列为空时返回null,而element()会抛出NoSuchElementException异常。
    • pull()和remove()方法将移除并返回队头,但是pull()在队列为空时返回null,而remove()会抛出NoSuchElementException异常。

    1.7.1 PriorityQueue

    优先级队列当我们在PriorityQueue中调用offer()方法来插入一个对象时,这个对象会在队列中被排序。我们还可以提供自己的Comparator来修改这个顺序。PriorityQueue可以确保当你调用peek()、poll和remove()方法时,获取的元素将是队列中优先级最高的元素。(Comparator.reverseOrder() 反序)

    public class PriorityQueueDemo {
        public static void main(String[] args) {
            PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
            Random rand = new Random(47);
            for (int i = 0; i < 10; i++) {
                priorityQueue.offer(rand.nextInt(10));
            }
            QueueDemo.printQ(priorityQueue);
            List<Integer> ints = Arrays.asList(14, 12, 13, 15, 17, 16);
            priorityQueue = new PriorityQueue<>(ints);
            QueueDemo.printQ(priorityQueue);
            priorityQueue = new PriorityQueue<>(ints.size(), Comparator.reverseOrder());
            priorityQueue.addAll(ints);
            QueueDemo.printQ(priorityQueue);
    
            String string = "I Love You";
            List<String> strings = Arrays.asList(string.split(""));
            PriorityQueue<String> stringPQ = new PriorityQueue<>(strings);
            QueueDemo.printQ(stringPQ);
            stringPQ = new PriorityQueue<>(strings.size(), Comparator.reverseOrder());
            stringPQ.addAll(strings);
            QueueDemo.printQ(stringPQ);
    
            Set<Character> charSet = new HashSet<>();
            for (char c : string.toCharArray()) {
                charSet.add(c);
            }
            PriorityQueue<Character> charPQ = new PriorityQueue<>(charSet);
            QueueDemo.printQ(charPQ);
        }
    }
    

    运行结果:

    0 1 1 2 3 5 7 8 8 9 
    12 13 14 15 16 17 
    17 16 15 14 13 12 
        I L Y e o o u v 
    v u o o e Y L I     
      I L Y e o u v
    

    相关文章

      网友评论

          本文标题:容器相关简单运用(一)

          本文链接:https://www.haomeiwen.com/subject/fbnysctx.html