Create collection in Java without knowing the type?

For example, can I create List that will contain String and other List? And if I can do that, what is the specific of my List? I can just say that it contains data of type Object, right?

Jon Skeet
people
quotationmark

Yes, you can use:

List<Object> list = new ArrayList<>();
list.add("A string");
list.add(new ArrayList<Foo>());
// etc

It's usually not a good idea, as when you get items out of the list, it can be a pain to work out what you need to do with them. If you can design your way out of that, it would be a good idea... but the above will work.

people

See more on this question at Stackoverflow