简体   繁体   English

包含在Java中无法正常工作的方法

[英]Contains method not working properly in Java

I would like to check if a string contains a specified word. 我想检查一个字符串是否包含指定的单词。 If the string contains that specified word i don't want to print that. 如果字符串包含指定的单词,我不想打印该单词。 Now there may be redundant words but i will only print unique words excluding my pre-defined word list for example, "traffic", "collapse". 现在可能会有多余的单词,但是我只会打印除我的预定义单词列表以外的唯一单词,例如“流量”,“折叠”。 For that I am using Set to store the unique strings and checking that using if-statement and contains. 为此,我使用Set来存储唯一的字符串,并使用if-statement和contains进行检查。 But it is not working properly. 但是它不能正常工作。 Although my pre-defined restrictive word is "traffic" but still my program prints traffic along with all the words. 尽管我的预定义限制性词是“流量”,但我的程序仍会与所有单词一起显示流量。 It seems the filter/contains method is not working properly. 似乎filter / contains方法无法正常工作。 PS: I converted all the words in lowercase to avoid case sensitivity. PS:我将所有单词都转换为小写,以避免区分大小写。 Below is my code. 下面是我的代码。 Pls help me to understand what is wrong? 请帮我了解什么是错的? I am using Java code. 我正在使用Java代码。

import java.util.HashSet;
import java.util.Set;


public class SetTest {

    public static void main(String[] args) {
        Set<String> placeSet=new HashSet<String> ();

        String s1="traffic";
        String s2="mumbai";
        String s3="Mumbai";
        String s4="roadcollapse";

        placeSet.add(s1.toLowerCase());
        placeSet.add(s2.toLowerCase());
        placeSet.add(s3.toLowerCase());
        placeSet.add(s4.toLowerCase());

        for (String place:placeSet)
        {
            if (!place.contains("traffic") || !place.contains("collapse"))
            {
                System.out.println (place);
            }
        }

    }

}

If you want to print a word only if it doesn't contain any of blacklisted words, then the condition should look like this: 如果您只想在不包含任何列入黑名单的单词的情况下打印该单词,则条件应如下所示:

 for (String place:placeSet)
    {
        if (!(place.contains("traffic") || place.contains("collapse"))) {
            System.out.println (place);
        }
    }

Because you want to print if word does not contain word_1 or word_1, so it should be NOT (condition1 OR condition2) 因为要在单词不包含单词_1或单词_1的NOT (condition1 OR condition2)进行打印,所以它应该NOT (condition1 OR condition2)

In case of multiple blacklisted words, you can work with a Set also: 如果有多个列入黑名单的单词,您还可以使用Set:

public static void main(String[] args) {
    Set<String> blacklist = Stream.of("traffic","collapse").collect(Collectors.toSet());
    ...
    for (String place:placeSet) {
        if (blacklist.stream().noneMatch(place::contains)) {
            System.out.println (place);
        }
    }
}

If the string contains that specified word i don't want to print that. 如果字符串包含指定的单词,我不想打印该单词。

Use && instead of || 使用&&代替||

 for (String place:placeSet)
        {
            if (!place.contains("traffic") && !place.contains("collapse"))
            {
                System.out.println (place);
            }
        }

Convenient way for me - stream API 对我来说方便的方法-流API

    placeSet.stream()
            .filter(p -> !p.contains("traffic"))
            .filter(p -> !p.contains("collapse"))
            .forEach(System.out::println);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM