Tuesday, 23 June 2015

Java8: Generate a Random String in One Line

This is a program which demonstrates the power of Java 8 streaming to generate random strings in Java. It chains the filter, map, limit, and collect to turn random numbers into the required string.


The program will output random strings containing numbers [0-9] and letters [a-z,A-Z].


The numbers in the code map to UniCode characters (for a full Unicode chart see here).

An explanation of the code is as follows:

  1. Generate random numbers within the range 48 (unicode for 0) to 122 (unicode for z).
  2. Only allow numbers less than 57 (the digits 0-9) or greater than 65 and less than 90 (letters A-Z) or great than 97 (the letters A-Z).
  3. Map each number to a char.
  4. Stop when you have the required length of the string. 
  5. Collect the chars produced into a StringBuilder
  6. Turn the StringBuilder in a String and return


8 comments:

  1. For random.ints(48,122) upper bound should be 123, because it exclusive

    ReplyDelete
    Replies
    1. Also (i<57 || i>65) && (i <90 || i>97) should be
      (i < 58 || i > 64) && (i < 91 || i > 96)

      Delete
    2. finally, to avoid of unnecessary boxing and unboxing

      random.ints(48,123)
      .filter(i -> (i < 58) || (i > 64 && i < 91) || (i > 96))
      .limit(length)
      .collect(StringBuilder::new, (sb, i) -> sb.append((char) i), StringBuilder::append).toString());

      Delete
    3. Great Article android based projects

      Java Training in Chennai Project Center in Chennai Java Training in Chennai projects for cse The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training Project Centers in Chennai

      Delete
  2. I found an example of a script for a random number generator on the Internet, but I wanted to study this topic in more

    depth because in many my projects a random series exist and it will help to test the password or won accessed. I advise you to download random number generator java https://explainjava.com/random-number-generator-java/ and read the detailed article because simply copying the code, the effectiveness will of course be, but you'll not develop as a professional behind this.

    ReplyDelete
  3. This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance knowledge. I like it and help me to development very well. Thank you for this brief explanation and very nice information. Well, got good knowledge.
    WordPress website development Chennai

    ReplyDelete
  4. 77%OFF Coach Outlet Store-Coach Bags Clearance Sale,100% Popular
    New & Popular Ray Ban Sunglasses Outlet Store Online,2020-2021 Stylish
    Clearance Coach Store | 80%OFF Coach Bags Outlet On Sale Online
    Share Best Best Adidas Yeezys Store|100% New & Real Yeezy Boost For Sale
    2020 Nike Air For 1 Shoes Outlet For Men & Women, 68-85%OFF Cheap Sale
    Official Michael Kors Outlet Store Online,100% Cheap MK Bags Sale
    61-82%OFF Air Jordan Shoe Stores|Cheap Jordans Releases,Hot Sale

    ReplyDelete