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);
相关推荐
花嫁代二娃9 分钟前
Linux:环境变量
linux
斜月三5 小时前
windows部署多实例filebeat监控相同路径下文件
windows·filebeat
l1x1n05 小时前
Vim 编辑器常用操作详解(新手快速上手指南)
linux·编辑器·vim
ajassi20007 小时前
开源 python 应用 开发(三)python语法介绍
linux·python·开源·自动化
o不ok!7 小时前
Linux面试问题-软件测试
linux·运维·服务器
DaxiaLeeSuper7 小时前
Prometheus+Grafana+node_exporter监控linux服务器资源的方案
linux·grafana·prometheus
尽兴-8 小时前
如何将多个.sql文件合并成一个:Windows和Linux/Mac详细指南
linux·数据库·windows·sql·macos
kfepiza8 小时前
Netplan 中 bridges、bonds、ethernets、vlans 之间的关系 笔记250711
linux·tcp/ip·shell
小小不董9 小时前
深入理解oracle ADG和RAC
linux·服务器·数据库·oracle·dba
杰夫贾维斯10 小时前
CentOS Linux 8 的系统部署 Qwen2.5-7B -Instruct-AWQ
linux·运维·人工智能·机器学习·centos