Enter your E-mail Address below for Free E-mail Alerts right Into your Inbox: -

Wednesday

What is Set in Java?

What is Set in Java?

Set implements the Collection<E>, Iterable<E> Interfaces.

public interface Set<E>
extends Collection<E>
Set in Java allows to store objects by storing them in each index. Other than Set Set doesn't allows Duplication of elements.To store an element in Set use the add() method.
Example:

package com.jobberindia;

import java.util.*;

public class HelloWorld {

public static void main(String[] args) {

        Set<String> set = new TreeSet<String>();
        set.add("Hello");
        set.add("World");
        set.add("Hello1");
        set.add("World2");
        set.add("Hello3");
        set.add("World4");
        System.out.println("Data stored in mainlist "+set);

}

}



Some Classes implementing List Interface are TreeSet,HashSet etc.

No comments:

Post a Comment