package org.kodejava.util;
import java.util.*;
public class SetToArray {
public static void main(String[] args) {
// Create a java.util.Set object and add some integers into the Set.
Set<Integer> numberSet = new HashSet<>();
numberSet.add(1);
numberSet.add(2);
numberSet.add(3);
numberSet.add(5);
numberSet.add(8);
// Converting a java.util.Set into an array can be done by creating a
// java.util.List object from the Set and then convert it into an array
// by calling the toArray() method on the list object.
List<Integer> numberList = new ArrayList<>(numberSet);
Integer[] numbers = numberList.toArray(new Integer[0]);
// Display the content of numbers array.
for (int i = 0; i < numbers.length; i++) {
Integer number = numbers[i];
System.out.print(number + (i < numbers.length - 1 ? ", " : "\n"));
}
// Display the content of numbers array using the for-each loop.
for (Integer number : numbers) {
System.out.print(number + ", ");
}
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024