List常用的操作

1、看List里是否存在某个元素 contains

复制代码
//省略建立list
 boolean contains = stringList.contains("上海");
 System.out.println(contains);

如果存在是true,不存在是false

2、看某个元素在List中的索引号 .indexOf

复制代码
List<String>stringList = new ArrayList<>();
        stringList.add("上海");
        stringList.add("厦门");
        int i = stringList.indexOf("上海");
        int j = stringList.indexOf("北京");
        System.out.println(i);
        System.out.println(j);

如果不存在,输出结果 -1 ,否则输出该元素在集合中是索引号。

3、数据库里某个字段 in (your_list)

复制代码
select * from t_class where id in  (your_list);

4、List空集合的size是0 若要判空,别用 size < 0 ,用.isEmpty()

复制代码
        List<String> newList = new ArrayList<>();
        boolean empty = newList.isEmpty();
        int size = newList.size();
        System.out.println(empty);
        System.out.println(size);

输出结果:

true

0

5、将指定集合添加到list结尾 listA.addAll(listB)

复制代码
        List<String>A = new ArrayList<>();
        A.add("上海");
        A.add("厦门");

        List<String>B = new ArrayList<>();
        B.add("北京");
        B.add("深圳");

        //addAll 成功为true
        boolean b = A.addAll(B);
        System.out.println(b);
        //此时A输出结果 [上海, 厦门, 北京, 深圳]
        System.out.println(A);
相关推荐
虾..6 分钟前
Linux HTTP服务器
linux·服务器·http
非凡ghost30 分钟前
Zen Browser:基于 Firefox 的极简开源浏览器,隐私与速度兼得
前端·网络·windows·学习·开源·firefox·软件需求
木梓辛铭1 小时前
windows下部署openclaw详细方法及错误处理办法
windows·学习方法·持续部署
John_ToDebug1 小时前
死锁案例:UI 线程阻塞等待跨进程 COM 注入
c++·windows·笔记
REDcker2 小时前
Linux Core Dump 配置与分析指南
linux·运维·服务器
IMPYLH2 小时前
Linux 的 chcon 命令
linux·运维·服务器
苦逼IT运维2 小时前
SVN 仓库目录迁移,仓库 “降级” 成子目录实战
linux·运维·ci/cd·svn·运维开发
阿拉斯攀登2 小时前
第 13 篇 输入设备驱动(触摸屏 / 按键)开发详解,Linux input 子系统全解析
android·linux·运维·驱动开发·rk3568·瑞芯微·rk安卓驱动
bukeyiwanshui3 小时前
【无标题】
linux·运维·服务器
疯狂吧小飞牛3 小时前
Linux 多网卡同网段配置冲突问题
linux·运维·服务器