Monday, 12 March 2018

List Vs Set in Java

We have Set and List interfaces in Java and we have implemented class like ArrayList and HashSet in Java Collections framework.

It is one of most conman and important java interview question and it is important fundamental concept to remember while dealing with collection.

We will see some of the differences in between set and list

1. Duplicates: This is basic and primary difference between set and List.  List allows duplicates where as Set does not allow Duplicates. Why because Set overrides/replace  the value if we insert duplicates.

2. Sorting Order : List is an ordered collection and will follow insertion order i.e  elements add first will get lower index value than later added. Set does not maintain any order. However we have Sorted set interface which follow some order based on compactor/comparable. Here set uses equals() method to check uniqueness for an element stored in set and SortedSet uses compare() method to sort the elements. Hence equals() and compare()  must be consistent each other in order to maintain proper behavior of elements in set and sorted set.

3. Arraylist is the popular implementation of List interface and HashSet and TreeSet is the popular implementation of Set interface.

When to use which collection: Based on above mentioned differences, we can go for List if we need insertion order and if our collection can contain duplicates or   we can go for set if our collection should have unique elements.

package com.sample.java.testing;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.mail.internet.NewsAddress;

public class ListVsSetDemo {

    public static void main(String[] args) {
      
        System.out.println("list example.............");
        List<String> courses = new ArrayList<>();
        courses.add("java");
        courses.add("j2ee");
        courses.add("spring");
        courses.add("servlets");
        courses.add("java");//allow duplicate and print it
      
        for(String course: courses){
            System.out.println(course);//insertion order
        }
      
        System.out.println("set example.............");
        Set<String> courses2 = new HashSet<>();
        courses2.add("java");
        courses2.add("j2ee");
        courses2.add("spring");
        courses2.add("servlets");
        courses2.add("java");// it will allow but it override previous value with this value
      
        for(String course: courses2){
            System.out.println(course);// no insertion order
        }
      
        System.out.println("treeset example.............");
        Set<String> courses3 = new TreeSet<>();
        courses3.add("java");
        courses3.add("j2ee");
        courses3.add("spring");
        courses3.add("jdbc");
        courses3.add("servlets");
        courses3.add("java");// it will allow but it override previous value with this value
      
        for(String course: courses3){
            System.out.println(course);// follow ascending insertion order
        }
      
        System.out.println("get list from set ans sort example.............");
        List<String> setList = new ArrayList<>(courses);
        Collections.sort(setList);
      
        for(String course: courses3){
            System.out.println(course);// follow ascending insertion order
        }

    }

}

Output:
list example.............
java
j2ee
spring
servlets
java
set example.............
servlets
j2ee
java
spring
treeset example.............
j2ee
java
jdbc
servlets
spring
get list from set ans sort example.............
j2ee
java
jdbc
servlets
spring

No comments:

Post a Comment