00 32Java高级之集合工具类

1 Stack栈操作

栈是一种先进后出的数据结构,其基本操作形式如下:

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

public class Stack<E>
extends Vector<E>


可以发现Stack是Vector子类,但是它使用的并不是Vector类之中所提供的方法,而是采用如下的两个方法:
(1)入栈:public E push​(E item)
(2)出栈:public E pop()
范例:实现栈的操作

package org.lks.demo;

import java.util.Stack;

public class JavaReflectDemo {
	public static void main(String[] args) {	
		Stack<String> stack = new Stack<String>();
		stack.push("hello");
		stack.push("world");
		stack.push("wuwwu");
		System.out.println(stack.pop());
		System.out.println(stack.pop());
		System.out.println(stack.pop());
		System.out.println(stack.pop());
	}
}

/*
wuwwu
world
hello
Exception in thread "main" java.util.EmptyStackException
	at java.base/java.util.Stack.peek(Stack.java:102)
	at java.base/java.util.Stack.pop(Stack.java:84)
	at javasenior/org.lks.demo.JavaReflectDemo.main(JavaReflectDemo.java:14)
*/

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

2 Queue队列

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

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

队列的使用主要依靠Queue接口之中提供的方法来处理,提供有如下方法:
(1)向队列之中追加数据:boolean offer​(E e),可以直接使用add()方法;
(2)通过队列获取数据:E poll(),弹出后删除数据。
范例:实现队列操作

package org.lks.demo;

import java.util.LinkedList;
import java.util.Queue;

public class JavaReflectDemo {
	public static void main(String[] args) {	
		Queue<String> queue = new LinkedList<String>();
		queue.add("hello");  //use add() to add element, return true or false
		queue.offer("world");  //use offer() to add element. return true or false
		queue.offer("nihao");
		
		System.out.println(queue.poll());  //use poll() to get the head of the queue, if this queue is empty return null
		if(queue.peek() != null) {
			System.out.println(queue.remove());
		}
	}
}


除了LinkedList之类之外,还有一个优先级队列的概念,可以使用实现优先级队列PriorityQueue<E>(比较功能)。

范例:使用优先级队列

package org.lks.demo;

import java.util.PriorityQueue;
import java.util.Queue;

public class JavaReflectDemo {
	public static void main(String[] args) {	
		Queue<String> queue = new PriorityQueue<String>();
		queue.offer("Z");
		queue.offer("A");
		queue.offer("G");
		queue.offer("H");
		System.out.println(queue.toString());
	}
}
/*
[A, H, G, Z]
*/

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

3 Properties属性操作

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

public class Properties extends Hashtable<Object,​Object>

可以发现在继承Hashtable的时候为Hashtable中定义的泛型为Object,Properties是不需要操作泛型的,因为他可以操作的类型只能是String类型,在Properties之中如果想要实现属性操作可以采用如下的方法:
(1)设置属性:public Object setProperty​(String key, String value)
(2)取得属性,key不存在返回null:public String getProperty​(String key)
(3)取得属性,不存在返回默认值:public String getProperty​(String key, String defaultValue)
(4)输出属性内容:public void store​(OutputStream out, String comments) throws IOException
(5)通过输入流读取属性内容:public void load​(InputStream inStream) throws IOException
范例:观察属性的设置和取得

package org.lks.demo;

import java.util.Properties;

public class JavaReflectDemo {
	public static void main(String[] args) {	
		Properties prop = new Properties();
		prop.setProperty("lks", "hhy big fool");
		prop.setProperty("hhy", "you fuck!");
		System.out.println(prop.getProperty("lks"));
		System.out.println(prop.getProperty("zsl", "I'm fool"));
		System.out.println(prop.getProperty("zsl"));
	}
}

/*
hhy big fool
I'm fool
null
*/

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

package org.lks.demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class JavaReflectDemo {
	public static void main(String[] args) {	
		Properties prop = new Properties();
		prop.setProperty("lks", "hhy big fool, 哈哈");
		prop.setProperty("hhy", "you fuck!");
		try {
			prop.store(new FileOutputStream(new File("D:" + File.separator + "info.properties")), "中文注释-English");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

通过程序的执行可以发现,的确可以实现资源文件的输入处理,但是如果输出的是中文则自动帮助用户进行处理。
范例:读取资源文件

package org.lks.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class JavaReflectDemo {
	public static void main(String[] args) {	
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(new File("D:" + File.separator + "info.properties")));
			System.out.println(prop.getProperty("hhy"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


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

4 Collections工具类

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

范例:使用Collection操作List集合

package org.lks.demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class JavaReflectDemo {
	public static void main(String[] args) {	
		List<String> list = new ArrayList<String>();
		Collections.addAll(list, "hhy", "big", "fool");
		System.out.println(list);
	}
}


范例:数据反转

package org.lks.demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class JavaReflectDemo {
	public static void main(String[] args) {	
		List<String> list = new ArrayList<String>();
		Collections.addAll(list, "hhy", "big", "fool");
		Collections.reverse(list);
		System.out.println(list);
	}
}


范例:使用二分查找

package org.lks.demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class JavaReflectDemo {
	public static void main(String[] args) {	
		List<String> list = new ArrayList<String>();
		Collections.addAll(list, "hhy", "big", "fool");
		Collections.sort(list);
		System.out.println(list);
		System.out.println(Collections.binarySearch(list, "hhy"));
	}
}

/*
[big, fool, hhy]
2
*/

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

面试题:请解释Collection与Collections的区别?
(1)Collection是集合接口,允许保存单值对象;
(2)Collections是集合操作工具类。

发布了122 篇原创文章 · 获赞 11 · 访问量 4216

猜你喜欢

转载自blog.csdn.net/weixin_43762330/article/details/104771089
00
今日推荐