Knowledge Walls
John Peter
Pune, Maharashtra, India
How to use Unique list using SET Class in Core Java with Example
1661 Views
UniqueSetCollectionExample
import java.util.HashSet;
import java.util.Set;

public class UniqueSetCollectionExample {
    public static void main(String args[]) throws Exception{    
        Set<String> uniqueUserNames = new HashSet<String>();
            uniqueUserNames.add("Sarathy");
            uniqueUserNames.add("Gopal");
            uniqueUserNames.add("Raja");
            uniqueUserNames.add("Sarathy");
            
            
        System.out.println(uniqueUserNames);
    }
}
Output 
[Sarathy, Raja, Gopal]
Hints 
Set interface assures all the Object taken by the add method keep it unique on the Set List. .add() method is used to add the object to the List. Here in this above example String is the Object to persist on the list.

HashSet<String>() Class provides all the definitions of the set interface.
Best Lessons of "One day One Thing to Know"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details