Java8 Stream之字符串是否包含数组中的任何字符串

    /**
     * 一个字符串是否包含数组中的任何字符串
     * 使用Java 8或更高版本,可以依赖Stream API来执行此类操作
     * @param inputString 字符串:这个班级有张三和李四同学
     * @param items 数组 ["张三", "李斯", "王五"]
     * @return boolean
     */
    public static boolean containsItemFromArray(String inputString, String[] items) {
        // Convert the array of String items as a Stream
        // For each element of the Stream call inputString.contains(element)
        // If you have any match returns true, false otherwise
        return Arrays.stream(items).anyMatch(inputString::contains);
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Java 中,可以使用以下几种方法来判断一个字符串是否数组中: 1. 使用 for 循环遍历数组,逐个比较字符串是否相等。 ```java String[] arr = {"apple", "banana", "orange"}; String target = "apple"; boolean found = false; for (int i = 0; i < arr.length; i++) { if (arr[i].equals(target)) { found = true; break; } } if (found) { System.out.println("Found the target string in the array."); } else { System.out.println("Could not find the target string in the array."); } ``` 2. 使用 Arrays.asList() 将数组转换为列表,然后使用列表的 contains() 方法来判断字符串是否数组中。 ```java import java.util.Arrays; import java.util.List; String[] arr = {"apple", "banana", "orange"}; String target = "apple"; List<String> list = Arrays.asList(arr); if (list.contains(target)) { System.out.println("Found the target string in the array."); } else { System.out.println("Could not find the target string in the array."); } ``` 3. 使用 Java 8 的 Stream API,使用 anyMatch() 方法来判断是否存在符合条件的元素。 ```java import java.util.Arrays; String[] arr = {"apple", "banana", "orange"}; String target = "apple"; boolean found = Arrays.stream(arr).anyMatch(s -> s.equals(target)); if (found) { System.out.println("Found the target string in the array."); } else { System.out.println("Could not find the target string in the array."); } ``` 请注意,在 Java 中,字符串的比较应使用 equals() 方法,而不是 == 运算符。 ### 回答2: 要判断某个字符串是否数组中,可以使用Java中的循环结构和条件判断语句来实现。 首先,需要使用一个循环遍历数组中的每个元素。可以使用for循环或者foreach循环来逐个访问数组元素。 其次,在循环中使用条件判断语句来判断当前遍历到的元素是否与目标字符串相同。可以使用equals()方法来比较两个字符串的内容是否相同。 最后,如果找到了与目标字符串相同的元素,则可以根据需求执行相应的操作,例如返回true表示找到了,或者执行其他逻辑。 以下是一个示例代码: ```java public class Main { public static void main(String[] args) { String[] array = {"apple", "banana", "orange", "grape"}; String target = "banana"; boolean isFound = false; for (String str : array) { if (str.equals(target)) { isFound = true; break; } } if (isFound) { System.out.println("目标字符串存在于数组中。"); } else { System.out.println("目标字符串不存在于数组中。"); } } } ``` 在上述示例中,我们创建了一个包含若干字符串数组array,然后定义了目标字符串target为"banana"。接着,使用foreach循环遍历数组中的每个元素,通过equals()方法判断当前元素与目标字符串是否相同。如果找到了相同的元素,则将isFound标志置为true,并使用break语句跳出循环。最后,根据isFound标志的值输出对应的结果。 需要注意的是,数组中的元素与目标字符串的比较必须使用equals()方法,而不能使用"=="运算符,因为"=="比较的是引用地址是否相同,而不是比较字符串的内容。 ### 回答3: 在Java中,我们可以使用循环结构和条件判断语句来判断某个字符串是否存在于数组中。具体步骤如下: 1. 首先,定义一个存储字符串数组,并初始化数组元素。 2. 接下来,使用for循环遍历数组,依次取出每个元素。 3. 在循环内部,使用if条件语句判断当前元素是否与目标字符串相等。 4. 如果相等,则表示目标字符串数组中存在,可以执行相应的操作(如打印提示信息等)。 5. 如果不相等,则继续遍历数组中的下一个元素。 6. 循环结束后,如果没有找到与目标字符串相等的元素,则说明目标字符串不存在于数组中。 以下是一个示例代码: ```java public class Main { public static void main(String[] args) { String[] array = {"apple", "banana", "orange", "grape"}; String target = "orange"; boolean isExists = false; for (int i = 0; i < array.length; i++) { if (array[i].equals(target)) { // 判断当前元素是否与目标字符串相等 isExists = true; break; } } if (isExists) { System.out.println(target + "存在于数组中"); } else { System.out.println(target + "不存在于数组中"); } } } ``` 以上代码通过for循环遍历数组中的元素,并使用equals方法判断当前元素是否与目标字符串相等。如果找到相等的元素,则设置isExists为true,并跳出循环。最后,根据isExists的值输出相应的提示信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值