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=0;i<10;i++){
            final int num =i;
            que.offer(new Request(){//匿名内部类对象,只能访问final修饰的变量。
                @Override
                public void deposit() {                    System.out.println(""+num+"个人,办理存款业务,存款额度为:"+(Math.random()*10000));
                }
            });
        }
        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>(3);
        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)


相对路径和绝对路径:
1.绝对路径:windows要指定盘符,Linux和Unix要用/
2.相对路径,相对当前工程,
3.根据类路径加载资源文件:
类所在的跟路径:
类.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"));
    }
}
复制代码

 


本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/4833566.html,如需转载请自行联系原作者

相关文章
|
1月前
|
前端开发 Java
java中的Queue队列的用法
java中的Queue队列的用法
19 1
|
1月前
|
存储 安全 算法
解读 Java 并发队列 BlockingQueue
解读 Java 并发队列 BlockingQueue
20 0
|
1月前
|
存储 安全 Java
java集合框架及其特点(List、Set、Queue、Map)
java集合框架及其特点(List、Set、Queue、Map)
|
3月前
|
Java
队列(JAVA)
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出的性质。
21 0
|
3月前
|
安全 Java 容器
Java Review - Queue和Stack 源码解读
Java Review - Queue和Stack 源码解读
38 0
|
1月前
|
安全 Java API
Java并发 - J.U.C并发容器类 list、set、queue
Queue API 阻塞是通过 condition 来实现的,可参考 Java 并发 - Lock 接口 ArrayBlockingQueue 阻塞 LinkedBlockingQueue 阻塞 ArrayQueue 非阻塞 LinkedQueue 非阻塞
|
2月前
|
存储 安全 Java
JAVA常用队列类
JAVA常用队列类
|
2月前
|
算法 安全 前端开发
Java Queue接口及其常用实现类分析
Java Queue接口及其常用实现类分析
|
3月前
|
C++ Java Go
Java每日一练(20230428) 搜索旋转排序数组、栈实现队列、平方根
Java每日一练(20230428) 搜索旋转排序数组、栈实现队列、平方根
45 0
Java每日一练(20230428) 搜索旋转排序数组、栈实现队列、平方根
|
3月前
|
Java
JAVA AQS 抽象队列同步器
在 AQS(AbstractQueuedSynchronizer)中,可以通过一些机制来实现共享锁。AQS是Java并发包中的一个基础框架,它提供了一种用于构建锁和同步器的工具。

热门文章

最新文章