2

Scenario

I've got an ArrayList<[String, String, String, String]> to store data.
Now I'm trying to check if the ArrayList contains String.

for(String i : checkArtefakts){
            System.out.println(i);
            if(headerAndBodyTestSuites.toString().contains(i)){
                System.out.println(i + "  " + headerAndBodyTestSuites.indexOf(headerAndBodyTestSuites.contains(i)));
            }
        }

I've implemented a toString() Method but it also didn't solve my problem.

Question

How can I check each Element of ArrayList ([String,String,String,String])

if it contains my search String.

And why wouldn't it even find it in my toString() method?

public String toString() {
    return "[XQ: " + xqueryFileName + "] [Path: " + testDir + "] [INP: " + firstInputFile
        + "] [INP: " + secondInputFile + "] [EXP: " + expectedFile + "]";

}

Example

ArrayList<HeaderAndBodyTestcase> where HeaderAndBodyTestcase =

public HeaderAndBodyTestcase(final String xqueryFileName, final String testDir,
        final String firstInputFile, final String secondInputFile, final String expectedFile)
11
  • You can create some better data structure then ArrayList ([String,String,String,String]) Commented Jul 20, 2017 at 8:37
  • 2
    What type is [String, String, String, String] ? Commented Jul 20, 2017 at 8:38
  • What are you recommending?@Abubakkar @TimBiegeleisen it's of time HeaderAndBodyTestcase which takes 4 Strings. Commented Jul 20, 2017 at 8:38
  • You want to check if one of the four string of you object (for each of the list) is equals to search string ? Commented Jul 20, 2017 at 8:45
  • 1
    You are so unclear .. Commented Jul 20, 2017 at 8:56

3 Answers 3

5

If you were using List<String[]> you can use Arrays.asList(), to check if the String[] contains your string with .contains() method.

But in your case it's a List of HeaderAndBodyTestcase, so you need to implement a contains method in your HeaderAndBodyTestcase class to check if any of the class members is equal to the searched String.

This is how should be your code:

public boolean contains(String search){
    return this.xqueryFileName.equals(search) || this.testDir.equals(serach) || this.firstInputFile.equals(search) || this.secondInputFile.equals(search) || this.expectedFile.equals(search);
}
Sign up to request clarification or add additional context in comments.

2 Comments

contains instead of equals solved my problem. Thank you!
@0x45 great, glad it helps, yes using .contains is better if the members String s contains other characters thatn the searched ` String`.
3

You need to implement a method in the class which will look if the search String is one of the attribute :

public boolean objectContainString(String search) {
     return Arrays.asList(xqueryFileName, testDir, firstInputFile, secondInputFile, expectedFile)
                  .contains(search);
}

And use like this : boolean bool = list.stream().anyMatch(e -> e.objectContainString(search));

3 Comments

@0x45 you can think about vote up answers you like/thank ;)
I did but my Reputation is too low >:-(
@0x45 I vote up your question so you get more than 15rep so you can vote up whenever you want ;)
0

You can do something simple as this:

    String searchedString = "SomeString";
    boolean found = false;
    for(String string : arrayList) {
          if (searchedString.equals(string)){
              found = true;
          }
    }

Or you could use the java stream API. (Requires Java 8)

    List<String> list = new ArrayList<>();
    list.add("SOME_STRING_1");
    list.add("SOME_STRING_2");
    list.add("SOME_STRING_3");
    String searchString = "SOME_STRING...";
    if(list.stream().anyMatch((string)->string.equals(searchString))) {
        // The string was in the list
    } else {
        // The string was not in the list
    }

Where arrayList contains your strings. Im not sure what you meant by ArrayList<[String, String, String, String]>, but this might be what you are looking for.

3 Comments

@0x45 Aha, you want to check if a search string is a substring of any of the strings in the arraylist? Ex. ["hello", "goodbye", "hello goodbye"], then you supply it with for example: "ello", and you would get string at index 0, and 2? Yes?
I just want to check if it contains the string, I don't care about the index. But generally yes
instead of list.stream().filter((string)->string.equals(searchString)).findFirst().isPresent()use list.stream().anyMatch(...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.