How to Check If an Arraylist of Strings Contains Substrings of Another Arraylist of Strings

java - check if a substring is present in an arraylist of strings in java

You cannot use contains method of ArrayList, because you cannot get around checking each string individually.

In Java 8 you can hide the loop by using streams:

boolean found = arr1.stream().anyMatch(s -> s.contains("string2"));

How to check if two String values from an Arraylist are contained in another Arraylist?

As in your case list2 is superset of list1 which you are checking, the for loop should look like:

boolean allElementsPresent = true;
for (String list11 : list1) {
if (!list22.contains(list11)){
allElementsPresent = false;
}
}
if(allElementsPresent){
System.out.println ("List 2 contains the strings from list1");
}

There is no need to iterate over list2 objects. You only need to iterate over list1 to check if each and every element exists in list2 or not.

ArrayList contains one or more entities from another ArrayList

Like list2.stream().anyMatch(list1::contains) in java 8.

Checking if an ArrayList contains another ArrayList as element

The issue here is that you're getting confused with the use of containsAll and the list of lists.

containsAll is a method that checks whether this collection contains all of the elements of the given collection. In this case:

  • This collection has 1 element, which is a List<String>;
  • The given collection has 3 elements, which are "One, "Two" and "Three".

And clearly, this collection, which only contains a List<String> (which is ["First, "Two", "Three"]), does not contain the 3 elements; it only contains a list of those three elements.

So what you really want here isn't containsAll, but contains, i.e. you want to check whether your list contains another list (and not its elements).

The following works:

if (!Main.contains(temp1)) {
Main.add(temp1);
}

and will result in Main being [[One, Two, Three]], added just once.

The side question is: why does it work? Well now, the question is: does my List<List<String>>, which is [[One, Two, Three]], contains this List<String>, which is [One, Two, Three]? Since two lists are equal when they have the same size and all of their elements are equal, it does contain it.

Find a String from one ArrayList as a Charsequence of another ArrayList String in Java 8

At first you should use functional style programming. forEach() doesn't allow you more than standard for (...)

For exact matching

ArrayList<String> arrayListFound = arrayListB.stream()
.filter(arrayListA::contains)
.collect(Collectors.toList());

For substring matcing

ArrayList<String> arrayListFound = arrayListB.stream()
.filter(itemB -> arrayListA.stream()
.matchAny(itemA -> itemA.contains(itemB))
)
.collect(Collectors.toList());

Check if a String is in an ArrayList of Strings

temp = bankAccNos.contains(no) ? 1 : 2;

Compare string with arraylist element and get another arraylist element

Pretty Simple stuff. You can use the following code:

public int getSecondListValue(ArrayList<String> list1, ArrayList<Integer> list2, String compare) {

for (int i = 0; i < list1.size() && i < list2.size(); i++) {
if (list1.get(i).equals(compare))
return list2.get(i);
}
return null;
}


Related Topics



Leave a reply



Submit