1

I am trying to find if a list contains a string. I have a list object as follows:

Please note this is just example code, to illustrate my point/question!

import java.util.List;

public class FilterByList {

    private String actionHero;
    private String actionHero2;
    private String move;
    private int number;
    private String actionHero3;

    public FilterByList(String actionHero, String actionHero2, String move, int number, String actionHero3) {
        this.actionHero = actionHero;
        this.actionHero2 = actionHero2;
        this.move = move;
        this.number = number;
        this.actionHero3 = actionHero3;
    }

    public String getActionHero() {
        return actionHero;
    }

    public void setActionHero(String actionHero) {
        this.actionHero = actionHero;
    }

    public String getActionHero2() {
        return actionHero2;
    }

    public void setActionHero2(String actionHero2) {
        this.actionHero2 = actionHero2;
    }

    public String getMove() {
        return move;
    }

    public void setMove(String move) {
        this.move = move;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getActionHero3() {
        return actionHero3;
    }

    public void setActionHero3(String actionHero3) {
        this.actionHero3 = actionHero3;
    }
}

And then:

public static void main(String[] args) {

    List<FilterByList> myList = Collections.singletonList(
    new FilterByList("superman", "antman", "ACTION", 123, "batman"));

    System.out.println(myList);
    if (myList.contains("batman")) {
        System.out.println("found it!");
    } else {
        System.out.println("************************* Did not find anything!!1");
    }
}

It does NOT find batman in the list object. So what is printed is the follows:

[com.company.FilterByList@7577b641]
************************* Did not find anything!!1

Any idea how I can:

a) print the contents of the list?

b) find an element in the list with as little code as possible?

Would appreciate your help. Please use my code context to answer as it will give me more pointers.

6
  • 1
    Can you show the FilterByList class Commented Dec 28, 2018 at 13:56
  • It is usually considered not best practice to store multiple different types in the same collection. Why do you need to do this? Commented Dec 28, 2018 at 13:57
  • Add toString method to FilterByList and run myList.forEach(System.out::println); Commented Dec 28, 2018 at 13:59
  • 1
    myList contains only one FilterByList object, and this object doesn't equal to "batmat". Commented Dec 28, 2018 at 14:00
  • @DanilaZharenkov - Sure. Any way to see what is inside that FilterByList object? Commented Dec 28, 2018 at 14:02

4 Answers 4

1

You run contains on List<FilterByList> and that list doesn't have String batman It has instance of FilterByList, that one of members is field of type string and value 'batman'

Based on your code. You create instance of object FilterByList and you try to compare that object with String

Line

if (myList.contains("batman")) {

Those object are different types, that is the reason, why it is not found

To check if there is batman you can use Stream API

boolean d = myList.stream().map(FilterByList::getActionHero3).anyMatch(hero3 -> hero3.equals("batman"));
Sign up to request clarification or add additional context in comments.

3 Comments

Can you give me an example (in code) based on my code please, sorry. Just trying to understand more...
Ok i see what you are saying. Is there a way to find it?
Wow. Streams is a nice thing to do! I will upvote but the other guy answered first so can award him. I really appreciate your help and telling me about stream api. I'll definitely read up more on it. :)
1

Add to your FilterByList class containsHero(String hero) where you compare hero with each of 3 heroes. You don't need to store this single object in List. Just use it.

  FilterByList f = new FilterByList("superman", "antman", "ACTION", 123, "batman");
  if (f.containsHero("batman")) {
      System.out.println("found it!");
  } else {
      System.out.println("************************* Did not find anything!!1");
  }

P.S. imho all the things you are trying to do looks very strange...

Comments

1

I added a new method to the class for searching

public boolean containsHero(String hero) {
    return actionHero.equals(hero) ||  actionHero2.equals(hero) ||  actionHero3.equals(hero);
}

And then used it with streams like this

if (myList.stream().anyMatch(f -> f.containsHero("batman"))) {
    System.out.println("found it!");
} 

To get a readable output of your class you can override the toString() method, here is one example

@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append(actionHero);
    builder.append(", ");
    builder.append(actionHero2);
    builder.append(", ");

    builder.append(actionHero3);
    builder.append(": ");

    builder.append(move);
    builder.append(": ");

    builder.append(number);

    return builder.toString();
}

Doing System.out.println(myList);will then output

[superman, antman, batman: ACTION: 123]

Comments

0

You can put into the list two different types of objects, e.g. String and Integer if you will omit the generic type, and implicitly it will be List<Object> myList = new ArrayList<Object>(); for example:

List myList = new ArrayList();
myList.add("superman");
myList.add("antman");
myList.add("ACTION");
myList.add(123);
myList.add("batman");

System.out.println(myList);
if (myList.contains("batman")) {
    System.out.println("found it!");
} else {
    System.out.println("************************* Did not find anything!!1");
}

But it's not a good way to use collections. For sure, it depends of your particular task, but I'd like to advice to use different collections with different types.

If you want to achieve exactly what you're asking about, you can use Stream API like this:

List<FilterByList> myList = Collections.singletonList(
new FilterByList("superman", "antman", "ACTION", 123, "batman"));

System.out.println(myList);
if (myList.stream().map(FilterByList::getActionHero3).allMatch("batman"::equals)) {
    System.out.println("found it!");
} else {
    System.out.println("************************* Did not find anything!!1");
}

anyMatch method is more preferable to use, if there will be more than one element in the collection:

(myList.stream().map(FilterByList::getActionHero3).anyMatch("batman"::equals))

3 Comments

Thanks for this. I needed to use List<FilterByList> myList, I updated my question..
I think it should be anyMatch instead allMatch
@wardziniak For the example which @Saffik was provided allMatch is more strict. But of course if it will be more that one element in the collection it will be better to use anyMatch. Thanks

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.