How to check if an item in an ArrayList does not contain a certain word?

Multi tool use
Multi tool use


How to check if an item in an ArrayList<String> does not contain a certain word?



I want to use a for loop like this:


`button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Object.setList();

for(int i = 0; i < Object.getList.size(); i++)
if(!Object.getList.get(i).trim().toLowerCase().contains("hello")) {
Toast.makeText(getContext(), "List does not contain "Hello", Toast.LENGTH_LONG).show;
}
}`



This is my model:


public class Object {
private ArrayList<String> mArrayList;

public void setList() {
mArrayList = new Arraylist<>();
mArrayList.add("Random Message");
mArrayList.add("ZZZZ")
}

public ArrayList<String> getList() {
return mArrayList;
}
}



I still keep getting the Toast message, but it works just fine when i use:


if(!Object.getList().toString.trim().toLowerCase().contains("hello"))





what is your ouput and what is the desired output ?
– valik
Jul 1 at 10:32




3 Answers
3



I am not really sure how this expression:


Object.getList().toString.trim().toLowerCase().contains("hello")



works. Consider two adjacent elements: "he" and "llo". Even in this case your solution would state that the string "hello" exists in the ArrayList. Since there is ho hashing in ArrayList, the only solution is via brute force (Giving a performance of O(n)). Here is the solution:


ArrayList


ArrayList


O(n)


for(String element: list) {
if (str.equalsIgnoreCase(element)) {
// Found
}
}





Object.getList().toString.trim().toLowerCase().contains("hello") works because Object.getList().toString.trim().toLowerCase() would return "hello world!, random message, zzzz"
– Satyendra Kumar
Jul 1 at 10:53


Object.getList().toString.trim().toLowerCase().contains("hello")


"hello world!, random message, zzzz"





I mean, you consider 2 elements: "he" and ", llo". The point being that the operation is not the right way of testing the existance of elements and will fail on boundary conditions.
– Prashant
Jul 1 at 10:55





Actually it would not fail, if list contains "he" and "llo" adjacent. However the code Object.getList().toString.trim().toLowerCase().contains("hello") is absolutely wrong, because it tries to find if the String representation of list contains the word or not, when actually you want to find whether list contains the word or not.
– Satyendra Kumar
Jul 1 at 10:59


Object.getList().toString.trim().toLowerCase().contains("hello")



If you want to do it one line, do it like this:


if(object.getList().stream().noneMatch(it -> "hello".equalsIgnoreCase(it))){
// ...
}



This will work if you are using Java 8.



If your aim is to check if each string in the list does not have "hello" and show toast each time then you can do so :


Object.getList().forEach(el -> {
if(!el.toLowerCase().contains("hello")){
// show toast
}
})



If your aim is to check if each string in the list does not have "hello" and show toast only once the do so :


if(Object.getList().stream().noneMatch(el -> !el.toLowerCase().contains("hello"))){
// toast
}



If you wanna simply check if there is any sting that matches any element of list then do what @SatyendraKumar sugested.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

kJE2AShh2AIsfQXTB2DT04vR2E5IQLhTWEDBS6TnuX0fF8u0BRAL5neb MW7EMlv Npin8L6XPYP9ssAfSra7yZkQCDdmP0DP
ZTmBbn,QhuWx5pDxeR3 LGCr3WJydZR3BPdx bL JgLX,d3wthiRVKrsjhzT5Ci0Q,Zbeq5DN

Popular posts from this blog

Rothschild family

Cinema of Italy