I have a string
String string="I would loved to be the part of cricket team"
and I have a arraylist
ArrayList <String> list = new ArrayList();
list.add("part");
list.add("bend");
list.add("bet");
list.add("bear");
list.add("beat");
list.add("become");
list.add("begin");
Now I want to search from this string using this arraylist. How can i Do that without using for loop ?
asked Aug 21, 2015 at 11:39
4 Answers 4
Using Java 8 streams:
list.stream()
.filter(s -> string.contains(s))
.forEach(System.out::println);
answered Aug 21, 2015 at 11:54
1 Comment
marstran
@OnkarMusale Probably because none of the search strings matched the input.
You have to check one by one by using String.compare()
function.
Is it what you're asking?
Francisco Romero
13.2k23 gold badges100 silver badges173 bronze badges
answered Aug 21, 2015 at 11:45
Comments
for (String x : list) {
if(string.contains(x)) {
System.out.print(x);
}
}
marstran
28.3k5 gold badges66 silver badges71 bronze badges
answered Aug 21, 2015 at 11:45
3 Comments
Andrew
How can i Do that without using for loop ?
SacJn
@AndrewTobilko don't think so, it can be
Vipul Jain
@AndrewTobilko- I know we can use in this way also, but I was somehow trying to avoid that, that's why I asked this question
// Use JAVA 8 feature
String str = "I would loved to be the part of cricket team";
ArrayList <String> list = new ArrayList();
list.add("part");
list.add("bend");
list.add("bet");
list.add("bear");
list.add("beat");
list.add("become");
list.add("begin");
boolean anyMatch = list.stream().anyMatch(s -> s.contains(str));
answered Aug 25, 2015 at 13:32
Comments
lang-java
Now I want to search from this string using this arraylist
Can you explain what it is ?