4/17/19

Set | Java Collections Framework (JCF) Part -7


Set is simply put, it's a collection
Normally set is defined by curly braces "{}"

Example:
{index,middle,ring,pinky,thum} - Hand Fingers types
{1,12,33,43}

It will remove duplicate elements and retain unique element.

We can assign only distinct or unique element as set element


Set Type
-HashSet
-TreeSet

Set has mainly two methods
-add() - add the elements
-get() - get the elements

Map VS Set

Set
data is called Elements
one element

Map
data is called key-value pair
key and value (all values based on keys)


Common things Set and Map
-Does not allow duplicate elements




Example of Set Java Code..


//import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class SetExample {

    public static void main(String[] args) {

        // Create HashSet | TreeSet
        //HashSet is not considered the order
        //TreeSet is considered the order
        //Set set =new HashSet();
        Set set =new TreeSet();

        set.add("MonDay");
        set.add("TuesDay");
        set.add("WednesDay");
        set.add("ThursDay");
        set.add("FriDay");
        set.add("SaturDay");
        set.add("SunDay");

        Iterator i = set.iterator();

        while (i.hasNext()){
            System.out.println(i.next());
        }


    }

}

No comments:

Post a Comment

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *