JavaSE學習 第十一章 集合與映射

來源:互聯網
上載者:User

程式人生系列之JavaSE學習 集合與映射

1.集合架構

三個主要內容: Set   List   Map

關係結構:


2. Collection 介面

Set 和 List 的父介面,專門定義了一些公用的方法

3. Set 和 List 介面:兩者對父介面 Collection 進行了不同的擴充

Set 是集合,不能包含相同的元素

List 是列表,可以有相同的元素

4. ArrayList :長度可變的數組

範例程式碼:

import java.util.Date;

import java.util.ArrayList;

public class TestArrayList{

public static void main(String[] args) {

ArrayList h = new ArrayList();

h.add("1st");

h.add("2nd");

h.add(new Integer(3));

h.add(new Double(4.0));

h.add("2nd");      // 重複元素, 加入

h.add(new Integer(3)); // 重複元素,加入

System.out.println(h);

System.out.println("size=" + h.size());

System.out.println("-----替換指定元素-----");

h.set(3,"replace");

System.out.println("-----for迴圈遍曆-----");

for(int i=0;i<h.size();i++){

System.out.println(h.get(i));

}

System.out.println("-----取用特定元素-----");

Integer it = (Integer)h.get(2);

System.out.println(it.intValue());

System.out.println("-----插入元素-----");

h.add(3,new Date());

System.out.println("-----轉換為數組-----");

Object[] os = h.toArray();

for(Object o : os){

System.out.println(o);

}

}

}

測試結果:

5.Vector  : 注意它和ArrayList 的區別

範例程式碼:

import java.util.Date;

import java.util.Vector;

public class TestVector{

public static void main(String[] args) {

Vector v = new Vector();

v.add("1st");

v.add("2nd");

v.add(new Integer(3));

v.add(new Double(4.0));

v.add("2nd");      // 重複元素, 加入

v.add(new Integer(3)); // 重複元素,加入

System.out.println(v);

System.out.println("size=" + v.size());

System.out.println("-----替換指定元素-----");

v.set(3,"replace");

System.out.println("-----for迴圈遍曆-----");

for(int i=0;i<v.size();i++){

System.out.println(v.get(i));

}

System.out.println("-----取用特定元素-----");

Integer it = (Integer)v.get(2);

System.out.println(it.intValue());

System.out.println("-----插入元素-----");

v.add(3,new Date());

System.out.println("-----轉換為數組-----");

Object[] os = v.toArray();

for(Object o : os){

System.out.println(o);

}

}

}

測試結果:

6. Stack :棧,很重要的一種資料結構,它繼承了 Vector 類

範例程式碼:

import java.util.Date;

import java.util.Stack;

public class TestStack{

public static void main(String[] args) {

Stack s = new Stack();

s.push("hello");

s.push(new Date());

s.push(400);  //自動封裝,等價於s.push(new Integer(400));

s.push(3.14);

System.out.println("彈棧前:size=" + s.size());

System.out.println(s.pop());

System.out.println("彈棧後:size=" + s.size());

System.out.println(s.peek());

System.out.println("peek操作後:size=" + s.size());

while(!s.isEmpty())

System.out.println(s.pop());

}

}

測試結果:

7. Iterator 介面:迭代器,很重要,對於實現了 List介面的類提供了一個統一的遍曆方式

範例程式碼:

import java.util.Date;

import java.util.ArrayList;

import java.util.Vector;

import java.util.Iterator;

public class TestIterator{

public static void main(String[] args) {

ArrayList a = new ArrayList();

a.add("China");

a.add("USA");

a.add("Korea");

Iterator  it = a.iterator();

while(it.hasNext()){

String country = (String)it.next();

System.out.println(country);

}

Vector v = new Vector();

v.addElement(new Date());

v.addElement(new Date(200008755554L));

it = v.iterator();

while(it.hasNext()){

Date time = (Date)it.next();

System.out.println(time);

}

}

}

測試結果:

8.TreeSet :可以實現排序功能的集合,預設是字典序

   HashSet

範例程式碼:

import java.util.TreeSet;

import java.util.Iterator;

public class TestTreeSet{

public static void main(String[] args) {

TreeSet ts = new TreeSet();

ts.add("orange");

ts.add("banana");

ts.add("apple");

ts.add("grape");

Iterator it = ts.iterator();

while(it.hasNext()){

String fruit = (String)it.next();

System.out.println(fruit);

}

}

}

測試結果:

範例程式碼:

import java.util.Date;

import java.util.HashSet;

import java.util.Iterator;

public class TestHashSet{

public static void main(String[] args) {

HashSet h = new HashSet();

h.add("1st");

h.add("2nd");

h.add(new Integer(3));

h.add(new Double(4.0));

h.add("2nd");       // 重複元素, 未被加入

h.add(new Integer(3)); // 重複元素,未被加入

h.add(new Date());

System.out.println("開始: size=" + h.size());

Iterator it = h.iterator();

while(it.hasNext()){

Object o = it.next();

System.out.println(o);

}

h.remove("2nd");

System.out.println("移除元素後: size=" + h.size());

System.out.println(h);

}

}

測試結果:

9.Comparable 介面:重寫compareTo方法可以定製比較邏輯

範例程式碼:

  ----------------------------------->   Person.java

public class Person implements java.lang.Comparable{

private final int id;

private String name;

private int age;

public Person(int id,String name,int age){

this.id = id;

this.name = name;

this.age = age;

}

public int getId(){

return id;

}

public void setName(String name){

this.name = name;

}

public String getName(){

return name;

}

public void setAge(int age){

this.age = age;

}

public int getAge(){

return age;

}

public String toString(){

return "Id: " + id + "\tName: " + name + "\tAge: " + age;

}

@Override

public int compareTo(Object o){

Person p = (Person)o;

return this.id - p.id;

}

@Override

public boolean equals(Object o){

boolean flag = false;

if(o instanceof Person){

if(this.id == ((Person)o).id)

flag = true;

}

return false;

}

}

------------------------------------------> 測試類別

import java.util.TreeSet;

import java.util.Iterator;

public class TestComparable{

public static void main(String[] args) {

TreeSet ts = new TreeSet();

ts.add(new Person(1003,"張三",15));

ts.add(new Person(1008,"李四",25));

ts.add(new Person(1015,"王五",73));

ts.add(new Person(1001,"趙六",49));

Iterator it = ts.iterator();

while(it.hasNext()){

Person employee = (Person)it.next();

System.out.println(employee);

}

}

}

10.Map 介面:以索引值對的形式儲存資料

11.HashMap:基於雜湊表的映射集合結構

----------------->person.java

public class Person implements java.lang.Comparable{

private final int id;

private String name;

private int age;

public Person(int id,String name,int age){

this.id = id;

this.name = name;

this.age = age;

}

public int getId(){

return id;

}

public void setName(String name){

this.name = name;

}

public String getName(){

return name;

}

public void setAge(int age){

this.age = age;

}

public int getAge(){

return age;

}

public String toString(){

return "Id: " + id + "\tName: " + name + "\tAge: " + age;

}

@Override

public int compareTo(Object o){

Person p = (Person)o;

return this.id - p.id;

}

@Override

public boolean equals(Object o){

boolean flag = false;

if(o instanceof Person){

if(this.id == ((Person)o).id)

flag = true;

}

return false;

}

}

-------------------------------------> 測試類別

import java.util.Set;

import java.util.HashMap;

import java.util.Collection;

import java.util.Iterator;

public class TestHashMap{

public static void main(String[] args) {

HashMap hm = new HashMap();

hm.put(new Integer(1003),new Person(1003,"張三",15));

hm.put(new Integer(1008),new Person(1008,"李四",25));

hm.put(1015,new Person(1015,"王五",73));   //自動封裝

hm.put(1001,new Person(1001,"趙六",49));

System.out.println("----檢索單個元素----");

Person p = (Person)hm.get(1008);

System.out.println(p);

System.out.println("----遍曆所有\"鍵\"(元素名)----");

Set names = hm.keySet();

for(Object o : names){

System.out.println(o);

}

System.out.println("----遍曆所有\"值\"(元素值)----");

Collection values = hm.values();

for(Object o : values){

System.out.println(o);

}

}

}

HashTable 與 HashMap 基本上相同的,但是也有一些區別

安全執行緒的一般都是用於多線程的環境中

12. Arrays 類:數組類,裡面有很多的靜態方法可供數組直接調用

範例程式碼:

import java.util.List;

import java.util.Arrays;

public class TestArrays{

public static void main(String[] args) {

Integer[] a = {3,25,12,79,48};

System.out.println(a);

System.out.println(Arrays.toString(a));

Arrays.sort(a);

System.out.println(Arrays.toString(a));

int idx = Arrays.binarySearch(a,25);

System.out.println(idx);

List list = Arrays.asList(3,4,5);

System.out.println(list);

list.set(2,66);

System.out.println(Arrays.toString(a));

}

}

測試結果:

13. Collections 類:裡面定義了很多的對集合進行操作的方法

範例程式碼:

import java.util.Vector;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Enumeration;

public class TestCollections{

public static void main(String[] args) {

ArrayList alist = new ArrayList();

alist.add(75);

alist.add(38);

alist.add(21);

alist.add(4);

alist.add(12);

System.out.println("原列表: " + alist);

Collections.sort(alist);

System.out.println("排序後: " + alist);

Collections.reverse(alist);

System.out.println("逆序後: " + alist);

Collections.shuffle(alist);

System.out.println("混排後: " + alist);

Collections.rotate(alist,2);

System.out.println("移位後: " + alist);

ArrayList blist = new ArrayList();

blist.add(55);

blist.add(66);

System.out.println("新列表: " + blist);

Collections.copy(alist,blist);

System.out.println("拷貝後: " + alist);

System.out.println("列表中出現元素66的次數: " + Collections.frequency(alist,66));

System.out.println("列表中元素的最大值: " + Collections.max(alist));

System.out.println("列表中元素的最小值: " + Collections.min(alist));

Enumeration en = createDemoEnumeration();

ArrayList clist = Collections.list(en);

System.out.println("Enumeration->ArrayList: " + alist);

}

public static Enumeration createDemoEnumeration(){

Vector v = new Vector();

v.add("Tom");

v.add("Billy");

v.add("Jenny");

return v.elements();

}

}

測試結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.