Java generics error


I need your help because of a problem is a generics code. I reduced the problem to a test case. The code comprises of a class that is to be tested with JUnit, and the associated JUnit test case.

Something is wrong in my code, as the last line in the JUnit test fails. The Debugging class has a method that prints the content of a list:
 * Prints the content of the vector.
 * @param <T> the generic type
 * @param list the vector
public static <T> void printContent(final List<T> list) {
	for (final T element : list) {
		if (element instanceof List) {
			printContent((List<T>) element);
		} else {
			System.out.print(' ');

Open in new window

The list has elements after the method is called, and the contents are printed out by the printContent method as follows:

 generics.DataServiceTest$TrainClassDto@24e86073 generics.DataServiceTest$TrainClassDto@141430f3 generics.DataServiceTest$TrainClassDto@3999abd1 generics.DataServiceTest$TrainClassDto@17df43e2 generics.DataServiceTest$TrainClassDto@42e1b6ac

What I can't see is the error that results in the names of the DTO (data transfer object) objects instead of their contents. If their contents would have been translated, the 0. element would start with the ID of 1, which would make the second test case true.

Thank you for your help!
Ahmet Ekrem SABANSenior IT consultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nothing to do with generics. It's because TrainClassDto does not override toString

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ahmet Ekrem SABANSenior IT consultantAuthor Commented:
Thank you very much, sir! You opened my eyes. :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.