Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我有两个字符串,需要与单词进行比较_Java_Core - Fatal编程技术网

Java 我有两个字符串,需要与单词进行比较

Java 我有两个字符串,需要与单词进行比较,java,core,Java,Core,我尝试过拆分字符串,但对我来说,阻碍我的是单词的索引不同 在输出中,我需要打印“win”我建议您使用列表而不是数组,它可以简化您在任何角度下的生活 有了这些代码,您就可以实现您的目标 String first-word="they will win"; String second-word="will they"; String[] spiltfirstWord= firstWord.split("\\s+"); String[] spiltsecondWord= secondWord.s

我尝试过拆分字符串,但对我来说,阻碍我的是单词的索引不同


在输出中,我需要打印“win”

我建议您使用
列表
而不是数组,它可以简化您在任何角度下的生活

有了这些代码,您就可以实现您的目标

String first-word="they will win";

String second-word="will they";

String[] spiltfirstWord= firstWord.split("\\s+");

String[] spiltsecondWord= secondWord.split("\\s+");
String firstWord=“他们会赢”;
String secondWord=“他们愿意”;
List firstList=Arrays.asList(firstWord.split(\\s+));
List secondList=Arrays.asList(secondWord.split(\\s+));
列表结果=新建ArrayList();
for(字符串字:firstList){
如果(!secondList.contains(word)){
结果:添加(word);
}
}
系统输出打印项次(结果);
输出:

[赢]

PS:您可以用Java8Lambdas代替cicle

    String firstWord="they will win";
    String secondWord="will they";

    List<String> firstList = Arrays.asList(firstWord.split("\\s+"));
    List<String> secondList = Arrays.asList(secondWord.split("\\s+"));
    List<String> result = new ArrayList<>();

    for( String word : firstList){
        if(!secondList.contains(word)){
            result.add(word);
        }
    }

    System.out.println(result);
List result=firstList.stream()
.filter(word->!secondList.contains(word))
.collect(Collectors.toList());

您应该将第一个字符串映射为字符串数组,将另一个字符串映射为一组字符串。这将简化匹配操作并提高效率

它将给出这样一种逻辑:

    List<String> result = firstList.stream()
            .filter(word -> !secondList.contains(word))
            .collect(Collectors.toList());
String firstWord=“他们会赢”;
String secondWord=“他们愿意”;
设置splitsecondWord=Arrays.stream(secondWord.split(\\s+))
.collect(收集器.toSet());
List missingWords=Arrays.stream(firstWord.split(\\s+))
.filter->!splitsecondWord.contains
.collect(Collectors.toList());
System.out.println(漏字);
输出:

[赢]


另一种可能的解决方案是使用
模式

String firstWord = "they will win";
String secondWord = "will they";

Set<String> splitsecondWord = Arrays.stream(secondWord.split("\\s+"))
                                    .collect(Collectors.toSet());

List<String> missingWords = Arrays.stream(firstWord.split("\\s+"))
                                .filter(s -> !splitsecondWord.contains(s))
                                .collect(Collectors.toList());

System.out.println(missingWords);
patternspilter=Pattern.compile(\\s+);
String firstWord=“他们会赢”;
String secondWord=“他们愿意”;
List secondWordList=splitter.splitAsStream(secondWord.collect(Collectors.toList());
splitter.splitAsStream(第一个字)
.filter(w->!secondWordList.contains(w))
.forEach(System.out::println);
在这里,您可以轻松完成
String firstword=“他们总有一天会赢”;
String secondword=“他们愿意”;
List diff=Arrays.stream(firstword.split(\\s+)).filter(s->!secondword.contains)
.collect(Collectors.toList());
系统输出打印项数(差异);//输出[赢,一,天]

你的意思是想获取一个字符串中的单词,而不是另一个字符串中的单词?顺便说一下,标识符中不允许使用hypens。你可以尝试
收集(Collectors.toList())
而不是创建一个列表。好的提示,编辑!;)您还必须知道,
Arrays.asList
将返回一个虚假的列表,而许多普通的列表操作都不受支持。
    Pattern splitter = Pattern.compile("\\s+");
    String firstWord = "they will win";
    String secondWord = "will they";
    List<String> secondWordList = splitter.splitAsStream(secondWord).collect(Collectors.toList());

    splitter.splitAsStream(firstWord)
            .filter(w -> !secondWordList.contains(w))
            .forEach(System.out::println);
 String firstword = "they will win one day";
        String secondword = "will they";
        List<String> diff = Arrays.stream(firstword.split("\\s+")).filter(s -> !secondword.contains(s))
                .collect(Collectors.toList());
System.out.println(diff); // output [win, one, day]