javaSE高階開發之集合類——棧與佇列
阿新 • • 發佈:2019-01-08
一、棧
package com.wschase.hashmap; import java.util.Stack; /** * Author:WSChase * Created:2019/1/7 */ ////一、棧--》先進後出 // //在生活中有很多這樣的例子,就是先進後廚,這些的原理都是堆疊的原理 // //比如:瀏覽器的關閉、編輯器的撤銷、座位等等 // //在我們的java中提供了一個stack類,這個類是vector的子類 // //其中:入棧是push 出棧用pop //public class TestStack { // public static void main(String[] args) { // Stack<String> stack=new Stack<>(); // System.out.println("當前棧是否為空:"+stack.isEmpty()); // //入棧 // stack.push("java"); // stack.push("C++"); // stack.push("PHP"); // System.out.println("當前棧是否為空:"+stack.isEmpty()); // System.out.println("當前棧的元素個數:"+stack.size()); // //觀察棧頂——>這個peek()就是取得棧頂元素的函式 // System.out.println("棧頂是否是PHP:"+"PHP".equals(stack.peek())); // // //出棧 // System.out.println(stack.pop());//PHP——>出棧的元素 // System.out.println(stack.pop());//C++ // System.out.println(stack.pop());//java // System.out.println(stack.pop());//?(此時棧為空) // // //重要:如果我們的棧空會拋異常,所以我們出棧的時候需要判斷一下我們的棧是否為空 // while ((!stack.isEmpty())){ // System.out.println(stack.pop()); // } // } //}
二、佇列
package com.wschase.hashmap; import java.util.LinkedList; import java.util.Queue; /** * Author:WSChase * Created:2019/1/7 */ //二、佇列-->先進先出 //解決高併發:我們把程式變成分散式-->將資料庫同樣分佈10個,會分佈分表(還可以備份) public class TestQueue { //1.佇列的常見操作 public static void code1(){ LinkedList<String> queue=new LinkedList<>(); System.out.println("對列的元素是否為空"+queue.isEmpty()); //入佇列 queue.add("java"); queue.add("C++"); queue.add("PHP"); System.out.println("佇列的元素是否為空"+queue.isEmpty()); System.out.println("佇列的數量:"+queue.size()); //檢視隊頭 System.out.println("隊頭元素為:"+"PHP".equals(queue.peek())); //出佇列 System.out.println(queue.poll());//java System.out.println(queue.poll());//C++ System.out.println(queue.poll());//PHP System.out.println(queue.poll());//?-->注意:當我們的佇列為空的時候和棧不同,他不是指標異常,而是返回一個值null } //2.佇列的應用 //生產者-消費者模型 //生產者和消費者的能力不匹配(生產的速度和消費的速度不匹配) //採用佇列的方式存在生產消費的資源(元素),解耦生產者和消費者的實現邏輯 public static void code2(){ Queue<String> queue=new LinkedList<>(); //生產者 new Thread(new Runnable() { { System.out.println("生產者執行緒啟動"); } @Override public void run() { while(true){ try{ Thread.sleep(1000); //生產資料 String data = String.valueOf(Math.random()); System.out.println("生產者:"+data); queue.add(data); }catch(InterruptedException e){ e.printStackTrace(); } } } }).start(); //消費者 new Thread(new Runnable() { { System.out.println("消費者執行緒啟動"); } @Override public void run() { while(true){ try{ Thread.sleep(1000); System.out.println("消費者:"+queue.poll()); }catch(InterruptedException e){ e.printStackTrace(); } } } }).start(); } public static void main(String[] args) { } }
三、Properties(檔案)
package com.wschase.hashmap; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; /**檔案:當我們想要獲取一些檔案資訊的時候我們就使用Properties這個類去直接讀取就可以了,這樣比較方便,不需要我們只用資料流 * Author:WSChase * Created:2019/1/7 */ public class TestProperties { public static void main(String[] args) throws IOException { //*.properties 稱之為屬性檔案 //Properties 稱之為屬性類 //*.properties->Properties //讀取檔案:lode->Properties InputStream/Reader //寫入檔案:store->*.properties OutputStream/Writer try{ //1.通過檔案FileInputStream Properties properties=new Properties(); properties.load(new FileInputStream("E:\\javasecode\\Javayuandaima\\bit-JAVA\\java-idea-1-7-collection2\\src\\com\\wschase\\hashmap\\hello.properties")); //讀的方法: //1.getProperties(key) //2.getProperty(key,defaultValue) System.out.println(properties.get("C++")); System.out.println(properties.getProperty("C++"));//獲得屬性 System.out.println(properties.get("php")); System.out.println(properties.get("C++")); System.out.println(properties.getProperty("php","php is best")); //寫的方法: //1.put //2.setProperty(建議使用這個) properties.put("php","php is good"); properties.setProperty("Go","go is best"); //儲存 properties.store(new FileOutputStream("E:\\javasecode\\Javayuandaima\\bit-JAVA\\java-idea-1-7-collection2\\src\\com\\wschase\\hashmap\\hello1.properties")); }catch(IOException e){ e.printStackTrace(); } } }