java怎么判断字符串在不在list中

1088
2023/8/8 0:22:29
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Java中,可以使用List的contains()方法来判断一个字符串是否在列表中。示例代码如下:

List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("orange");
String str = "apple";
if(list.contains(str)) {
System.out.println(str + " is in the list.");
} else {
System.out.println(str + " is not in the list.");
}

输出结果:

apple is in the list.

如果要判断的字符串不在列表中,则输出结果为:

apple is not in the list.

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: java怎么打印map中的数据