当前位置: 首页 > news >正文

Java 集合 示例

Collection

package com.example.demo1;import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collection;public class CollectionDemo {public static void main(String[] args) {// ======================================// 1. 初始化Collection集合(使用ArrayList实现)// ======================================Collection<String> fruits = new ArrayList<>();// ======================================// 2. 演示Collection常用方法// ======================================// boolean add(E e):添加元素fruits.add("苹果");fruits.add("香蕉");fruits.add("橙子");System.out.println("添加后集合:" + fruits);  // 输出:[苹果, 香蕉, 橙子]// boolean contains(Object o):判断是否存在System.out.println("是否包含香蕉:" + fruits.contains("香蕉"));  // 输出:true// int size():集合长度System.out.println("当前集合大小:" + fruits.size());  // 输出:3// boolean remove(Object o):移除指定元素System.out.println("移除苹果结果:" + fruits.remove("苹果"));  // 输出:trueSystem.out.println("移除后集合:" + fruits);  // 输出:[香蕉, 橙子]// boolean isEmpty():判断是否为空System.out.println("集合是否为空:" + fruits.isEmpty());  // 输出:false// boolean removeIf(Predicate):根据条件移除(需要Java 8+)// 示例:移除所有包含"橙"字的元素boolean removed = fruits.removeIf(fruit -> fruit.contains("橙"));System.out.println("条件移除结果:" + removed);  // 输出:trueSystem.out.println("条件移除后集合:" + fruits);  // 输出:[香蕉]// void clear():清空集合fruits.clear();System.out.println("清空后集合:" + fruits);  // 输出:[]// ======================================// 3. 重新填充数据用于遍历演示// ======================================fruits.add("葡萄");fruits.add("草莓");fruits.add("芒果");// ======================================// 4. 迭代器遍历(Iterator)// ======================================Iterator<String> iterator = fruits.iterator();System.out.println("\n迭代器遍历:");while (iterator.hasNext()) {  // 判断是否有下一个元素String fruit = iterator.next();  // 获取当前元素并移动指针System.out.print(fruit + " ");   // 输出:葡萄 草莓 芒果 // 示例:用迭代器删除元素(安全操作)if ("草莓".equals(fruit)) {iterator.remove();  // 删除当前指向的元素}}System.out.println("\n迭代器删除后集合:" + fruits);  // 输出:[葡萄, 芒果]// ======================================// 5. 增强for循环(for-each)// ======================================System.out.println("\n增强for循环遍历:");for (String fruit : fruits) {  // 自动遍历集合中每个元素System.out.print(fruit + " ");  // 输出:葡萄 芒果 }// ======================================// 6. Lambda表达式的使用(结合Collection的forEach)// ======================================System.out.println("\nLambda表达式遍历:");fruits.forEach(fruit -> {  // Lambda表达式替代匿名内部类System.out.print(fruit + " ");  // 输出:葡萄 芒果 });// 更简洁的Lambda写法(方法引用)// fruits.forEach(System.out::print);}
}

能使用 Lambda 表达式,需满足以下条件

1. 目标类型为函数式接口

Lambda 表达式只能用于函数式接口的上下文。函数式接口指的是仅包含一个抽象方法的接口。

@FunctionalInterface
public interface Predicate<T> {boolean test(T t);
}

Predicate<T> 被 @FunctionalInterface 注解标记,表明它是函数式接口,且只有一个抽象方法 test(T t)。所以,removeIf 方法的参数位置可使用 Lambda 表达式。

2. 参数类型兼容

Lambda 表达式的参数类型要和函数式接口中抽象方法的参数类型兼容。Predicate<String> 里 test 方法的参数类型为 String,因此 Lambda 表达式的参数类型也得是 String,像 fruit -> fruit.contains("橙") 中,fruit 就被推断为 String 类型。

3. 返回值类型匹配

Lambda 表达式的返回值类型要和函数式接口中抽象方法的返回值类型一致。Predicate<String> 里 test 方法的返回值类型是 boolean,所以 fruit -> fruit.contains("橙") 的返回值也得是 boolean 类型,contains 方法恰好返回 boolean 类型的值,满足要求。

不用Lambda 表达式的写法

// ... 已有代码 ...
// boolean removeIf(Predicate):根据条件移除(需要Java 8+)
// 示例:移除所有包含"橙"字的元素
boolean removed = fruits.removeIf(new java.util.function.Predicate<String>() {@Overridepublic boolean test(String fruit) {return fruit.contains("橙");}
});
System.out.println("条件移除结果:" + removed);  // 输出:true
System.out.println("条件移除后集合:" + fruits);  // 输出:[香蕉]
// ... 已有代码 ...

http://www.lryc.cn/news/589555.html

相关文章:

  • 【Qt】插件机制详解:从原理到实战
  • redisson tryLock
  • HAProxy双机热备,轻松实现负载均衡
  • [Python] -实用技巧6-Python中with语句和上下文管理器解析
  • Hessian矩阵在多元泰勒展开中如何用于构造优化详解
  • 记一次POST请求中URL中文参数乱码问题的解决方案
  • LeetCode 1888. 使二进制字符串字符交替的最少反转次数
  • 整除分块练习题
  • 使用Spring Cloud LoadBalancer报错java.lang.IllegalStateException
  • AI助手指南:从零开始打造Python学习环境(VSCode + Lingma/Copilot + Anaconda + 效率工具包)
  • 学习秒杀系统-实现秒杀功能(商品列表,商品详情,基本秒杀功能实现,订单详情)
  • Sharding-JDBC 分布式事务实战指南:XA/Seata 方案解析(三)
  • 2HDMI/1DP转EDP/LVDS,支持4K,144HZ和240HZ.
  • LSA链路状态通告
  • 学习软件测试的第十六天
  • 项目进度跨地域团队协作困难,如何统一进度安排
  • 原来时间序列挖掘这么简单
  • 力扣73:矩阵置零
  • NW917NW921美光固态闪存NW946NW952
  • 游戏行业中的恶梦:不断升级的DDoS攻击
  • 【HarmonyOS】ArkUI-X 跨平台框架入门详解(一)
  • 3.正则化——新闻分类
  • 【stm32】新建工程
  • STM32裸机开发(中断,轮询,状态机)与freeRTOS
  • MyBatis与Spring整合优化实战指南:从配置到性能调优
  • Conda 核心命令快速查阅表
  • 系统编程是什么
  • 22-C#的委托简单使用-2
  • ai问答推荐企业排名优化?:五大企业核心竞争力全景对比
  • 从0开始学习R语言--Day47--Nomogram