2

Hello so far this is what i have done

class Ideone{

    public static void main (String[] args) throws java.lang.Exception{

        Person p1 = new Person("person1");
        Person p2 = new Person("person1");
        Person [] array = new Person[]{p1, p2};

        String name_to_test = "person1";

        System.out.println(Arrays.toString(Arrays.stream(array).filter(x -> x.name.equals(name_to_test)).toArray()));

    }
}

class Person{

    public String name;

    public Person(String name){
        this.name = name;
    }

}

but i get

[Person@1ab7765, Person@128cdfa] while i want to return them by the same name which i filter them.

Any ideas?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Rentonie
  • 477
  • 1
  • 10
  • 25
  • 1
    possible duplicate of [How to create a println/print method for a custom class](http://stackoverflow.com/questions/8001664/how-to-create-a-println-print-method-for-a-custom-class) – Dici Sep 02 '15 at 09:57

3 Answers3

4

You should map() every filtered Person to its corresponding name:

Arrays.stream(array)
      .map(Person::getName)
      .filter(name_to_test::equals)
      .toArray();

Of course, you have to make sure that there is a getter method for name in Person:

class Person {
    public String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • That's nice but i specifically made the variable public to test it without getName function. – Rentonie Sep 02 '15 at 09:58
  • 2
    Then, instead of `Person::getName`, you can use `x -> x.name` – Konstantin Yovkov Sep 02 '15 at 09:59
  • Not very convincing. Clearly the OP does not know about `Object.toString`, so if you want him to learn something... BTW this is a duplicate and should be closed as such – Dici Sep 02 '15 at 09:59
  • @Dici Indeed i was missing the toString solution and i'm happy to get it under my belt. Althought this solution it's what i was looking for when i originally made the post, so it's hard for me to overlook his help. – Rentonie Sep 02 '15 at 10:08
  • 3
    Better to map first, then filter. This way you can even use `.filter(name_to_test::equals)`! – Tagir Valeev Sep 02 '15 at 10:10
  • @TagirValeev, true! I updated the answer accordingly. Thanks:) – Konstantin Yovkov Sep 02 '15 at 10:13
  • 4
    It's not so clear though why the OP wants to have an array of several equal names... – Tagir Valeev Sep 02 '15 at 10:23
  • @TagirValeev liked your suggestions for `.filter(name_to_test::equals)` very much i.e. use of method reference as Predicates. – Mrinal Sep 02 '15 at 18:39
2

You can @Override the Object#toString() method in your Person objects to print their names instead.

This will display their name property when you print out the array.

The [class]@[hash] notation is the default Object#toString() representation of objects when not overridden.

Mena
  • 47,782
  • 11
  • 87
  • 106
1

You have missed to override toString method in class.

class Person{

    public String name;

    public Person(String name){
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}

Now the output is: [Person{name='person1'}, Person{name='person1'}]

akhil_mittal
  • 23,309
  • 7
  • 96
  • 95