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);}}
- Java的一些高级特性(四)——集合
- Java的一些高级特性(一)——Java 7新特性
- Java的一些高级特性(二)——初始化和赋值部分
- Java的一些高级特性(三)——类部分
- Java的一些高级特性(三)——关于反射
- Java的一些高级特性(五)——测试和异常处理
- Java的一些高级特性(六)——Java7中的目录和文件管理
- Java的一些高级特性(七)——Java7中的I/O
- Java的一些高级特性(八)——Java7中的线程
- java的一些高级特性总结
- java基础:高级类特性(四)
- JAVA的一些高级知识集合
- JAVA的一些高级知识集合
- java高级特性之--集合
- java高级特性:集合框架
- JAVA高级工程师课程笔记整理——(四)高级特性
- Python学习笔记(四)——高级特性
- Python 的一些高级特性
- 阿里入股新浪微博:动机与前景分析
- 不使用 Ruby 的十个理由
- 三国杀之无线网络安全解析
- Windows进程系列(2) -- Svchost进程
- 数据挖掘竞赛
- Java的一些高级特性(四)——集合
- 千万要避免的五种程序注释方式
- 字符串笔记(五)
- 神印落幕 远去的繁华
- 卡巴斯基:只有合作才能捍卫网络安全
- git using
- 用数据说话!IE10 和 Chrome 等四大浏览器横评
- union:C/C++语言关键字
- 字符串笔记(六)