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);
相关推荐
艾莉丝努力练剑2 分钟前
【Python基础:语法第四课】列表和元组——Python 里的“爱情”:列表善变,元组长情
大数据·人工智能·windows·python·安全·pycharm·编辑器
羑悻的小杀马特2 分钟前
【仿RabbitMQ实现消息队列项目】交换机智能路由、队列流量隔离、绑定信息精准定向、消息可靠投递——四模块协同打造低耦合消息系统!
linux·rabbitmq·项目·消息·交换机·队列·绑定信息
Ronin3054 分钟前
【Linux网络】NAT、代理服务、内网穿透
linux·网络·智能路由器·内网穿透·nat·代理服务器·内网打洞
DeeplyMind13 分钟前
AMD rocr-libhsakmt分析系列3-1: Apertures
linux·amdgpu·rocm·kfd·rocr
无奈笑天下2 小时前
银河麒麟桌面OS使用分区编辑器将/backup分区删除并扩容至根分区参考教程
linux·数据库·经验分享·编辑器
CheungChunChiu8 小时前
Linux 内核设备模型与驱动框架解析 ——以 rk-pcie 为例
linux·运维·ubuntu
列逍9 小时前
Linux进程(三)
linux·运维·服务器·环境变量·命令行参数
水天需01010 小时前
VS Code Ctrl+Shift+V 预览 Markdown 无效的解决方案
linux
John_ToDebug10 小时前
深度解析 Chromium 浏览器 UI 刷新机制与 ThemeService 的核心作用
chrome·windows·ui
赖small强12 小时前
【Linux C/C++开发】Linux 平台 Stack Protector 机制深度解析
linux·c语言·c++·stack protector·stack-protector·金丝雀机制