Java的一些高级特性(四)——集合

来源:互联网 发布:程序员35岁以后干什么 编辑:程序博客网 时间:2024/04/30 04:05

Java的集合类型主要有如下几种:

Set/Queue/List/Set/Tree/Stack等。


我们首先来看一个HashSet的用法:

package com.freesoft.javaadvanced;import java.util.HashSet;import com.freesoft.testentity.Kalamata;import com.freesoft.testentity.Ligurio;import com.freesoft.testentity.Olive;import com.freesoft.testentity.Picholine;public class TestHashSet {public static void printSize(HashSet<Olive> olives){System.out.println("There are " + olives.size() + " olives in the set.");}public static void main(String[] args) {HashSet<Olive> olives = new HashSet<>();Olive k = new Kalamata();Olive l = new Ligurio();Olive p = new Picholine();olives.add(k);olives.add(l);printSize(olives);olives.add(p);printSize(olives);// 由于HashSet内部元素需要通过Hash Value识别,所以同一个元素无法保存两次olives.add(p);printSize(olives);// HashSet中可以添加nullolives.add(null);printSize(olives);// 删除元素olives.remove(k);printSize(olives);}}


注意每次运行的时候HashSet内元素的顺序不是一成不变的。



接下来我们看看如何使用TreeSet。TreeSet是一个自动排序的集合,所以我们需要对泛型对象的类实现Comparable接口:

package com.freesoft.testentity;public class Olive implements Comparable<Olive>{public static final long BLACK = 0x000000;private String name;private long color = BLACK;public Olive(){}public Olive(String name) {// 由于字段color已经被设置为默认值为BLACK,故这里无需再次设置this.name = name;}public Olive(String name, long value) {super();this.name = name;this.color = value;}@Overridepublic String toString() {return "Olive [name=" + name + ", color=" + color + "]";}@Overridepublic int compareTo(Olive arg0) {String s1 = this.name;String s2 = arg0.name;return s1.compareTo(s2);}}

我们再按照如下的规则实现子类:

package com.freesoft.testentity;public class Kalamata extends Olive {public Kalamata() {super("Kalamata");}}

最后我们的测试程序是这样:

package com.freesoft.javaadvanced;import java.util.HashSet;import java.util.TreeSet;import com.freesoft.testentity.Kalamata;import com.freesoft.testentity.Ligurio;import com.freesoft.testentity.Olive;import com.freesoft.testentity.Picholine;public class TestTreeSet {public static void printSize(TreeSet<Olive> olives){System.out.println("There are " + olives.size() + " olives in the set.");}public static void main(String[] args) {Olive l = new Ligurio();Olive p = new Picholine();Olive k = new Kalamata();TreeSet<Olive> olives = new TreeSet<>();try {olives.add(k);olives.add(l);olives.add(p);} catch (Exception e) {e.printStackTrace();}System.out.println(olives);}}

从上面的程序可以看出,随便我们添加的时候的元素顺序是什么,TreeSet在添加的过程中会自动按照排序顺序插入元素。



我们在来看看LinkedList这个列表:

package com.freesoft.javaadvanced;import java.util.LinkedList;import com.freesoft.testentity.Kalamata;import com.freesoft.testentity.Ligurio;import com.freesoft.testentity.Olive;import com.freesoft.testentity.Picholine;public class TestLinkedList {public static void main(String[] args) {Olive k = new Kalamata();Olive l = new Ligurio();Olive p = new Picholine();LinkedList<Olive> list = new LinkedList<>();list.add(k);list.add(l);list.add(p);System.out.println(list);// 这里的add实际上是执行插入操作,且列表中的元素可以反复添加list.add(2, k);System.out.println(list);// 改变元素属性,该对象在列表中的引用也会及时更新k.setColor(0xff0000);System.out.println(list);}}

最后我们看看队列。队列的方法比较简单,就是一个先进先出(FIFO)的操作:

我们先看一下之前的list,我们通过帮助得知LinkedList实现了Queue接口。注意体会peek和poll的区别,一个删除元素一个不删除。

package com.freesoft.javaadvanced;import java.util.LinkedList;import com.freesoft.testentity.Kalamata;import com.freesoft.testentity.Ligurio;import com.freesoft.testentity.Olive;import com.freesoft.testentity.Picholine;public class TestLinkedList {public static void main(String[] args) {Olive k = new Kalamata();Olive l = new Ligurio();Olive p = new Picholine();LinkedList<Olive> olives = new LinkedList<>();olives.add(k);olives.add(l);olives.add(p);System.out.println(olives);// 这里的add实际上是执行插入操作,且列表中的元素可以反复添加olives.add(2, k);System.out.println(olives);// 改变元素属性,该对象在列表中的引用也会及时更新k.setColor(0xff0000);System.out.println(olives);Olive o1 = olives.poll();System.out.println(o1);System.out.println(olives);}}




原创粉丝点击