Java中的栈、队列、Propertise属性文件操作

1.Stack栈

栈的特性:先进后出

1.1 常用方法

//入栈
public E push(E item) 
//出栈
public synchronized E pop() 
//返回栈顶元素
public synchronized E peek() 
//判断栈是否为空
public boolean empty() 
import java.util.Stack;

public class Test {
    public static void main(String[] args) {
        //1.创建一个栈对象
        Stack<String> stack = new Stack<>();
        //2.入栈操作
        stack.push("辣条");
        stack.push("鸡爪");
        stack.push("翅尖");
        //3.返回栈的大小
        System.out.println(stack.size());
        //4.输出栈顶元素
        System.out.println(stack.peek());
        //打印栈
        System.out.println(stack);
        //5.出栈操作 ---根据先进后出 此次应该翅尖出栈
        System.out.println(stack.pop());
        //根据先进后出 此次应该鸡爪出栈
        System.out.println(stack.pop());
        //根据先进后出 此次应该辣条出栈
        System.out.println(stack.pop());
        //6.输出栈是否为空---此时应该为空
        System.out.println(stack.isEmpty());
        //7.继续出栈 观察结果
        System.out.println(stack.pop());
    }
}

Java中的栈、队列、Propertise属性文件操作
当栈为空时,进行出栈操作会产生空指针异常,所以一般在出栈是进行栈不为空的判断

2.队列

队列的特性:先进先出

2.1 常用方法

//入对列
boolean add(E e);
//出队列
E poll();
//返回栈顶元素
E peek();
import java.util.LinkedList;
import java.util.Queue;
public class Test {
    public static void main(String[] args) {
        //1.创建一个队列
        Queue<String> queue = new LinkedList<>();
        //2.入对列
        queue.add("辣条");
        queue.add("鸡爪");
        queue.add("翅尖");
        //3.输出队列中的元素
        System.out.println(queue.size());
        //4 输出队顶元素
        System.out.println(queue.peek());
        //输出
        System.out.println(queue);
        //4.出队列 根据先进先出的原则来看 此次辣条出队列
        System.out.println(queue.poll());
        //根据先进先出的原则来看 此次鸡爪出队列
        System.out.println(queue.poll());
        //根据先进先出的原则来看 此次翅尖出队列
        System.out.println(queue.poll());
        //5.判断队列是否为空
        System.out.println(queue.isEmpty());
        //6.继续出队列 观察结果
        System.out.println(queue.poll());
    }
}

Java中的栈、队列、Propertise属性文件操作
当队列为空时,出队列不会产生空指针异常,会输出null

3.Propertise属性文件操作

在java中有一种属性文件(资源文件):*.propertise文件,在这种文件里面其内容的 保存形式为"key = value",且所有的属性实际上都是以字符串的形式出现的,Propertise是Hashtable的子类,线程安全。

public class Properties extends Hashtable<Object,Object>

3.1 常用方法

//1.设置属性
public synchronized Object setProperty(String key, String value) ;
//2.根据Key值取得value
public String getProperty(String key) 
//3.根据Key值取得value 如果没有指定的key值则返回defaultValue
public String getProperty(String key, String defaultValue) 
import java.util.Properties;
public class Test{
    public static void main(String[] args) {
        //1.取得一个Propertise属性
        Properties properties = new Properties();
        //2.设置属性
        properties.setProperty("1","Java");
        properties.setProperty("2","数据结构");
        //3.根据key值取得value
        System.out.println(properties.getProperty("1"));
        System.out.println(properties.getProperty("0"));
        //4.根据key值取得value,如果没有指定的key值则返回defaultValue
        System.out.println(properties.getProperty("0","网络"));
    }
}

Java中的栈、队列、Propertise属性文件操作

3.2 IO支持的方法

 //保存属性
 public void store(OutputStream out, String comments) throws IOException
 //读取属性
 public synchronized void load(InputStream inStream) throws IOException 
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Test{
    public static void main(String[] args) {
        //1.取得Propertise对象
        Properties properties = new Properties();
        //2.加载属性文件
        try {
            //2.1 加载文件系统中的绝对路径文件
            properties.load(new FileReader("E:\\java作业\\CODE\\src\\栈和队列\\study.properties"));
//            //2.2.加载classpath中的文件
//            InputStream in = Test.class.getClassLoader()
//                    .getResourceAsStream("栈和队列/study.propertises");
//            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //2.输出属性文件中的内容
        System.out.println(properties);
        //3.根据key值返回value
        System.out.println(properties.getProperty("1"));
        System.out.println(properties.getProperty("2"));
        //根据key值返回value 如果没有对应的key值返回null
        System.out.println(properties.getProperty("3"));
        //根据key值返回value 如果没有对应的key值返回默认value---但是不会写入属性文件
        System.out.println(properties.getProperty("3","math"));
        //4.设置属性
        properties.setProperty("0","网络");
        //保存属性信息
        try {
            properties.store(new FileWriter("E:\\java作业\\CODE\\src\\" +
                    "栈和队列\\study.properties"),"这是从外部写入的命令");

        } catch (IOException e) {
            e.printStackTrace();
        }
        //5.输出属性中的内容
        System.out.println(properties);
    }
}

Java中的栈、队列、Propertise属性文件操作