-1

I am new to Java8 streams and was playing around with them. I was following this talk by Venkat Subramaniam Programming with Streams in Java 8 and coding it.

I called .stream() twice one after another got the following results:-

  1. get, in uppercase, the names of all female older than 18

[SARA, SARA, PAULA]

  1. print all males

basicCode.Person@c39f790

basicCode.Person@71e7a66b

  • Am I missing something in my code? Why I am getting object addresses? toString() was missing in the class
  • Can I call .stream() immediately after the previous .stream() YES we can

I am doing the following:-

List <Person> people = createPeople(); // creating objects

    System.out.println("1. get, in uppercase, the names of all female older than 18");
    System.out.println( 
            people.stream()
            .filter(person -> person.getGender() == Gender.FEMALE)
            .filter(person -> person.getAge() > 18)
            .map(person -> person.getName().toUpperCase())              
            .collect(toList())
            );

            System.out.println("2. print all males");
            people.stream()
            .filter(p -> p.getGender() == Gender.MALE)
            .forEach(System.out::println);

public class Person {
private final String name;
private final Gender gender; //is an enum {MALE, FEMALE}
private final int age;
}
Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
jaamit
  • 393
  • 3
  • 11
  • 3
    You don't have a `toString` in your `Person` class so you get the default. – M. Deinum Nov 10 '17 at 11:53
  • yes..override the toString() method in your Person class and print out the relevant details – akshaya pandey Nov 10 '17 at 11:54
  • thanks Deinum..sometimes we forget basics while striving for advanced. – jaamit Nov 10 '17 at 12:00
  • @jaamit that only happens when you ignore basics and attempt to reach too far before you're ready. You can't just skip ahead and pretend you can start from streams or other "more interesting" things and ignore all the other basic building blocks. At least not if you intend to become a reliable programmer. – Kayaman Nov 10 '17 at 12:04
  • 2
    @Kayaman I don't agree with you... I have fair Java 5 & 7 experience but sometimes while learning new things you try things e.g. streams and mix and match examples. When things don't work we doubt new learning and may overlook basics... – jaamit Nov 10 '17 at 12:07
  • Overriding `toString()` is absolute basic knowledge. I don't believe one bit that you have significant experience, especially when you distinguish versions with "Java 5 & 7 experience". I assume you've acquired some form of certificates or have taken courses, but are missing the actual real world experience and understanding. – Kayaman Nov 10 '17 at 12:12
  • @Kayaman I don't have any certificate or course.. I am professional developer... Java 5 vs 7 is wrt usage of new idioms...let's leave it here – jaamit Nov 10 '17 at 12:15
  • 1
    A **professional** developer doesn't get confused when he hasn't implemented `toString()`. A beginner programmer gets confused at that. – Kayaman Nov 10 '17 at 12:17
  • @Kayaman I agree. Especially with the formulation "Why I am getting object addresses?" – glglgl Nov 10 '17 at 12:46
  • 1
    @Kayaman don't assume overlooking toString() is a criteria for judging ... I use different languages for different projects .. anyways I don't want to carry on with this conversation with you ... – jaamit Nov 10 '17 at 12:49
  • I'm not assuming anything. It's an excellent criteria for judging. For example if this came up in a job interview, I would send the applicant packing right away. I don't care if you know how to use streams if you don't understand how the **absolute basics** of the language work. – Kayaman Nov 10 '17 at 12:52

2 Answers2

2

Anaother alternative would be to do the same as you do with your females, but omit the .toUpperstring().

See how you call .getName() for the females, but don't for the males.

So do

System.out.println("2. print all males");
people.stream()
    .filter(p -> p.getGender() == Gender.MALE)
    .map(p -> p.getName()) // this line was missing
    .forEach(System.out::println);

or just

System.out.println("2. print all males");
people.stream()
    .filter(p -> p.getGender() == Gender.MALE)
    .map(Person::getName) // this is an alternative
    .forEach(System.out::println);
glglgl
  • 89,107
  • 13
  • 149
  • 217
1

You are.missing toString() implementation in your person class, so you're getting the basic object toString.
Implement it and you'll get what you want. Your streams are all fine

Nir Levy
  • 12,750
  • 3
  • 21
  • 38