Answers for "set in java"

3

java how to make set

//Creating HashSet 
HashSet<String> set = new HashSet(); 

//adding elements  
set.add("One");    
set.add("Two");    

//Removing element);
set.remove("One"); 

//Removing all the elements available in the set  
set.clear();
Posted by: Guest on October-26-2020
8

set java

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
Posted by: Guest on May-31-2020
1

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) {}
   }
}
Posted by: Guest on March-15-2020
9

set java

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.
Posted by: Guest on May-31-2020
2

set in java

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
Posted by: Guest on January-06-2021
0

java set operations

s1.containsAll(s2) — returns true if s2 is a subset of s1. (s2 is a subset of s1 if set s1 contains all of the elements in s2.)
s1.addAll(s2) — transforms s1 into the union of s1 and s2. (The union of two sets is the set containing all of the elements contained in either set.)
s1.retainAll(s2) — transforms s1 into the intersection of s1 and s2. (The intersection of two sets is the set containing only the elements common to both sets.)
s1.removeAll(s2) — transforms s1 into the (asymmetric) set difference of s1 and s2. (For example, the set difference of s1 minus s2 is the set containing all of the elements found in s1 but not in s2.)
Posted by: Guest on April-22-2021

Browse Popular Code Answers by Language