容器相关简单运用(一)

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容