Net-informations.com
SiteMap  | About    

Difference between List and Set in Java

List is an ordered sequence of elements whereas Set is a distinct list of elements which is unordered

Difference between List and Set in Java collection

List is a type of ordered collection that maintains the elements in insertion order while Set is a type of unordered collection so elements are not maintained any order.

List allows duplicates while Set doesn't allow duplicate elements . All the elements of a Set should be unique if you try to insert the duplicate element in Set it would replace the existing value.

List permits any number of null values in its collection while Set permits only one null value in its collection.

New methods are defined inside List interface . But, no new methods are defined inside Set interface, so we have to use Collection interface methods only with Set subclasses .

List can be inserted in in both forward direction and backward direction using Listiterator while Set can be traversed only in forward direction with the help of iterator

When to use

List is an ordered sequence of elements, however set is distinct list of element which is unordered. So, use list for storing non-unique objects as per insertion order and use set for storing unique objects in random order

Java collections framework

Java Collections are predefined set of classes or data structures which can be used to store multiple items in a single unit. Dynamically allocated data structures in Java (such as Hashtable, HashSet, HashMap,LinkedList, Vector, Stack, ArrayList) are supported in a unified architecture called the Collection Framework, which mandates the common behaviours of all the classes. More about.... Java Collection Framework







net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.