JAVA集合类续

本文深入讲解了Java集合类中的栈与队列特性,包括Stack的入栈与出栈操作及Queue的基本使用。同时介绍了Properties类如何操作属性文件,以及Collections工具类提供的集合操作方法。最后探讨了Stream数据流在数据处理中的应用。

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值