Which collection allows duplicate and null values in Java?

Prepare for the Revature Test with our comprehensive study material. Use flashcards and multiple-choice questions, each with hints and explanations. Get exam-ready now!

The collection that allows duplicate and null values in Java is the List. Specifically, the List interface is designed to hold an ordered collection (also known as a sequence) of elements. It allows multiple instances of the same object, which means duplicates are permitted. Furthermore, a List can contain null values, meaning that it is possible to have null elements within the collection.

For instance, implementations of the List interface, such as ArrayList and LinkedList, support these characteristics. ArrayList allows adding multiple entries of the same value and can also store null as an element. This flexibility of having both duplicates and nulls is a defining feature that distinguishes List from other collections like Set, which does not allow duplicates, and Map, which treats keys uniquely but can have null values associated with those keys.

Although arrays (the last option) also allow duplicate and null values, they are not considered a collection in the same sense as the Java Collection Framework, which makes List the more appropriate answer in the context of this question.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy