1. 程式人生 > >Java基礎(五十九)-集合工具類(Java類集框架)

Java基礎(五十九)-集合工具類(Java類集框架)

1:Stack棧

棧是一種先進後出的資料結構。例如:在文字編輯器上都有撤銷功能,那麼每次使用的時候,最後一次的編輯操作永遠是最先撤銷的,那麼這個功能就是利用棧來實現的,棧的基本操作形式如下。

在這裡插入圖片描述

案例:實現棧的操作

import java.util.Stack;

public class JavaAPIDemo {
	public static void main(String[] args) throws Exception {
		Stack<String> all = new Stack<String>() ;
		all.push("A") ;
		all.push("B") ;
		all.push("C") ;
		System.out.println(all.pop());
		System.out.println(all.pop());
		System.out.println(all.pop());
		System.out.println(all.pop());	// 無資料、EmptyStackException
	}
}

在這裡插入圖片描述

通過此時的操作可以發現,所有的儲存之後將按照倒序的形式進行彈出,如果棧已經空了,則會丟擲空棧異常。

在這裡插入圖片描述

2:Queue對列

Quene描述的是一個佇列。而佇列的主要特點是實現先進先出的操作形式。基本的操作形式如下。

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

案例:實現佇列操作

import java.util.LinkedList;
import java.util.Queue;

public class Test {
	public static void main(String[] args) throws Exception {
		Queue<String> queue = new LinkedList<String>() ;
		queue.offer("X") ;	// 追加佇列資料,通過隊尾追加
		queue.offer("A") ;	// 追加佇列資料,通過隊尾追加
		queue.offer("Z") ;	// 追加佇列資料,通過隊尾追加
		System.out.println(queue.poll()); // 彈出資料、X
		System.out.println(queue.poll()); // 彈出資料、A
		System.out.println(queue.poll()); // 彈出資料、Z
	}
}

在這裡插入圖片描述

除了LinkedList子類之外,還有一個優先順序佇列的概念,可以使用 PriorityQueue還是先優先順序佇列(比較功能)。

在這裡插入圖片描述

案例:使用優先順序佇列。

import java.util.PriorityQueue;
import java.util.Queue; 

public class JavaAPIDemo {
	public static void main(String[] args) throws Exception {
		Queue<String> queue = new PriorityQueue<String>() ;
		queue.offer("X") ;	// 追加佇列資料,通過隊尾追加
		queue.offer("A") ;	// 追加佇列資料,通過隊尾追加
		queue.offer("Z") ;	// 追加佇列資料,通過隊尾追加
		System.out.println(queue.poll()); // 彈出資料、X
		System.out.println(queue.poll()); // 彈出資料、A
		System.out.println(queue.poll()); // 彈出資料、Z
	}
}

對於佇列的選用原則也是需要根據實際的專案環境來決定的。

3:Properties屬性操作

在這裡插入圖片描述

import java.util.Properties;

public class Test {
	public static void main(String[] args) throws Exception {
		Properties prop = new Properties() ;
		// 設定的內容只允許是字串
		prop.setProperty("mldn", "www.mldn.cn") ;
		prop.setProperty("mldnjava", "www.mldnjava.cn") ;
		System.out.println(prop.getProperty("mldn"));
		System.out.println(prop.getProperty("sina","NoFound"));
		System.out.println(prop.getProperty("sina"));
	}
}

在這裡插入圖片描述
在這裡插入圖片描述
案例:將屬性內容儲存在檔案之中

import java.util.Properties;
import java.io.File;
import java.io.FileOutputStream;

public class Test {
	public static void main(String[] args) throws Exception {
		Properties prop = new Properties() ;
		// 設定的內容只允許是字串
		prop.setProperty("mldn", "www.mldn.cn") ;
		prop.setProperty("mldnjava", "www.mldnjava.cn") ;
		prop.setProperty("BeiJing", "北京");
		
		prop.store(new FileOutputStream(new File("D:"+File.separator+"info.properties")), "中文的註釋看不見的-English");
	}
}

在這裡插入圖片描述

通過程式的執行可以發現,的確可以實現資原始檔的輸入處理,但是如果輸出的是中文則自動幫助使用者進行處理。

案例:讀取資原始檔


import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

public class Test {
	public static void main(String[] args) throws Exception {
		Properties prop = new Properties() ;
		prop.load(new FileInputStream(new File("D:" + File.separator + "info.properties")));
		System.out.println(prop.getProperty("mldn"));
	}
}

在這裡插入圖片描述

使用Properties型別的最大特點是可以進行資源內容的輸入與輸出的梳理操作,但是在實際的開發之中Propertites往往用於讀取配置資源的資訊,這一點主要是在標準的設計之中做程式初始化時候使用。

4:Collections工具類

在這裡插入圖片描述


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {
	public static void main(String[] args) throws Exception {
		List<String> all = new ArrayList<String>();
		Collections.addAll(all, "Hello", "World", "MLDN");
		System.out.println(all);
	}
}

在這裡插入圖片描述

案例:資料的反轉

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test{
	public static void main(String[] args) throws Exception {
		List<String> all = new ArrayList<String>();
		Collections.addAll(all, "Hello", "World", "MLDN");
		Collections.reverse(all);
		System.out.println(all);
	}
}

範例:使用二分查詢

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {
	public static void main(String[] args) throws Exception {
		List<String> all = new ArrayList<String>();
		Collections.addAll(all, "Hello", "World", "MLDN");
		Collections.sort(all) ; // 先進行排序處理
		System.out.println(all);
		System.out.println(Collections.binarySearch(all, "MLDN"));
	} 
}

在這裡插入圖片描述

在這裡插入圖片描述