检查字符串中的字符串列表

时间:2018-02-19 13:56:31

标签: java arrays string arraylist

我有List<String>List<Object>。在List<String>中,我有我想要的字符串。在另一个List<Object>中,其中一个字符串变量将包含我想要的所有字符串。我怎样才能获得该字符串或当我找到所有listOne字符串时如何返回true。

示例:

List<String> listOne = ["I have"," three"," Dollars"]

List<Object> listTwo = [[1,"I have One Dollar", 500],
                        [2,"I have two Dollars", 541],
                        [31,"I have three Dollars with card", 568]
                        [3,"I have three Dollars", 568],
                        [4,"I have Four Dollars", 521]]

当我的listOne字符串完全匹配时,如何从listTwo获取第四个对象。

代码部分:

Details.java:

public class Details {
     int sNo;
     String text;
     int value;

     public int getsNo() {
       return sNo;
     }
     public void setsNo(int sNo) {
       this.sNo = sNo;
     }

     public String getText() {
        return text;
     }
     public void setText(String text) {
        this.text = text;
     }

    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
  }

主要类别:

package com.adp.aca.core.helper.daoimpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.velocity.runtime.directive.Foreach;

public class TestClass {



    static int count ; 

    public static void main(String args[]) {

        List<String> listOne = new ArrayList<String>();
        listOne.add("I have");
        listOne.add(" three");
        listOne.add(" Dollars");

        List<Details> listTwo = new ArrayList<Details>();

        Details detailOne = new Details();
        detailOne.setsNo(1);
        detailOne.setText("I have One Dollar");
        detailOne.setValue(500);

        Details detailTwo = new Details();
        detailTwo.setsNo(2);
        detailTwo.setText("I have two Dollars");
        detailTwo.setValue(541);

        Details detailThree = new Details();
        detailThree.setsNo(31);
        detailThree.setText("I have three Dollars with card");
        detailThree.setValue(568);

        Details detailFour = new Details();
        detailFour.setsNo(3);
        detailFour.setText("I have three Dollars");
        detailFour.setValue(568);

        Details detailFive = new Details();
        detailFive.setsNo(4);
        detailFive.setText("I have Four Dollars");
        detailFive.setValue(521);


        listTwo.add(detailOne);
        listTwo.add(detailThree);
        listTwo.add(detailFour);
        listTwo.add(detailFive);
        listTwo.add(detailTwo);


        List<String> actualStrings = new ArrayList<String>();

        for (Details detail : listTwo) {
            actualStrings.add(detail.getText());
        }

       /** Ended up here ***/
        for (int i = 0; i < actualStrings.size(); i++) {
            for (int j=0; j<listOne.size();j++) {
            actualStrings.get(i).contains(listOne.get(j));
             count=i;
            }

        }       
    }
}

2 个答案:

答案 0 :(得分:0)

static String concat(List<String> l) {
    StringBuilder sb = new StringBuilder();
    for(String s : l)
        sb.append(s);
    return sb.toString();
}

static Details getMatch(List<Details> listTwo, String s) {
    for (Details d : listTwo) {
        if (d.getText().equals(s))
            return d;
    }
    return null;
}

您可以使用以下功能:

Detail d = getMatch(listTwo, concat(listOne));

答案 1 :(得分:0)

你可以从List one构建一个String并在循环中与Object列表进行比较,或者你可以迭代2个列表并进行比较,如伪代码所示。

for (Details detail: listTwo) {
String text = detail.getText();
String[] texts = text.split(" ");
List textList = Arrays.asList(texts);
if (texts.length == listOne.size()) {
    int count = 0;
    for (String val: listOne) {
        if (textList.contains(val) count++;
        else break;
    }
    if (count == listOne.size()) {
        result = detail;
    }
}
else {
    break;
}

}