根据Java中的第一个迭代变量对嵌套循环元素进行分组 [英] Grouping the nested loop elements based on the first iteration variable in Java

查看:176
本文介绍了根据Java中的第一个迭代变量对嵌套循环元素进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个列表,其中一个句子用于关键字,另一个用于关键字.这个想法是检查句子中是否有关键字.并按顺序将它们放在每个句子的列表中.

I have 2 lists one for the sentence one for the keywords. The idea is to check if the sentence have the keywords. and put them in a list for each sentence in order.

对不起,如果已经在这里提前复制了.

I am sorry if this is already duplicated here in advance.

List <String> sentence= new ArrayList <>();
sentence.add("this is a good dog");
sentence.add("cats drink milk");
sentence.add("Animals are beautiful creatures");

List <String> keyword= new ArrayList <>();
keyword.add("dog");
keyword.add("cats");
keyword.add("beautiful");
keyword.add("good");
keyword.add("are");
keyword.add("this");
keyword.add("milk");

我的想法是为每个列表创建2个嵌套循环:

My idea was to create 2 nested loops for each list:

for (int b = 0; b < sentence.size(); b++) {
    for (int c = 0; c < keyword.size(); c++) {
        if (sentence.get(b).contains(keyword.get(c))) {
            System.out.println(keyword.get(c));
        }
    }
}

此输出为:

dog
good
this
cats
milk
beautiful
are

所需的输出将是:

[this,good,dog]
[cats,milk]
[are,beautiful]

这就像按句子顺序获取所有现有关键字一样,与关键字顺序无关.

So it is like getting all the existing keywords, in the order of the sentence,not related to keywords order.

,然后按照存在顺序对每个句子的现有关键字进行分组.

and then group the existing keywords for each sentence, as in the order of existence.

希望很清楚.真的很感谢任何想法.不必遵循相同的方法.

Hope it is clear. Would really appreciate any ideas. doesnt have to follow the same method.

推荐答案

遍历您的sentence列表.对于每个句子,请遍历您的keyword列表.添加在tempList中找到的每个找到的关键字,按句子中关键字的索引对tempList进行排序,最后将每个tempList添加到列表列表中.示例:

Iterate over your sentence list. For each sentence iterate over your keyword list. Add each found keyword found in a tempList, sort the tempList by the index of keyword in sentence and finally add each tempList to a list of lists. Example:

public static void main(String[] args) {
    List <String> sentence= new ArrayList <>();
    sentence.add("this is a good dog");
    sentence.add("cats drink milk");
    sentence.add("Animals are beautiful creatures");

    List <String> keyword= new ArrayList <>();
    keyword.add("dog");
    keyword.add("cats");
    keyword.add("beautiful");
    keyword.add("good");
    keyword.add("are");
    keyword.add("this");
    keyword.add("milk");

    List<List<String>> result = new LinkedList<>();
    for(String sen: sentence){
        List<String> tempList = new ArrayList<>();
        for(String key: keyword){            
            if(sen.contains(key)){
                tempList.add(key);
            }
        }
        tempList.sort(new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                   return sen.indexOf(o1) -  sen.indexOf(o2) ;
                }
        });
        result.add(tempList);
    }
    for(List<String> r : result){
        System.out.println(r);
    }
}

这篇关于根据Java中的第一个迭代变量对嵌套循环元素进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆