How To Sort Set Java
Sort a Set in Java
- Use a Listing to Display a Sorted Prepare in Java
- Use the
TreeSet
to Display a Sorted Set in Java - Employ the
stream()
Function to Display a Sorted Set in Java
In Java, sets or HashSet
are ordinarily used to randomly access elements, as hash table elements are accessed using hash codes. Hash lawmaking is a unique identity that helps u.s.a. to identify the elements of the hash tabular array.
A HashSet
is an unordered collection. A ready in Coffee does not have random access methods (similar go(i)
where i is an index of that element), which are essential requirements past sorting algorithms. Putting simply, as HashSet
has its elements randomly placed, it does not offer any guarantee to order its elements due to undefined club.
Therefore, we need to call up a lilliputian out of the box to sort a set in Java. We will talk over a few methods to catechumen information technology to a dissimilar structure and sort information technology.
Employ a Listing to Display a Sorted Gear up in Java
A style to sort a HashSet
is by first converting it to a list and then sorting information technology.
We will add the elements from the prepare to the listing and then use the sort()
function to sort information technology.
For instance,
import java.util.*; public form instance{ public static void main(String[] args) { HashSet<Integer> number = new HashSet<>(); // Using add() method number.add(3); number.add together(6); number.add together(4); // converting HashSet to arraylist ArrayList<Integer> al = new ArrayList<>(number); // sorting the listing so press Collections.sort(al); System.out.println("Sorted list: "); for(int x: al) { System.out.print(ten + " "); } } }
Output:
Sorted list: three four 6
Use the TreeSet
to Display a Sorted Set in Java
TreeSet
class of the Collections
interface provides usa with the functionality of tree data construction. We can convert the HashSet
to TreeSet
when nosotros iterate through this collection. The elements are extracted in a well-defined order.
TreeSet
implements NavigableSet
interface, which extends SortedSet
, which farther extends the Set
interface.
See the following case.
import coffee.util.*; public form instance{ public static void principal(String[] args) { HashSet<Integer> number = new HashSet<>(); // Using add() method number.add(iii); number.add(half dozen); number.add together(four); // TreeSet gets the value of hashSet TreeSet myTreeSet = new TreeSet(); myTreeSet.addAll(number); System.out.println(myTreeSet); } }
Output:
[3, 4, half-dozen]
Use the stream()
Function to Display a Sorted Set in Java
There is a concise mode to this problem using the stream()
method. The stream API was introduced in Coffee 8 and is not a data structure in itself. Notwithstanding, it tin take objects from different collections and display them in the desired style based on pipeline methods.
Nosotros volition use the sorted()
method to display the final sequence in a sorted manner for our example.
See the lawmaking below.
import java.util.*; public class example { public static void main(Cord []args){ // creating hashset hs HashSet<Integer> hs = new HashSet<>(); // Using add together() method to add elements to hs hs.add(20); hs.add(iv); hs.add together(fifteen); // before sorting hs hs.forEach(System.out::println); System.out.println(); // extra line // after sorting hs hs.stream().sorted().forEach(Organisation.out::println); // yes } }
Output:
xx 4 15 iv 15 twenty
Write for us
DelftStack articles are written by software geeks like you lot. If you besides would like to contribute to DelftStack by writing paid articles, you lot tin check the write for united states folio.
Related Commodity - Java Set

Source: https://www.delftstack.com/howto/java/sort-a-set-in-java/
0 Response to "How To Sort Set Java"
Post a Comment