-1

I'm trying to print out a list which represents rows from the database At the moment when I try to print the list with a stream, it prints out what I think is the location of the item?

This is what i see as output:

BLL.Teacher@6d9c638

BLL.Teacher@7dc5e7b4

Process finished with exit code 0

Each of these outputs is 1 row in the database, because when i add another, an extra output adds to it.

This the code which give above output:

public class Main {
public static void main(String[] args) {
    try {
        Controller.getAlleTeachers()
                .stream()
                .forEach(System.out::println);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
}

When I add a map function to it, then it gives the correct output.

public class Main {
public static void main(String[] args) {
    try {
        Controller.getAlleTeachers()
                .stream()
                .map(m->m.getFirstName())
                .forEach(System.out::println);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
}

So above code does output the first names of the rows in the database Also when i print out the list using foreach, the output is correct:

public class Main {
public static void main(String[] args) {
    try {
        for (Teacher t: Controller.getAlleTeachers()
             ) {
            System.out.println(t.getFirstName() + " " +
                               t.getLastName() + " " +
                               t.getCourse() + " " +
                               t.getStartDate());
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
}

So this last part of code also output all the correct information, So I know the problem isn't with connecting to db or accessing the data, or even the methods passing the data. So I thought it must've been something with the stream? How come I get such a strange output when using streams?

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
forgotteng
  • 23
  • 4

3 Answers3

0

The difference is that the lambda expression uses the toString() of the object, the for each loop uses the values of the fields. You can put

System.out.println(t.getFirstName() + " " +
                               t.getLastName() + " " +
                               t.getCourse() + " " +
                               t.getStartDate());

inside the lambda expression

bichito
  • 1,406
  • 2
  • 19
  • 23
0

When you are trying to Print an instance of teacher; toString() method of that object gets called. Since you haven't overriden toString() you get the default toString() gets printed. BLL.Teacher@6d9c638

And If i recall correctly that hexdecimal value that comes after @ is the memory location of the object

Long story short try to implement Public String toString(){ return this.name +.... rest of field ;} On the teacher object

harry
  • 310
  • 3
  • 15
0

Looks like you didn't override toString() method in Teacher class. BLL.Teacher@6d9c638 and BLL.Teacher@7dc5e7b4 - these are references for objects(Default toString() output). Try to override toString() method.

Valerii
  • 106
  • 9