Sunday, October 14, 2018

How do you generate unique alphanumeric character in java?

Problem statement:
write an algorithm to generate alphanumeric characters using java.

public class GenerateUUID {
    public static void main(String args[]) {
        for (int i = 0; i < 1000000; i++) {
            UUID uuid1 = UUID.randomUUID();
            System.out.println(uuid1);  // 8-4-4-4-12 (36 char/32 + 4 )
        }
    }
}
Output:
889255d3-8179-4fd0-b870-fa7246e7b306
c46cc028-503d-469f-8aac-defe8b80a4df
d0fbf928-2c01-466f-86c6-6bf4c492799c
fa7bbabc-194b-4331-95fc-8676414858d5
dddca4af-83fe-44c4-8e65-c375146414bb
35681f4f-e3a9-4a45-85f0-d7ecccf6c2df
a577f324-e33f-4396-be9b-7b80cfe821de
----------------------------------------------
----------------------------------------------
----------------------------------------------

Note: we must create new instance on every single iteration withing a loop, then only it could be, else single instance will get looped over the multiple times.
*************************************************
public class GenerateUUID {
    public static void main(String args[]) {
        UUID uuid1 = UUID.randomUUID();
        for (int i = 0; i < 1000000; i++) {
            System.out.println(uuid1);  // 8-4-4-4-12 (36 char / 32 + 4 hyphen)
        }
    }
}
Output:
2bb222ac-23c0-488a-83c8-75c5c9bc5cdd
2bb222ac-23c0-488a-83c8-75c5c9bc5cdd
2bb222ac-23c0-488a-83c8-75c5c9bc5cdd
2bb222ac-23c0-488a-83c8-75c5c9bc5cdd
2bb222ac-23c0-488a-83c8-75c5c9bc5cdd
2bb222ac-23c0-488a-83c8-75c5c9bc5cdd
2bb222ac-23c0-488a-83c8-75c5c9bc5cdd
----------------------------------------------------------------
----------------------------------------------------------------
----------------------------------------------------------------

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...