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);
相关推荐
ycchenG713 分钟前
缓存元数据损坏操作步骤(lvmcache修复)
linux·缓存
一乐小哥30 分钟前
Docker 拉取镜像超时?别再瞎抄配置了!亲测 3 个有效镜像源 + 避坑指南
linux·docker
the sun3436 分钟前
从内核数据结构的角度理解socket
linux·运维·服务器
阿群今天学习了吗2 小时前
label studio 服务器端打开+xshell端口转发设置
linux·运维·服务器·笔记·python
Aczone282 小时前
Linux Framebuffer(帧缓冲)与基本 UI 绘制技术
linux·运维·ui
wdfk_prog12 小时前
[Linux]学习笔记系列 -- [arm][lib]
linux·运维·arm开发·笔记·学习
boy快快长大12 小时前
【Linux】常用命令(三)
linux·运维·服务器
ayaya_mana13 小时前
Notepad--:国产跨平台文本编辑器,Notepad++ 的理想替代方案
linux·windows·macos·编辑器·notepad·notepad--
岩屿15 小时前
.NET 应用程序 Linux下守护进程脚本编写
linux·运维·服务器·c#·.net
im_AMBER15 小时前
杂记 01
linux·运维·服务器