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);
相关推荐
CYRUS_STUDIO4 小时前
用 Frida 控制 Android 线程:kill 命令、挂起与恢复全解析
android·linux·逆向
熊猫李6 小时前
rootfs-根文件系统详解
linux
dessler8 小时前
Hadoop HDFS-高可用集群部署
linux·运维·hdfs
泽泽爱旅行8 小时前
awk 语法解析-前端学习
linux·前端
轻松Ai享生活1 天前
5 节课深入学习Linux Cgroups
linux
christine-rr1 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
三坛海会大神5551 天前
LVS与Keepalived详解(二)LVS负载均衡实现实操
linux·负载均衡·lvs
東雪蓮☆1 天前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
乌萨奇也要立志学C++1 天前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
凯子坚持 c1 天前
精通 Redis list:使用 redis-plus-plus 的现代 C++ 实践深度解析
c++·redis·list