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: 60A 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.
Also in Java:
- how to pass parameters to xsl file
- remove part of string java
- arrays.aslist.add
- full shutdown windows 10 cmd
- java obtain list string from list object
- java output length of each line in a text document
- bracket balancing program in java
- javafx fill tableview with data
- java find biggest number in array
- how to skip a line in java
- java iterate over a string
- java string replace character at position
- java random numbers in specific range
- java repository sql find not in list
- java hahmap
- how to get a user input in java
- java.lang.ClassCastException: cannot be cast to javax.servlet.Servlet
- how to scan a character in java
- reading string after double in java
- java find if element of list in present in another list
- java function that returns the index of the largest value in an array
- how to do for each in java
- android studio keep device awake
- stream java example