JAVA集合类续
1 栈与队列
1.1 Stack栈
栈:先进后出
import java.util.Stack;
public class StackTest {
public static void main(String[] args) {
//Stack 由 Stack 定义,并不是 Vector 的子类
Stack<String> stack = new Stack<>();
//push() 入栈
stack.push("hello");
stack.push("world");
//peek() 取得栈顶元素
System.out.println("栈顶元素:" + stack.peek());
//pop() 出栈
System.out.println(stack.pop());
System.out.println(stack.pop());
//栈已空,出栈出现EmptyStackException
System.out.println(stack.pop());
}
}
结果:
栈顶元素:world
Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Stack.java:102)
world
hello
at java.util.Stack.pop(Stack.java:84)
at com.Test.StackTest.main(StackTest.java:18)
1.2 Queue队列
队列:先进先出
import java.util.LinkedList;
import java.util.Queue;
public class QueueTest {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
//add() 添加元素
queue.add("hello");
queue.add("world");
//peek() 取得队列首元素
System.out.println("队列首元素:" + queue.peek());
//poll() 取出数据
System.out.println(queue.poll());
System.out.println(queue.poll());
//如果没有元素则返回null
System.out.println(queue.poll());
}
}
结果:
队列首元素:hello
hello
world
null
2 Properties属性文件操作
Properties是Hashtable的子类,定义为:public class Properties extends Hashtable<Object, Object>,只能操作String。
import java.util.Properties;
public class PropertiesTest {
public static void main(String[] args) {
Properties properties = new Properties();
//设置属性
properties.setProperty("h","hello");
properties.setProperty("w","world");
//取得属性值
System.out.println(properties.get("h"));
System.out.println(properties.get("w"));
}
}
结果:
hello
world
其他操作:
(1)保存属性:public void store(OutputStream out, String comments);
(2)读取属性:public synchronized void load(inputStream inStream);
3 Collections工具类
Collections是一个集合操作的工具类,包含集合反转、排序等操作。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsTest {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
//addAll() 一次性添加多个元素
Collections.addAll(list,"hello","world","!");
System.out.println(list);
//reverse() 集合反转
Collections.reverse(list);
System.out.println(list);
}
}
结果:
[hello, world, !]
[!, world, hello]
4 Stream数据流
MapReduce对数据的操作分两个阶段:Map:处理数据;Reduce:分析数据。该操作通过Stream数据流完成。
4.1 stream()
Collection接口中的stream()方法。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
public class StreamTest {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Collections.addAll(list,"hello","world");
//实例化stream对象
Stream<String> stream = list.stream();
//计算数量
System.out.println(stream.count());
}
}
结果:
2
4.2 Stream操作
数据过滤:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
public class StreamTest {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Collections.addAll(list,"hello","world");
//实例化stream对象
Stream<String> stream = list.stream();
//数据过滤,计算hello出现的次数
System.out.println(stream.filter((e)->e.contains("hello")).count());
}
}
结果:
1
(1)收集:collect();
(2)设置取出最大内容:public Stream limit(long maxSize);
(3)跳过的数据量:public Stream skip(long n);
本文深入讲解了Java集合类中的栈与队列特性,包括Stack的入栈与出栈操作及Queue的基本使用。同时介绍了Properties类如何操作属性文件,以及Collections工具类提供的集合操作方法。最后探讨了Stream数据流在数据处理中的应用。
2222

被折叠的 条评论
为什么被折叠?



