首页 > 其他分享 >005.Function函数式接口(Function函数式接口生成定长随机字符串 ,定长随机字符串生成策略)

005.Function函数式接口(Function函数式接口生成定长随机字符串 ,定长随机字符串生成策略)

时间:2023-01-07 00:23:01浏览次数:43  
标签:Function chars 接口 生成 随机 字符串

1.定长随机字符串生成策略

package com.imooc.lambda;

import java.util.Random;
import java.util.function.Function;

/**
 * Function函数式接口生成定长随机字符串
 */
public class FunctionSample
{
    public static void main(String[] args)
    {
        Function<Integer, String> randomStringFunction = l ->
        {
            String chars = "qwertyuiopasdfghjklzxcvbnm7894561230";
            StringBuffer stringBuffer = new StringBuffer();
            Random random = new Random();
            for (int i = 0; i < l; i++)
            {
                int position = random.nextInt(chars.length());
                stringBuffer.append(chars.charAt(position));
            }
            return stringBuffer.toString();
        };
        String apply = randomStringFunction.apply(16);
        System.out.println(apply);
    }
}

 

标签:Function,chars,接口,生成,随机,字符串
From: https://www.cnblogs.com/LLL0617/p/17031983.html

相关文章