DAY15:尚学堂高琪JAVA(129~131)

其他容器

DAY15:尚学堂高琪JAVA(129~131)

队列的实例1:

package othercollection;
/*
 * 使用队列模拟银行存款业务
 * */
import java.util.ArrayDeque;
import java.util.Queue;
public class Demo1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Queue<Request> queue = new ArrayDeque<>();
		// 模拟排队情况
		for (int i = 0; i < 10; i++) {
			final int num=i;
			queue.offer(new Request() {
				@Override
				public void deposit() {
					// TODO Auto-generated method stub
					System.out.println("第"+num+"个人办理存款业务,额度为:"+(float)Math.random()*1000);
				}

			});
		}
		dealWith(queue);
	}
	//处理业务
	public static void dealWith(Queue<Request> que){
		Request re=null;
		while(null!=(re=que.poll())){
			re.deposit();
		}
	}

	interface Request {
		// 存款
		void deposit();
	}

}

队列的实例2:

package othercollection;
/*
 * 129 使用队列实现自定义堆栈
 * */
public class Demo2 {
	public static void main(String[] args) {
		MyStack<String> history=new MyStack<String>(3);
		history.push("www.baidu.com");
		history.push("www.google.com");
		history.push("www.taobao.com");
		history.push("www.qq.com");
		
		System.out.println(history.size());
		String item=null;
		while ( null!=(item=history.pop())) {//pollLast有取出并移除的功能
			System.out.println(item);
		}
		System.out.println(history.size());
	}
}

package othercollection;
/*
 * 129 使用队列实现自定义堆栈
 * */
import java.util.ArrayDeque;
import java.util.Deque;

public class MyStack<E> {
	private Deque<E> container=new ArrayDeque<E>();
	private int cap;
	public MyStack(int cap) {
		super();
		this.cap = cap;
	}
	//实现进栈
	public boolean push(E e) {
		if (container.size()+1>cap) {
			return false;
		}
		return container.offerLast(e);
	}
	//出栈
	public E pop(){
		return container.pollLast();
	}
	//获取
	public E peek(){
		return container.peekLast();
		
	}
	public int size() {
		return this.container.size();
	}
}

DAY15:尚学堂高琪JAVA(129~131)

Enumeration

枚举,作用和Iterator类似,都是输出数据
方法:

  • hasMoreElements()
  • nextElement()
    实例:
package otherenumeration;
import java.util.Enumeration;
import java.util.Vector;
/*
 * 130 Enumeration的使用
 *  - hasMoreElements()
 	- nextElement()

 * */
public class Demo1 {
	@SuppressWarnings("rawtypes")
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Vector<String> v1=new Vector<String>();
		v1.add("java");
		v1.add("html");
		v1.add("python");
		
		Enumeration en=  v1.elements();
		while (en.hasMoreElements()) {
			System.out.println(en.nextElement());
			
		}
	}

}

DAY15:尚学堂高琪JAVA(129~131)

Hashtable

一:和HashMapd 区别:
1.Hashtable线程安全,同步,效率相对低
2.Hashtable的父类:Dictionary。HashMapd是AbstractMap
3.null : Hashtable的<k,v>都不为空
HashMapd的k最多一个null,值可以多个null

二:Properties
1.作用:读写资源配置文件
2.kv只能为字符串
3.方法:
setProperties(String key,String value)
getProperties(String key)
getProperties(String key, String defaultValue)

package properties;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 131 使用Properties输出到文件
		资源配置文件:
		1.  	.properties
			 	store(OutputStream out,String comments)
		   		store(Writer w,String comments)
		2.		.xml
				storetoXML(OutputStream os,String comments)
				storetoXML(OutputStream os,String comments,String encoding)
 * */
import java.util.Properties;

public class Demo02 {

	public static void main(String[] args) throws FileNotFoundException, IOException {
		// TODO Auto-generated method stub
		Properties p1=new Properties();
		//存储
		p1.setProperty("driver","oracle.jdbc.driver.OracleDriver");
		p1.setProperty("url","jdbc:oracle:thin:@localhost:1521:orcl");
		p1.setProperty("user","scott");
		p1.setProperty("pwd","935743");
		//获取
		String url=p1.getProperty("url","test");
		System.out.println(url);
		//存储到H:\\1\\(绝对路径)
		p1.store(new FileOutputStream(new File("H:\\1\\db.properties")),"db配置");
		p1.storeToXML(new FileOutputStream(new File("H:\\1\\db.xml")), "db配置");
		//存储 (相对路径)
		p1.store(new FileOutputStream(new File("src/db.properties")),"db配置");
 
	}

}

package properties;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
/*
 * 131 使用Properties读取资源配置文件
 *  
 * */
public class Demo03 {

	public static void main(String[] args) throws FileNotFoundException, IOException {
		// TODO Auto-generated method stub
		Properties p2=new Properties();
		//读取绝对路径
		p2.load(new FileReader("H:\\1\\db.properties"));
		System.out.println(p2.getProperty("user","test"));
		 //使用相对路径读取配置文件
		p2.load(Demo03.class.getResourceAsStream("/db.properties"));
		p2.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
		System.out.println(p2.getProperty("user","test"));
	}

}

DAY15:尚学堂高琪JAVA(129~131)

上一篇:F004-炒房客的流派(四)-----药单流 #490


下一篇:【力扣算法】131-分割回文串