package org.kodejava.util;
import java.util.TreeSet;
import java.util.SortedSet;
public class FirstSetElement {
public static void main(String[] args) {
SortedSet<String> numbers = new TreeSet<>();
numbers.add("One");
numbers.add("Two");
numbers.add("Three");
numbers.add("Four");
numbers.add("Five");
// SortedSet orders the items it contains. We can get the first
// item from the SortedSet using the first() method. The fist item
// will be "Five".
String firstElement = numbers.first();
System.out.println("firstElement = " + firstElement);
}
}
Category Archives: Util Package
How do I generate UUID / GUID in Java?
UUID / GUID (Universally / Globally Unique Identifier) is frequently use in programming. Some of its usage are for creating random file names, session id in web application, transaction id and for record’s primary keys in database replacing the sequence or auto generated number.
To generate UUID in Java we can use the java.util.UUID class. This class was introduced in JDK 1.5. The UUID.randomUUID() method return a UUID object. To obtain the value of the random string generated we need to call the UUID.toString() method.
We can also get the version and the variant of the UUID using the version() method and variant() method respectively. Let’s see the code snippet below:
package org.kodejava.util;
import java.util.UUID;
public class RandomStringUUID {
public static void main(String[] args) {
// Creating a random UUID (Universally unique identifier).
UUID uuid = UUID.randomUUID();
String randomUUIDString = uuid.toString();
System.out.println("Random UUID String = " + randomUUIDString);
System.out.println("UUID version = " + uuid.version());
System.out.println("UUID variant = " + uuid.variant());
}
}
The result of our program is:
Random UUID String = 87a20cdd-25da-4dc2-b787-68054ec2c5ca
UUID version = 4
UUID variant = 2
How do I add hours, minutes or seconds to a date?
This example shows you how to add or subtract hours, minutes or seconds to a date using the java.util.Calendar object.
package org.kodejava.util;
import java.util.Calendar;
public class DateAddSubtract {
public static void main(String[] args) {
// Gets a calendar using the default time zone and locale. The
// Calendar returned is based on the current time in the default
// time zone with the default locale.
Calendar calendar = Calendar.getInstance();
System.out.println("Original = " + calendar.getTime());
// Subtract 2 hour from the current time
calendar.add(Calendar.HOUR, -2);
// Add 30 minutes to the calendar time
calendar.add(Calendar.MINUTE, 30);
// Add 300 seconds to the calendar time
calendar.add(Calendar.SECOND, 300);
System.out.println("Updated = " + calendar.getTime());
}
}
The output of the code snippet:
Original = Wed Oct 06 19:47:53 CST 2021
Updated = Wed Oct 06 18:22:53 CST 2021
How do I search collection elements?
This code example use the Collections.binarySearch() to search an specified object inside a specified collections. Prior to calling the binarySearch() method we need to sort the elements of the collection. If the object is not sorted according to their natural order the search result will be undefined.
package org.kodejava.util;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Collections;
import java.text.DateFormatSymbols;
public class CollectionSearch {
public static void main(String[] args) {
DateFormatSymbols dfs = new DateFormatSymbols();
LinkedList<String> monthList =
new LinkedList<>(Arrays.asList(dfs.getMonths()));
// Sort the collection elements
Collections.sort(monthList);
System.out.println("Months = " + monthList);
// Get the position of November inside the monthList. It returns a positive
// value if the item found in the monthList.
int index = Collections.binarySearch(monthList, "November");
if (index > 0) {
System.out.println("Found at index = " + index);
System.out.println("Month = " + monthList.get(index));
}
}
}
The output of the code snippet above is below.
Months = [, April, August, December, February, January, July, June, March, May, November, October, September]
Found at index = 10
Month = November
How do I rotate elements of a collection?
This example demonstrate how to rotate the elements of a collection object. We can use the java.util.Collections class and call the rotate() method with the collection to rotate and the distance as the parameters.
package org.kodejava.util;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
public class CollectionRotate {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
// Add some items into the collection
for (int i = 0; i < 25; i++) {
numbers.add(i);
}
// Print the collection items
System.out.println(Arrays.toString(numbers.toArray()));
// Rotates the elements in the collection by the 10.
Collections.rotate(numbers, 10);
// Print the rotated collection items
System.out.println(Arrays.toString(numbers.toArray()));
}
}
Here is the program result:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
[15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
