如何在 Jmeter beanshell 脚本中使用 Hashset/set
How to use Hashset/set in Jmeter beanshell scripting
我在 ArrayList 中有 ID 列表,任何人都可以帮助我如何从列表中获取唯一 ID 的计数。
假设 ArrayList
可能包含:
ADD5C9
AA6F39
AA3D0D
AA48C9
8B9D48
63A859
ADD5C9
ADA162
AD9AD5
8B9D48
please find the code for the list
谢谢和最诚挚的问候
将所有列表元素添加到 Set 以删除任何重复值:
Set<String> set = new HashSet<>(theArrayList);
int numberOfUniques = set.size();
Java-8+解
或者,您可以像这样使用 Stream::distinct
:
long count = list.stream().distinct().count();
请注意 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) }
我在 ArrayList 中有 ID 列表,任何人都可以帮助我如何从列表中获取唯一 ID 的计数。
假设 ArrayList
可能包含:
ADD5C9
AA6F39
AA3D0D
AA48C9
8B9D48
63A859
ADD5C9
ADA162
AD9AD5
8B9D48
please find the code for the list
谢谢和最诚挚的问候
将所有列表元素添加到 Set 以删除任何重复值:
Set<String> set = new HashSet<>(theArrayList);
int numberOfUniques = set.size();
Java-8+解
或者,您可以像这样使用 Stream::distinct
:
long count = list.stream().distinct().count();
请注意 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) }