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);
相关推荐
梦痕长情10 分钟前
等保三级三权账户
linux·运维·服务器
CHANG_THE_WORLD1 小时前
1.简单的服务端和客户端(仅作为内容补充)
linux·服务器·网络
jiecy1 小时前
为Linux系统双网卡配置静态路由
linux·运维
壹号用户1 小时前
c++入门之list了解及使用
数据结构·list
weixin_307779132 小时前
Linux下Jenkins数据故障的系统化排查Shell脚本
linux·运维·服务器·jenkins
春风霓裳2 小时前
window设置永不更新
windows
荒--2 小时前
Burp Suite(二)
linux·运维·服务器
小白还菜3 小时前
linux(Debian)使用mdadm组磁盘RAID1阵列
linux·运维
不怕犯错,就怕不做4 小时前
GIT的简单打patch应用format-patch and git am
linux·git·全文检索