集合工具类

作者: 江湖非良人 | 来源:发表于2019-08-07 01:16 被阅读6次

Stack栈操作

  栈是一种先进后出的数据结构。例如:在文本编辑器上都有撤销功能,那么每次使用时可以发现,最后一次的编辑操作永远是最先撤销,那么这个功能就是利用栈来实现的,栈的基本操作形式如下:

Stack栈

  在Java程序中使用Stack来描述栈的操作,这个类的定义如下:

public class Stack<E> extends Vector<E>
Stack栈

可以发现Stack是Vector子类,但是它使用的并不是Vector类中所提供的方法,而是采用如下的两个方法:

  • 入栈:public E push​(E item)
  • 出栈:public E pop()

范例:实现栈的操作

import java.util.Stack;
public class JavaAPIDemo {
    public static void main(String[] args) throws Exception {
        Stack<String> all = new Stack<String> ();
        all.push("a");
        all.push("b");
        all.push("c");
        System.out.println(all.pop());//c
        System.out.println(all.pop());//b
        System.out.println(all.pop());//a
        System.out.println(all.pop());//Exception in thread "main" java.util.EmptyStackException
    }
}

通过上面的操作可以发现,所有保存的数据将按照倒序的形式弹出,如果栈已经空了,则会抛出空栈异常。

Queue队列

  Queue描述的是一个队列,而队列的主要特点是实现先进先出的操作形式。其基本的操作形式如下:

Queue

  如果将队列应用在多线程的“生产者与消费者”的模型处理上,那么对于生产者过快的情况下,就没有必要等待消费者或者数据了,可以将所有的内容保存在队列之中,队列的实现可以使用LinkedList子类来完成,观察这个类的定义:

LinkedList

  队列的使用主要依靠Queue接口之中提供的方法来处理,提供有如下方法:

  • 向队列中追加数据:boolean add​(E e)
  • 向队列中追加数据:boolean offer​(E e)
  • 通过队列获取数据(弹出不删除):E peek()
  • 通过队列获取数据(弹出并删除):E poll()

范例:实现队列操作

import java.util.LinkedList;
import java.util.Queue;
public class JavaAPIDemo {
    public static void main(String[] args) throws Exception {
        Queue<String> queue=new LinkedList();
        queue.add("X");//追加队列数据,通过队尾追加
        queue.offer("A");//追加队列数据,通过队尾追加
        queue.offer("Z");//追加队列数据,通过队尾追加
        System.out.println(queue.poll());//X
        System.out.println(queue.poll());//A
        System.out.println(queue.poll());//Z
        System.out.println(queue.poll());//null
    }
}

  除了LinkedList子类以外,还有一个优先级队列的概念,可以使用PriorityQueue实现优先级队列。这个类的定义如下:

public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable

public abstract class AbstractQueue<E> extends AbstractCollection<E> implements Queue<E>

public abstract class AbstractCollection<E> extends Object implements Collection<E>
PriorityQueue

范例:使用优先级队列

import java.util.PriorityQueue;
import java.util.Queue;
public class JavaAPIDemo {
    public static void main(String[] args) throws Exception {
        Queue<String> queue=new PriorityQueue();
        queue.add("X");//追加队列数据,通过队尾追加
        queue.offer("A");//追加队列数据,通过队尾追加
        queue.offer("Z");//追加队列数据,通过队尾追加
        System.out.println(queue.poll());//X
        System.out.println(queue.poll());//A
        System.out.println(queue.poll());//Z
    }
}

对于队列的选用原则也是需要根据实际的项目环境来决定的。

Properties属性操作

  在之前讲解国际化程序时讲解过资源文件(*.properties),这类文件的存储结构是按照“key=value”的形式存储的,而这种结构的保存形式与Map集合很相似,但是唯一的区别在于其保存的内容只能够是字符串,所以为了可以方便地描述属性的定义,java.util包中提供了Properties类型,此类是HashTable的子类。

public class Properties extends Hashtable<Object,Object>

可以发现在继承HashTable时为HashTable中定义的泛型为Object,Properties是不需要操作泛型的,因为它能够操作的类型只能是String类型,在Properties中如果想要实现属性的操作可以采用如下的方法实现:

  • 设置属性:
      public Object setProperty​(String key, String value)
  • 获取属性,key不存在返回null:
      public String getProperty​(String key)
  • 获取属性,key不存在返回默认值:
      public String getProperty​(String key, String defaultValue)
  • 输出属性内容:
      public void store​(OutputStream out, String comments) throws IOException
  • 通过输入流读取属性内容:
      public void load​(InputStream inStream) throws IOException

范例:观察属性的设置和取得

import java.util.Properties;
public class JavaAPIDemo {
    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.setProperty("mldn", "www.baidu.com");
        props.setProperty("java", "www.baidujava.com");
        System.out.println(props.getProperty("mldn"));//www.baidu.com
        System.out.println(props.getProperty("sina"));//null
        System.out.println(props.getProperty("sina", "notFound"));//notFound
    }
}

通过代码可以发现Properties中可以像Map集合那样进行内容的设置与获取,但是唯一的差别是它只能够操作String类型,另外需要注意的是,之所以会提供Properties类还有一个最重要的功能是它可以通过输出流输出属性,也可以使用输入流读取属性内容。

范例:将属性内容保存在文件中

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
public class JavaAPIDemo {
    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.setProperty("mldn", "www.baidu.com");
        props.setProperty("java", "www.baidujava.com");
        props.setProperty("city","杭州");
        props.store(new FileOutputStream(new File("/Users/david/Documents/mydir/mldn.properties")),"中文的注释-englishRemark");
    }
}

通过程序的执行发现,的确可以实现资源文件的输出处理,但是如果输出的是中文,则会自动进行Unicode转码处理。

范例:读取资源文件

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
public class JavaAPIDemo {
    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.load(new FileInputStream(new File("/Users/david/Documents/mydir/mldn.properties")));
        System.out.println(props.getProperty("mldn"));//www.baidu.com
        System.out.println(props.getProperty("java"));//www.baidujava.com
        System.out.println(props.getProperty("city"));//杭州
    }
}

  使用Properties类型的最大特点就是可以进行资源内容的输入与输出的处理操作,但是在实际开发中Properties往往用于读取配置资源的信息,这一点主要是在标准设计中做程序初始化准备时使用。

Collections工具类

  Collections是Java提供的一组集合数据的操作工具类,也就是说利用它可以实现各个集合的操作。
范例:使用Collections操作List集合

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class JavaAPIDemo {
    public static void main(String[] args) throws Exception {
        List<String> all=new ArrayList();
        Collections.addAll(all, "Hello","world","MLDN");
        System.out.println(all);//[Hello, world, MLDN]
        Collections.reverse(all);
        System.out.println(all);//[MLDN, world, Hello]
    }
}

范例:使用二分查找

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class JavaAPIDemo {
    public static void main(String[] args) throws Exception {
        List<String> all=new ArrayList();
        Collections.addAll(all, "Hello","world","MLDN");
        Collections.sort(all);//先进行排序处理
        System.out.println(all);//[Hello, MLDN, world]
        System.out.println(Collections.binarySearch(all, "MLDN"));//1
    }
}

  大部分情况下对于集合的使用可能没有这么多复杂要求,更多情况下就是利用集合保存数据,要么进行输出,要么进行查询。

相关文章

网友评论

    本文标题:集合工具类

    本文链接:https://www.haomeiwen.com/subject/lqtgdctx.html