0

I have a list of object :

List<Object[]> list = new ArrayList<>();
Object[] object = {"test", "test1", "test2"};
list.add(object);

List contains some data.

I have another string String str = "test";

I am using below code. What are best other ways:

for (Object []object1 : list) {
     for (Object obj : object1) {
        if (obj.equals("test")) {
          System.out.println("true");
        }
     }
}

How to check this string present in above list with minimum of code.

1
  • Simple use contains () method Commented Mar 9, 2017 at 16:39

2 Answers 2

2

Java 8 introduced Streams which are powerful, yet code-compact as you demanded. This answer uses more features of Java 8 sucha as Lambdas and Method References.

Here is a one-liner instruction:

boolean containsObject = list.stream().flatMap(Arrays::stream).filter(s->str.equals(s) ).findFirst().isPresent();

Here how it works:

boolean containsObject = list.stream() // Turning the List into a Stream of Arrays
    .flatMap(Arrays::stream)           // flattening the 2D structure into a single-dimensional stream of Objects (Note: using a Method reference)
    .filter(s->str.equals(s))          // Filtering the flat stream to check for equality (Note: using a Lambda expression)
    .findFirst()                       // Demands to find the first  Occurence that passed the Filter test
    .isPresent();                      // Collapse the stream and returns the result of the above demand (Note: the Stream makes no computation until this instruction)

This solution is code-compact, and brings the nice features of Streams such as parallelization and laziness.

Sign up to request clarification or add additional context in comments.

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
0

If you convert the Object[]s to lists, then you can call their contains(Object). You could either have list be a List<List<Object>>, or you could leave it with Object[] and wrap the Object[]s in a List as-needed.

Example of the "convert as needed":

for(Object[] object1 : list)
    if(Arrays.asList(object1).contains("test"))
        System.out.println("true");

Personally, I would have list be a List<List>. Whenever you add to it, just wrap your arrays in a list. Assuming arr is an Object[], that means list.add(Arrays.asList(arr));.

Alexander's answer is also correct (I think; I didn't examine it too closely), but I find long strings of stream operators to be less readable. If you disagree with my opinion on that, then use the stream operators.

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.