简体   繁体   中英

How to generate 1000 unique email-ids using java

My requirement is to generate 1000 unique email-ids in Java. I have already generated random Text and using for loop I'm limiting the number of email-ids to be generated. Problem is when I execute 10 email-ids are generated but all are same.

Below is the code and output:

public static void main() {
    first fr = new first();
    String n = fr.genText()+"@mail.com";

    for (int i = 0; i<=9; i++) {
        System.out.println(n);
    }
}

public String genText() {
    String randomText = "abcdefghijklmnopqrstuvwxyz";
    int length = 4;
    String temp = RandomStringUtils.random(length, randomText);
    return temp;
}

and output is:

myqo@mail.com
myqo@mail.com
...
myqo@mail.com

When I execute the same above program I get another set of mail-ids. Example: instead of 'myqo' it will be 'bfta' . But my requirement is to generate different unique ids.

For Example:

myqo@mail.com
bfta@mail.com
kjuy@mail.com

Put your String initialization in the for statement:

for (int i = 0; i<=9; i++) {
    String n = fr.genText()+"@mail.com";
    System.out.println(n);
}

I would like to rewrite your method a little bit:

public String generateEmail(String domain, int length) {
    return RandomStringUtils.random(length, "abcdefghijklmnopqrstuvwxyz") + "@" + domain;
}

And it would be possible to call like:

generateEmail("gmail.com", 4);

As I understood, you want to generate unique 1000 emails , then you would be able to do this in a convenient way by Stream API:

Stream.generate(() -> generateEmail("gmail.com", 4))
      .limit(1000)
      .collect(Collectors.toSet())

But the problem still exists. I purposely collected a Stream<String> to a Set<String> (which removes duplicates) to find out its size() . As you may see, the size is not always equals 1000

999
1000
997

that means your algorithm returns duplicated values even for such small range.

Therefore, you'd better research already written email generators for Java or improve your own (for example, by adding numbers, some special characters that, in turn, will generate a plenty of exceptions ).

If you are planning to use MockNeat , the feature for implementing email strings is already implemented.

Example 1:

String corpEmail = mock.emails().domain("startup.io").val();
// Possible Output: tiptoplunge@startup.io

Example 2:

String domsEmail = mock.emails().domains("abc.com", "corp.org").val();
// Possible Output: funjulius@corp.org

Note: mock is the default "mocking" object .

To guarantee uniqueness you could use a counter as part of the email address:

myqo0000@mail.com
bfta0001@mail.com
kjuy0002@mail.com

If you want to stick to letters only then convert the counter to base 26 representation using 'a' to 'z' as the digits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM