0

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
6
  • 1
    Now I want to search from this string using this arraylist Can you explain what it is ? Commented Aug 21, 2015 at 11:40
  • what you have tried so far.? Commented Aug 21, 2015 at 11:41
  • Means that if I search from this string using this arraylist then result should be "part". Commented Aug 21, 2015 at 11:41
  • You will need to iterate over the list somehow. Java 8 provides you with Streams and Lambda's, tried those yet? Commented Aug 21, 2015 at 11:42
  • try using string.contains(array[i]) and iterate over it and returnthe results.. Commented Aug 21, 2015 at 11:46

4 Answers 4

1

Using Java 8 streams:

list.stream()
 .filter(s -> string.contains(s))
 .forEach(System.out::println);
answered Aug 21, 2015 at 11:54

1 Comment

@OnkarMusale Probably because none of the search strings matched the input.
0

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

0
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

How can i Do that without using for loop ?
@AndrewTobilko don't think so, it can be
@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
0
// 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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.