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 ?

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);
Sign up to request clarification or add additional context in comments.

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?

Comments

0
for (String x : list) {
    if(string.contains(x)) {
        System.out.print(x);
    }
}

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));

Comments

Your Answer

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.