Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between List and Set

Difference Between List and Set

March 5, 2018 Posted by Lithmee

Key Difference – List vs Set
 

Most programming languages use arrays to store a set of data of the same type. One major drawback of  arrays is that, once the array size is declared, it cannot be modified. If the programmer wants to store a values exceeding the array size, then he should create a new array and copy the existing elements to the new array. In these situations, collections can be used. It is possible to add elements, delete elements and many other operations with the support of collections. There are different types of collections available in programming languages such as Java. List and Set are interfaces of collections hierarchy. The base interface for other interfaces is Collection. The key difference between List and Set is that List supports storing the same element multiple times while Set does not support storing the same element multiple times. Therefore, a Set does not allow duplication.

CONTENTS

1. Overview and Key Difference
2. What is List
3. What is Set
4. Similarities Between List and Set
5. Side by Side Comparison – List vs Set in Tabular Form
6. Summary

What is List?

The list is an interface that extends the Collection interface. There is a number of methods in Collection interface. The add method helps to add an element. The ‘remove method’ is to remove an element. There is ‘addAll method’ to add multiple elements while ‘removeAll method’ to remove the elements from the collection. The contains method helps to find whether a specific object is present in the List or not. The ‘containsAll’ is to find whether a set of objects are present in the collection. The iterator method is used to loop through the items of the list. As List extends Collection, all the methods of Collection belong to List. Other than those methods, the List has methods such as get and set. The programmer can get a value at a specific index using get method. The programmer can set a value at a specific index using the set method. The ‘indexOf’ is used to find the index of an element.

In a List, the operations can be performed according to the position. The programmer can provide the data element that is to be added to the index. So it will be added to the specific index. If the programmer does not give an index, the element will be added to the end of the List. It also maintains the inserted order. If element 1 is added and then element2 is added, then element1 will be before element2.

Difference Between List and Set

Figure 01: List and Set

ArrayList, LinkedList, Vector are some classes that implement List. In an ArrayList, accessing an element is fast but inserting and deleting is lower. ArrayList is not thread-safe. Accessing the same ArrayList from multiple threads might not give the same result. In a LinkedList, the elements are linked to both backward and forward. Inserting and deleting elements using a LinkedList is faster than the ArrayList. The LinkedList implements List and Queue Both. Vector is similar to ArrayList, but it is tread-safe because all the methods are synchronized.

What is Set?

Set is an interface that extends the Collection interface. As Set interface extends Collection, all the methods of Collection are also belongs to Set. A Set does not support duplication values. Therefore, the programmer cannot store the same element twice. It maintains a unique set of elements.  The SortedSet interface extends Set interface. SortedSet maintains the elements in sorted order. The NavigableSet interface extends SortedSet. The NavigableSet provides navigation methods such as lower, floor, ceiling etc.

HashSet, LinkedHashSet, and TreeSet are some classes that implement the Set interface. The HashSet implements Set interface. It does not maintain the inserted order. If the values are inserted as a,x,b it might store as, x,a,b. The LinkedSet maintains the inserted order. If the elements are inserted in a,x,b order, the storing order will be a,x,b.  The TreeSet implements Set and NavigableSet. It does not maintain the order of insertion but stores the elements in the sorted order. If the inserted order is a,c,b, then the elements will be stored as a,b,c. All HashSet, LinkedHashSet and TreeSet will not have any duplicate elements.

What are the Similarities Between List and Set?

  • Both List and Set interfaces extend Collection interface.
  • Both List and Set support operations such as adding, removing elements.

What is the Difference Between List and Set?

List vs Set

List Interface is the sub interface of Collection that contains methods to perform operations such as insert, delete based on the index. Set Interface is a sub interface of Collection that contains methods to perform operations such as  insert, delete elements while maintaining the unique elements.
 Classes
ArrayList, Vector, and LinkedList are classes that implement List interface. HashSet, LinkedHashSet, and TreeSet are classes that implement Set interface.
Element Duplication
List supports duplication of elements. Set does not support duplication of elements. Elements are unique.

Summary – List vs Set

Collections are used to store elements dynamically. Programming languages such as Java provides Collection interface. List and Set are two interfaces which belong to Collection interface. Both interfaces extend Collection. This article discussed the difference between List and Set. The key difference between List and Set is that List supports storing the same element multiple times while Set does not support storing the same element multiple time. Set always maintains unique elements.

Reference:

1.Point, Tutorials. “Java Collections.”, Tutorials Point, 8 Jan. 2018. Available here

Related posts:

Difference Between List and TupleDifference Between List and Tuple Difference Between Randomized and Recursive Algorithm Difference Between Phase and Pass in Compiler Difference Between Subset and Superset Difference Between getc and getchar_Figure 02Difference Between getc and getchar

Filed Under: Programming Tagged With: Compare List and Set, List, List and Set Differences, List and Set Similarities, List Classes, List Definition, List Duplication, List vs Set, set, Set Classes, Set Definition, Set Duplication

About the Author: Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Watch and Warning

Difference Between Watch and Warning

Difference Between Apatosaurus and Brachiosaurus

Difference Between Apatosaurus and Brachiosaurus

Difference Between Cyst and Abscess

Difference Between Blue Nosed Pit and Staffordshire Terrier

Difference Between Freelance and Contract

Latest Posts

  • What is the Difference Between Down Syndrome and Klinefelter Syndrome
  • What is the Difference Between Electron and Beta Particle
  • What is the Difference Between Agarose and Polyacrylamide Gel Electrophoresis
  • What is the Difference Between Organic and Bio Fertilizer
  • What is the Difference Between Distillation and Chromatography
  • What is the Difference Between Concealer and Corrector
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.