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

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);
    }
}

 

posted @ 2023-01-07 00:05  李林林  阅读(19)  评论(0编辑  收藏  举报