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
Java Streams Sum Values of a List of Maps
How to Override Spring Data JPA Repository Base Methods
Convert to Int from String of Numbers Having Comma
Program With Threads for Matrix Multiplication
Add Additional Rules to the Compare Method of a Comparator
How to Write New Line Character to a File in Java
How to Remove Duplicate and Sort Objects from Jsonarray Using Java
How to Return Different Results When Calling the Same Mocked Method
How to Mock JPA Repository'S Save Method in Unit Tests
How to Shut Down a Spring Boot Command-Line Application
How to Extract Ip Address in Spring MVC Controller Get Call
Maven: Best Way of Linking Custom External Jar to My Project
How to Query Dynamodb Based on Partition Key and Sort Key [Java]
How to Execute SQL Script File in Java
How to Accept Only Numeric Values in a Jtextfield
How to Efficiently Return the Max Possible Integer With a Given Number of Digits
Server_Error: [Code] 1675030 [Message]: Error Performing Query
How to Listen With Multiple Port for Multiple Client in Single Jpos Instance