【问题标题】:How to create a list of random uuid with specific size of elements in groovy如何在groovy中创建具有特定元素大小的随机uuid列表
【发布时间】:2020-10-12 12:56:41
【问题描述】:

说,我想快速创建一个包含 1000 个随机 UUID 的列表。实现这一目标的最佳方法是什么?

我在 Java 中查看了等效的一段代码: How to create a list with specific size of elements

尝试过的代码

List<String> generateValidations(final int count) {
        return Stream.generate(UUID.randomUUID().toString())
                .limit(count)
                .collect(Collectors.toList())
    }

但出现错误:

groovy.lang.MissingPropertyException: No such property: Stream for class: com.test.rds.specifications.ExecuteValidationsSpecification

【问题讨论】:

  • 缺少导入java.util.stream.Streamgenerate 接受一个函数。例如。 java.util.stream.Stream.generate({UUID.randomUUID().toString()}).limit(count).collect()
  • java.util.stream.* 在 Groovy 中不会自动导入

标签: performance groovy collections


【解决方案1】:

或者,没有流...

def listOfUuids = (1..1000).collect { UUID.randomUUID().toString() }

【讨论】:

    【解决方案2】:

    这行得通:

    Stream.generate(UUID::randomUUID).limit(count).collect(Collectors.toList())

    【讨论】:

      猜你喜欢
      • 2012-01-06
      • 2021-05-20
      • 1970-01-01
      • 2018-03-16
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 2018-02-12
      相关资源
      最近更新 更多