【问题标题】:How to use Hashset/set in Jmeter beanshell scripting如何在 Jmeter beanshell 脚本中使用 Hashset/set
【发布时间】:2020-02-11 20:08:53
【问题描述】:

我有一个 ArrayList 中的 ID 列表,谁能帮助我如何从列表中获取唯一 ID 的计数。

假设ArrayList 可能包含:

ADD5C9
AA6F39
AA3D0D
AA48C9
8B9D48
63A859
ADD5C9
ADA162
AD9AD5
8B9D48

please find the code for the list

感谢和最好的问候

【问题讨论】:

    标签: java jmeter beanshell


    【解决方案1】:

    将所有列表元素添加到Set 以删除所有重复值:

    Set<String> set = new HashSet<>(theArrayList);
    int numberOfUniques = set.size();
    

    【讨论】:

      【解决方案2】:

      请注意 since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language 用于编写脚本。

      在 Groovy 中,为 ArrayList 调用 unique() function 就足够了,它会删除所有重复项:

      def array = ['ADD5C9', 
                   'AA6F39',
                   'AA3D0D',
                   'AA48C9',
                   '8B9D48',
                   '63A859',
                   'ADD5C9',
                   'ADA162',
                   'AD9AD5',
                   '8B9D48']
      
      def unique = array.unique()
      
      unique.each { value -> log.info(value) }
      

      更多信息:Apache Groovy - Why and How You Should Use It

      【讨论】:

        【解决方案3】:

        Java-8+ 解决方案

        或者,您可以像这样使用Stream::distinct

        long count = list.stream().distinct().count();
        

        【讨论】:

          猜你喜欢
          • 2017-03-22
          • 2016-06-23
          • 2016-12-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-19
          相关资源
          最近更新 更多