如何在不遍历的情况下,用Java判断字符串是否存在于集合中?

更新于
2026-10-12 09:24:20
1阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计285个文字,预计阅读时间需要2分钟。

如何在不遍历的情况下,用Java判断字符串是否存在于集合中?

方法一:使用contains()函数检查List是否包含特定元素

List listDemo2=new ArrayList();listDemo2.add(12.1);listDemo2.add(12.2);listDemo2.add(12.3);listDemo2.add(12.4);double key=12.25;if(listDemo2.contains(key)) System.out.println(contains true);

如何在不遍历的情况下,用Java判断字符串是否存在于集合中?

方法一 采用contains 函数

List listDemo2 =new ArrayList (); listDemo2.add(12.1); listDemo2.add(12.2); listDemo2.add(12.3); listDemo2.add(12.4); double key = 12.25; if(listDemo2.contains(key)){ System.out.println("contains true "+listDemo2.contains(key)); }else{ System.out.println("contains flase "+listDemo2.contains(key)); } /**contains boolean contains(Object o) Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)). Specified by:contains in interface Collection Parameters:o - element whose presence in this list is to be testedReturns:true if this list contains the specified elementThrows:ClassCastException - if the type of the specified element is incompatible with this list (optional)NullPointerException - if the specified element is null and this list does not permit null elements (optional)*/ 方法二 采用获取下标的方法

List listDemo2 =new ArrayList (); listDemo2.add(12.1); listDemo2.add(12.2); listDemo2.add(12.3); listDemo2.add(12.4); double key = 12.25; if(listDemo2.indexOf(key) >= 0){ System.out.println("indexOf true "+listDemo2.indexOf(key)); }else{ System.out.println("indexOf flase "+listDemo2.indexOf(key)); } /* 如果存在,下标就为0或正值,反之为-1 */

本文共计285个文字,预计阅读时间需要2分钟。

如何在不遍历的情况下,用Java判断字符串是否存在于集合中?

方法一:使用contains()函数检查List是否包含特定元素

List listDemo2=new ArrayList();listDemo2.add(12.1);listDemo2.add(12.2);listDemo2.add(12.3);listDemo2.add(12.4);double key=12.25;if(listDemo2.contains(key)) System.out.println(contains true);

如何在不遍历的情况下,用Java判断字符串是否存在于集合中?

方法一 采用contains 函数

List listDemo2 =new ArrayList (); listDemo2.add(12.1); listDemo2.add(12.2); listDemo2.add(12.3); listDemo2.add(12.4); double key = 12.25; if(listDemo2.contains(key)){ System.out.println("contains true "+listDemo2.contains(key)); }else{ System.out.println("contains flase "+listDemo2.contains(key)); } /**contains boolean contains(Object o) Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)). Specified by:contains in interface Collection Parameters:o - element whose presence in this list is to be testedReturns:true if this list contains the specified elementThrows:ClassCastException - if the type of the specified element is incompatible with this list (optional)NullPointerException - if the specified element is null and this list does not permit null elements (optional)*/ 方法二 采用获取下标的方法

List listDemo2 =new ArrayList (); listDemo2.add(12.1); listDemo2.add(12.2); listDemo2.add(12.3); listDemo2.add(12.4); double key = 12.25; if(listDemo2.indexOf(key) >= 0){ System.out.println("indexOf true "+listDemo2.indexOf(key)); }else{ System.out.println("indexOf flase "+listDemo2.indexOf(key)); } /* 如果存在,下标就为0或正值,反之为-1 */