java set example

import java.util.*;
public class SetDemo {

  public static void main(String args[]) { 
      int count[] = {34, 22,10,60,30,22};
      Set<Integer> set = new HashSet<Integer>();
      try {
         for(int i = 0; i < 5; i++) {
            set.add(count[i]);
         }
         System.out.println(set);

         TreeSet sortedSet = new TreeSet<Integer>(set);
         System.out.println("The sorted list is:");
         System.out.println(sortedSet);

         System.out.println("The First element of the set is: "+ (Integer)sortedSet.first());
         System.out.println("The last element of the set is: "+ (Integer)sortedSet.last());
      }
      catch(Exception e) {}
   }
} 

OUTPUT:

              [34, 22, 10, 60, 30]
              The sorted list is:
              [10, 22, 30, 34, 60]
              The First element of the set is: 10
              The last element of the set is: 60

4
1
Awgiedawgie 440220 points

                                    A Set is a Collection that cannot contain duplicate elements.
  
The Set interface contains only methods inherited from Collection 
and adds the restriction that duplicate elements are prohibited.

4 (1 Votes)
0
5
1
Awgiedawgie 440220 points

                                    import java.util.*;
public class SetDemo {

  public static void main(String args[]) { 
      int count[] = {34, 22,10,60,30,22};
      Set&lt;Integer&gt; set = new HashSet&lt;Integer&gt;();
      try {
         for(int i = 0; i &lt; 5; i++) {
            set.add(count[i]);
         }
         System.out.println(set);

         TreeSet sortedSet = new TreeSet&lt;Integer&gt;(set);
         System.out.println(&quot;The sorted list is:&quot;);
         System.out.println(sortedSet);

         System.out.println(&quot;The First element of the set is: &quot;+ (Integer)sortedSet.first());
         System.out.println(&quot;The last element of the set is: &quot;+ (Integer)sortedSet.last());
      }
      catch(Exception e) {}
   }
} 

5 (1 Votes)
0
0
0
Awgiedawgie 440220 points

                                    import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/*
check a set size by set.size()
*/

public class MainClass {
  public static void main(String[] a) {
    String elements[] = { &quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot; };
    Set set = new HashSet(Arrays.asList(elements));

    System.out.println(set.size());
  }
}

0
0
4
8
Awgiedawgie 440220 points

                                    
SET: Can only store unique values, 
     And does not maintain order
- HashSet can have null, order is not guaranteed
- LinkedHashSet can have null and keeps the order 
- TreeSet sorts the order and don't accept null 

4 (8 Votes)
0
3.75
4
Phoenix Logan 186120 points

                                    //Creating HashSet 
HashSet&lt;String&gt; set = new HashSet(); 

//adding elements  
set.add(&quot;One&quot;);    
set.add(&quot;Two&quot;);    

//Removing element);
set.remove(&quot;One&quot;); 

//Removing all the elements available in the set  
set.clear();  

3.75 (4 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
set in java programs how to check how many elements in a set java setsin java how to use set in javas make set in java set function i java how to use sets in java defining set function java set function java how to make a set in ajav set is java java make new set what is a set in java 8 set == java how to use the set method in java using a set in java set function example java set in jva java define set with values declare set in java java set explained set java declare how to create set java How does set work in java how to set values in set in java value set java code of set in java SET JAVA IN how to with set java java is in set create set from elements java A set in java ? how is a set in java set(a,0) meaning in java java work with set declare a new set in java Make a Java set declaring a set in java what is the use of set in java create new set from a set java how tocreate Set in java how to use set java how to initialize set in java how to how to set in java set method java example set set java java.util.set in java how to implement set in java create new set java java creating a set a java set implementing set in java sets methods in java purpose of set in java how to do set in java set in java syntax of set in java what is setS in java how to set value for Set java java create set with value use of set in java How to code a set in java set - set java define set java set java implementation Set implementation java JAVA set&lt; java set of implement set in java create a java set set in java example java whats a set java using set how too use set in java creating set in java declare set java java set() java set this java set using set in java java set \ set jaava set or java .set() in java use set in java where to use set in java java set operations implement a set in java Set javas java util set example set en java create a set in java set in java methods java create set with values java set implementation java set of set java set.of example how to do set in java how to set java how to use set method in java set methods java new set javva why we are using set in java declare a set in java whatis set in java java set(i) java declare set where can we use set in JAVA\ java create set what is a set java how does a set work in java java how to use Set how to manually declare a set in java .set java .set() java set in jaba what does .set do in java define a set in java set javva set of java set operations in java create set with elements in java what is set java set method in java set java util java declare new set implementing a set in to java Set java method java is set set function in java java set methods functions of set in java how to create set i java set s = new set() in java set method java \set java set operations java how i can write a set in java set javaa creating a set java implementing a set in java create set in java create set with values java code for set operations in java new set in java java set syntax set implementation in java what does this set do in java java declare a set making a set in java what is syntax of set in java set for java how to declare set program of set in java how to get an element of a set in java set.add java switch java how to sort a set import java how to make a set how to read into set in java set of strings java is set a collection create a new set java set collection in java java can set hold string as well? can set hold string as well? set java docs collection set in java java define set java sett string of set in java java set of strings how to make set in java with custom equivalent how to make set in java set get java java set contains set interface in java how to access elements of a set in java string util to set definition of a Set&lt;T&gt; s in java new set en java set example java Set&lt;&gt; in java add to set java get value from set in java java set util how to set in set of interger java class Set java set contains java java library set declare set javs display set values in java how to set elements from set in java mplement a set data structure with the following methods: Set in javaa print set in java how to write a set in alted HOW TO READ FROM SET set documentation unordered set in java 3 different options with two data sets java java methods for sets set string example of set java how to use set set ? api java SET IN JACA set java methods set of strings java set(string,&lt; object &gt;) how to get element from set in java java what is a set used for what are set in java create set in java sample what is a navigable set in java java what is a set java set return only element how to make set in java set opearation in java set() in javq java program to implement set java set programs .set method java setcaret in java how to instance a set java set methods in java add set to other set java java setTitle set of unique objects java java set have how to set a value to set in java java set function to element setDigit java setlookandfeel java example how to dispay 2nd element in set i java java for set return set elements in java Set&lt;String&gt; java (); java.util.Set.add(Object)&quot; because set is null set data structure java example java set how to make a Set&lt;&gt; in java how to create a set java how to declare set java setrgb java create a set class in java there is a set method in java Example of Set in JAVA api de java metodo set methods of class Set in java java set collection classes java setter java set example object set of string java java set doc java set collection examples java set collection xample how to define set in java list.set function in java means what set in java does set javac set java string value java sets java collection set types of sets in java java.set java use of all set java use of set doorsnede sets java create set type in java set size jva java import set how to implement a set in java import for sets java set Javavalue set sreing java how to construct a set in java how acess elements in set in java java set class make a set in java get element from set java how to get value from set get values in a set java what is Set in java java set program get all elelemtns in set java Set in Java create how to access element in a a set in jva Set&lt;T&gt; in java how to use sets java java make set create a basic set in java sett java example codes of set&lt;&gt; in java example codes of set in java setter java set data type methods java having a set how to get elements from set java for set string set java sintax set of objects in java using $set example Set &lt; java java set.of() Set jaa java set uses use a set that sets collection use a set collection how to check if something is in a set java how to make a new set in java create a set java hashocede method in java set interface haschocode() method in java set interface java set add everything once java integer set methods set add java hashcode method in java set interface java set.add make new set in java datatablelistview java .set method how to get a set element in java Set javadoc set object into set object to set java java set T set java functions java a set of class a set of integers in java Sets in java java collections set make my class a Set in java hot to use set in java methods in a set in java how to set [] .set in java how to create a set&lt;string&gt; in java how to create a set of strings in java java set code how to declare a set in java sett in java java a set set in javas java set.get Set.java collection return everything as a set java how do sets store objects java java set tutorial how to set api in java set api in java set useful in java how to access an element in a set java set java collection example java set values for set set java class java set methods models java set&lt;string&gt; java how to make set create a set of items java interface set set class java java return and set element sets code java how to make Set class in java how to make Set classes in java how to get set elements in java java set containsorelse java how to run if an element is in a set methods for java set setclass in java set new set jaa how to set new element java set All java example of a set java java set values java set&lt;&gt; how to define a set in java java set set subclasses of Set Java set access element java set in java functions java set add setv in java how to make a set java set java api set functions java java how to create set how to get set element in java set library in java set interface import SET in java get in set java how to get set of sets into new set declaration java java get all elements of set of sets java how to use set collection java how to used set collection java set functions set of objects java create set java java set interface set injava how to use java Set&lt;String&gt; how to add set of numbers using collections in java java check if a set contains a set of strings java how to set -xss set java documentation current set and incoming set java java set of objects set of string in java get elements in set java set declaration in java java set retainall set integer java calss Java Set addAll equals different type of set methods java java set string set syntax java java set collection how to get values from set in java how to get element from a set in java defining a set in java how to make a set in java how to add object into set in java Java String set type of set in java how to create a set in java how to store set in java access elements in set java how to create set in java check if set contains string java string set java storing data in a set java get set element in java new set java get values in set java retrieve values from set in java set constructor java java new set set interface java java checkset java scheckset java create new set sets java set structure in java java .set set code in java set i java import set java how to access set in java set string example set string java set() java what is set in java called set string java example set java example write java code to illustrate the concept of set Write a Java code to illustrate the concept of Set is data type set in java java set method syntax set interface in java what does set do java how to call set object in java where do you set Java retainall java cast set set funtions in java How to use Set in Java Set java java set object Set in java collections Baledung set in java java set predefined methods set in java interface set java java.util.set set functions in java java set api what is a java set set java java util set Set&lt;String&gt; java set like type in java how to declare set in java set in jave Set&lt;T&gt; java java object set method for determining if object contained in set of objects set api java set maem how to enter elements in a set in java with just the limits present methods of set in java what is a set in java types of sets java set java set&lt;&gt; java set methods java gfg java code to convert array to set examples java set using a set java using sets java using set java java set example
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source