logo image

How to generate universally unique identifier (UUID) in Java



Java has java.util.UUID utility class using which you can generate universally unique identifier (UUID), following program demonstrate it.

import java.util.UUID;
import java.util.stream.IntStream;

public class GenerateUUID {
	public static void main(String[] args) {
		IntStream.range(0, 10).forEach((uuid) -> {
			System.out.println(UUID.randomUUID());
		});
	}
}

Output:

d311fba9-2442-4c84-ae2c-98a5892d9b90
ef4cbe7b-f758-454d-8898-7c0402a35956
b1946c42-fa9e-4ec1-b5d3-5b6300ed63d0
c92746c1-4baa-4eb9-8d4e-ed77ff1c7eba
6443fd59-8995-4269-bdf9-4495ae7260dd
61677697-e809-413e-8a2f-d08bd2153c2f
8156faa1-0016-421a-a861-a55b28edbe8b
97629e68-2b86-4f8c-a309-bad3d3525fbe
53420274-1d18-4ab1-9fc0-5c79a359c120
b8ab6514-83d9-4b2d-990d-6cdded57b544