队列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的更多相关文章

  1. STL中的单向队列queue

    转载自:http://blog.csdn.net/morewindows/article/details/6950917 stl中的queue指单向队列,使用时,包含头文件<queue>. ...

  2. java-Enumeration,单向队列Queue及双向队列Deque等容器简单使用

    1.Enumeration容器使用: package com.etc; import java.util.Enumeration; import java.util.Vector; /* Enumer ...

  3. Python 双向队列Deque、单向队列Queue 模块使用详解

    Python 双向队列Deque 模块使用详解 创建双向队列Deque序列 双向队列Deque提供了类似list的操作方法: #!/usr/bin/python3 import collections ...

  4. Python进阶【第二篇】多线程、消息队列queue

    1.Python多线程.多进程 目的提高并发 1.一个应用程序,可以有多进程和多线程 2.默认:单进程,单线程 3.单进程,多线程 IO操作,不占用CPU python的多线程:IO操作,多线程提供并 ...

  5. STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)(转)

    向量(vector) <vector> 连续存储的元素<vector> Vector<int>c; c.back()    传回最后一个数据,不检查这个数据是否存在 ...

  6. 队列Queue和栈

    1.队列Queue是常用的数据结构,可以将队列看成特殊的线性表,队列限制了对线性表的访问方式,只能从线性表的一段添加(offer)元素, 从另一段取出(poll)元素,队列遵循先进先出的原则. 2.J ...

  7. Java中的queue和deque对比详解

    队列(queue)简述 队列(queue)是一种常用的数据结构,可以将队列看做是一种特殊的线性表,该结构遵循的先进先出原则.Java中,LinkedList实现了Queue接口,因为LinkedLis ...

  8. 【java多线程】队列系统之说说队列Queue

    转载:http://benjaminwhx.com/2018/05/05/%E8%AF%B4%E8%AF%B4%E9%98%9F%E5%88%97Queue/ 1.简介 Queue(队列):一种特殊的 ...

  9. Java 中的队列 Queue

    一.队列的定义 我们都知道队列(Queue)是一种先进先出(FIFO)的数据结构,Java中定义了java.util.Queue接口用来表示队列.Java中的Queue与List.Set属于同一个级别 ...

随机推荐

  1. [jobdu]树的子结构

    判断一棵树B是否是A的子树,对A做DFS,然后不断判断是否和B相同. 其实也可以不对A做DFS,直接遍历A中的每个节点和B做树的比较就行了. #include <iostream> #in ...

  2. Linux进程创建和结束

    在Linux中,进程的创建由系统调用fork和vfork完成.它们生成一个子进程并且子进程是父进程的一个复制品. Fork系统调用对应的kernel函数是sys_fork,此函数简单的调用kernel ...

  3. Android list刷新后仍然定位到原来的位置,解决。

    问题: 有一个list,点击item时会做一些事情,然后重新加载数据,此时希望点击重新刷新后item还在原来的位置,而不是跳转到开头. 实现如下: 1.listview添加监听setOnScrollL ...

  4. Android 内核初识(7)RefBase、LightRefBase、sp和wp

    简介 RefBase是Android中所有对象的始祖,类似MFC中的CObject及Java中的Object对象.在Android中,RefBase结合sp和wp,实现了一套通过引用计数的方法来控制对 ...

  5. IIS由于无法创建应用程序域,因此未能执行请求。错误: 0x80070005 拒绝访问

    网站静态页面(.html)是可以访问的,但是动态页面(.aspx)就出错了. 服务器上还有其他网站,但是都可以正常浏览,这就说明不是IIS本身有问题了,问题应该出在网站本身. 百度后,都说是权限问题, ...

  6. Nginx+Keepalived 做负载均衡器

    1.安装 keepalived   1 2 3 4 5 6 7 8 9 tar zxvf keepalived-XXXX.tar.gz ./configure --prefix=/usr/local/ ...

  7. NOI2010海拔

    2007: [Noi2010]海拔 Time Limit: 20 Sec  Memory Limit: 552 MBSubmit: 1302  Solved: 612[Submit][Status] ...

  8. Android 系统日期时间的获取

    import java.text.SimpleDateFormat; SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月 ...

  9. windows下rundll32介绍

    最近看书介绍rundll32可以加载dll文件并执行其中导出函数,在MSDN中我们可以看到绍http://support.microsoft.com/kb/164787/zh-cn rundll32调 ...

  10. JSP---JSP中4个容器-pageContext使用

    这里重点只讲pageContext容器的用法哦. 因为另外的3个容器(request,session,application)在前面的servlet中已经演示过很多遍了 容器 作用域 pageCont ...