java09 队列Queue与Deque
队列Queue与Deque.
Enumeration
Hashtable与Hashtable子类Properties(资源配置文件)
引用类型(强、软、弱、虚)与WeakHashMap
IdentitvHashMap与EnumMap
同步控制与只读设置
开源工具包:
-Guava:Google Collection
-Apache:Commons Collection
容器总结 队列:
-单向队列(一端访问)
-一般队列:FIFO,先进先出。 -特殊队列:优先级队列和堆栈LIFO,后进先出。浏览器历史也是使用堆栈实现的,后进先出。 方法:插入add(e)、offer(e),移除remove()、poll(),获取element()、peek()
-双向队列(两端访问) 方法:插入第一个元素addFirst(e)、offerFirst(e)、push(e),插入最后一个元素addLast(e)、offerLast(e)、add(e)、offer(e),移除第一个元素removeFirst()、pollFirst()、remove()、pop()、poll(),移除最后一个元素removeLast()、pollLast(),获取第一个元素getFirst()、peekFirst()、element()、peek(),获取最后一个元素getLast()、peekLast() /**
* 使用队列模拟银行存款业务
*/
public class Demo01 {
/*public interface Queue<E> extends Collection<E> {//jdk
boolean add(E e);
boolean offer(E e);
E remove();
E poll();
E element();
E peek();
}*/
public static void main(String[] args) {
Queue<Request> que =new ArrayDeque<Request>();
//模拟排队情况
for(int i=;i<;i++){
final int num =i;
que.offer(new Request(){//匿名内部类对象,只能访问final修饰的变量。
@Override
public void deposit() { System.out.println("第"+num+"个人,办理存款业务,存款额度为:"+(Math.random()*));
}
});
}
dealWith(que);
}
//处理业务
public static void dealWith(Queue<Request> que){//先进先出
Request req =null;
while(null!=(req=que.poll())){
req.deposit();
}
}
}
interface Request{
//存款
void deposit();
} public class Demo02 {
public static void main(String[] args) {
MyStack<String> backHistory =new MyStack<String>();
backHistory.push("www.baidu.com");
backHistory.push("www.google.com");
backHistory.push("www.sina.com");
backHistory.push("www.bjsxt.cn"); System.out.println("大小:"+backHistory.size()); //遍历
String item=null;
while(null!=(item=backHistory.pop())){
System.out.println(item);
}
/*后进先出
输出:www.sina.com
www.google.com
www.baidu.com*/
}
} Enumeration接口:
和Iterator一样,Iterator取代了Enumeration。Enumeration用于jdk1.5之前。
public class Demo01 { public static void main(String[] args) {
Vector<String> vector =new Vector<String>();
vector.add("javase");
vector.add("html");
vector.add("oracle");
//遍历该Vector
/* public Enumeration<E> elements() {//jdk的源码
return new Enumeration<E>() {
int count = 0;
public boolean hasMoreElements() {
return count < elementCount;
}
public E nextElement() {
synchronized (Vector.this) {
if (count < elementCount) {
return (E)elementData[count++];
}
}
throw new NoSuchElementException("Vector Enumeration");
}
};
}*/
Enumeration<String> en =vector.elements();//
while(en.hasMoreElements()){
System.out.println(en.nextElement());
}
}
} /**
* Enumeration 子类
* StringTokenizer类似于String split() 字符串分割
* 不支持正则表达式,split()支持正则表达式,
* StringTokenizer(String str, String delim)
*/
public class Demo02 {
public static void main(String[] args) {
String emailStr="bjsxt@163.com;bjsxt@qq.com;bjsxt@sohu.com";
StringTokenizer token =new StringTokenizer(emailStr,";");//实现了Enumeration接口。所以有hasMoreElements和nextElement方法。
//遍历获取
while(token.hasMoreElements()){
System.out.println(token.nextElement());
}
}
} Hashtable:Map实现类,与HashMap操作相同。
HashMap线程不安全,效率相对高,键最多一个null,值可以为多个null,父类AbstrctMap。
Hashtable线程安全(同步的),效率相对低下,键与值都不能为null,父类Dictionary(字典)。
Properties为Hashtable的子类,Properties经常用于读写资源配置文件,键与值只能为字符串。 方法:
setProperty(String key, String value)
getProperty(String key)
getProperty(String key, String defaultValue) 后缀为.properties的文件的存储和读操作
store(OutputStream out, String comments)
store(Writer writer, String comments)
load(InputStream inStream)
load(Reader reader)
后缀为.xml文件的存储和读取
storeToXML(OutputStream os, String comment)//默认UTF-8字符集
storeToXML(OutputStream os, String comment,String encoding)
loadFromXML(InputStream in) 相对路径和绝对路径:
.绝对路径:windows要指定盘符,Linux和Unix要用/
.相对路径,相对当前工程,
.根据类路径加载资源文件:
类所在的跟路径:
类.class.getResourceAsStream("/"),
Thread.currentThread().getContextClassLoader().getResourceAsStream("") import java.util.Properties;
/**
* Properties 资源配置文件的读写,
* 1、Properties只能存储字符串,因此key 与value 只能为字符串
* 2、存储与读取
* setProperty(String key, String value)
* getProperty(String key, String defaultValue)
* @author Administrator
*
*/
public class Demo01 {
public static void main(String[] args) {
//创建Properties对象
Properties pro =new Properties();
//存储
pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
pro.setProperty("user", "scott");
pro.setProperty("pwd", "tiger");
//获取
String url =pro.getProperty("url","test");//后面是默认值
System.out.println(url);
}
} import java.util.Properties;
/**
* 使用Properties输出(写入到)到文件
* 资源配置文件(可以动态的切换数据库):
*
* 1、.properties
* store(OutputStream out, String comments)
store(Writer writer, String comments)
2、.xml
storeToXML(OutputStream os, String comment) :UTF-8字符集
storeToXML(OutputStream os, String comment, String encoding) * @author Administrator
*
*/
public class Demo02 { public static void main(String[] args) throws FileNotFoundException, IOException {
//创建对象
Properties pro =new Properties();
//存储
pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
pro.setProperty("user", "scott");
pro.setProperty("pwd", "tiger"); //存储到e:/others 绝对路径有盘符:
pro.store(new FileOutputStream(new File("e:/others/db.properties")), "db配置");//others文件夹要存在,把pro对象的内容写入到文件。
/*键值对
#db\u914D\u7F6E//这是注释
#Mon Jul 21 13:03:09 CST 2014
user=scott
url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
driver=oracle.jdbc.driver.OracleDriver
pwd=tiger*/ pro.storeToXML(new FileOutputStream(new File("e:/others/db.xml")), "db配置");
/*key和value
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>db配置</comment>
<entry key="user">scott</entry>
<entry key="url">jdbc:oracle:thin:@localhost:1521:orcl</entry>
<entry key="driver">oracle.jdbc.driver.OracleDriver</entry>
<entry key="pwd">tiger</entry>
</properties>*/ //使用相对路径,默认的相对路径是当前的工程。下面是在3处存了这个属性文件。
pro.store(new FileOutputStream(new File("db.properties")), "db配置");
pro.store(new FileOutputStream(new File("src/db.properties")), "db配置");
pro.store(new FileOutputStream(new File("src/com/bjsxt/others/pro/db.properties")), "db配置");
}
} import java.util.Properties;
/**
* 使用Properties读取配置文件,写一次读多次。
* 资源配置文件:
* 使用相对与绝对路径读取
* load(InputStream inStream)
load(Reader reader)
loadFromXML(InputStream in)
*/
public class Demo03 {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pro=new Properties();
//读取 绝对路径
pro.load(new FileReader("e:/others/db.properties"));
//读取 相对路径
//给客户的时候只给class文件(字节码文件),不会给源代码,src是源代码文件路径
pro.load(new FileReader("src/com/bjsxt/others/pro/db.properties"));
System.out.println(pro.getProperty("user", "bjsxt"));
}
} import java.util.Properties;
/**
* 使用类相对路径读取配置文件
* bin 路径
*/
public class Demo04 {
public static void main(String[] args) throws IOException {
Properties pro =new Properties();
//类相对路径,第一个/表示根目录(class文件中表示bin目录) ,Demo04.class.getResourceAsStream("/")表示当前类所在的跟路径(这里就是src目录)。
pro.load(Demo04.class.getResourceAsStream("/com/bjsxt/others/pro/db.properties"));
//Thread.currentThread()当前线程(main线程),getContextClassLoader上下文的类加载器,Thread.currentThread().getContextClassLoader("")当前类所在的跟路径(这里就是src目录),class文件中表示bin目录
pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/bjsxt/others/pro/db.properties"));
System.out.println(pro.getProperty("user", "bjsxt"));
}
}
java09 队列Queue与Deque的更多相关文章
- STL中的单向队列queue
转载自:http://blog.csdn.net/morewindows/article/details/6950917 stl中的queue指单向队列,使用时,包含头文件<queue>. ...
- java-Enumeration,单向队列Queue及双向队列Deque等容器简单使用
1.Enumeration容器使用: package com.etc; import java.util.Enumeration; import java.util.Vector; /* Enumer ...
- Python 双向队列Deque、单向队列Queue 模块使用详解
Python 双向队列Deque 模块使用详解 创建双向队列Deque序列 双向队列Deque提供了类似list的操作方法: #!/usr/bin/python3 import collections ...
- Python进阶【第二篇】多线程、消息队列queue
1.Python多线程.多进程 目的提高并发 1.一个应用程序,可以有多进程和多线程 2.默认:单进程,单线程 3.单进程,多线程 IO操作,不占用CPU python的多线程:IO操作,多线程提供并 ...
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)(转)
向量(vector) <vector> 连续存储的元素<vector> Vector<int>c; c.back() 传回最后一个数据,不检查这个数据是否存在 ...
- 队列Queue和栈
1.队列Queue是常用的数据结构,可以将队列看成特殊的线性表,队列限制了对线性表的访问方式,只能从线性表的一段添加(offer)元素, 从另一段取出(poll)元素,队列遵循先进先出的原则. 2.J ...
- Java中的queue和deque对比详解
队列(queue)简述 队列(queue)是一种常用的数据结构,可以将队列看做是一种特殊的线性表,该结构遵循的先进先出原则.Java中,LinkedList实现了Queue接口,因为LinkedLis ...
- 【java多线程】队列系统之说说队列Queue
转载:http://benjaminwhx.com/2018/05/05/%E8%AF%B4%E8%AF%B4%E9%98%9F%E5%88%97Queue/ 1.简介 Queue(队列):一种特殊的 ...
- Java 中的队列 Queue
一.队列的定义 我们都知道队列(Queue)是一种先进先出(FIFO)的数据结构,Java中定义了java.util.Queue接口用来表示队列.Java中的Queue与List.Set属于同一个级别 ...
随机推荐
- matlab 画图
先前讲解了简单绘图方法: http://www.cnblogs.com/youxin/p/3859923.html x = 0:pi/100:2*pi; y = sin(x); plot(x,y)下面 ...
- poj1177Picture(线段树-周长并)
链接 神奇的扫描线啊 估计之前刷面积并的时候太急了 没来得及理解 .. 有一大段代码是与面积并一模一样的 都是离散化 更新 面积并是扫描的x 这次也一样 因为周长只算外围的 所以扫描到一条x边时 要 ...
- MVC——数据库增删改查(Razor)
一.显示信息 .Models(模板) private MyDBDataContext _context = new MyDBDataContext(); //定义一个变量取出所有数据 public L ...
- apache和tomcat
Apache 和 Tomcat 都是web网络服务器,两者既有联系又有区别,在进行HTML.PHP.JSP.Perl等开发过程中,需要准确掌握其各自特点,选择最佳的服务器配置. Apache是web服 ...
- 浅谈MS-SQL锁机制
锁的概述 一. 为什么要引入锁 多个用户同时对数据库的并发操作时会带来以下数据不一致的问题: 丢失更新A,B两个用户读同一数据并进行修改,其中一个用户的修改结果破坏了另一个修改的结果,比如订票系统 脏 ...
- Shell 脚本编程
Shell 两个数据比较取差异 flag=0 local array1=("a" "b") local array2=("a" " ...
- 编译 skia
0.准备工作 在 https://android.googlesource.com/ 用 git 代码,当然也可以从skia的官方https://code.google.com/p/skia/ 中获取 ...
- oracle rac IP详解
rac环境下vip/public/private IP的区别 每个节点要2块网卡, 3个IP,虚拟IP或者叫做业务IP,单个网卡当掉可以“漂”到其他网卡是继续提供服务 在Oracle RAC环境下,每 ...
- bzoj 1879 [Sdoi2009]Bill的挑战(状压DP)
Description Input 本题包含多组数据. 第一行:一个整数T,表示数据的个数. 对于每组数据: 第一行:两个整数,N和K(含义如题目表述). 接下来N行:每行一个字符串. Output ...
- bzoj 2434 [Noi2011]阿狸的打字机(fail树+离线处理+BIT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2434 [题意] 按照一定规则生成n个字符串,回答若干个询问:(x,y),问第x个字符串 ...