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?