javaSE高级开发之集合类——栈与队列

一、栈

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();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/ZhuiZhuDream5/article/details/86029057