Question

I'm using BeanShell in JMeter. BeanShell is a Java interpreter that does not support generics. As soon as I enter an angle bracket (e.g. "<") my script will be rejected.

If I could use generics I would do the following:

for ( Map.Entry<String,Object> entry : vars.entrySet() ) {
    System.out.println( "  key = " + entry.getKey() );
}

However I cannot create the variable entry because I'm not permitted to declare Map.Entry<String,Object> in my script.

So - given my method entrySet() and the type it returns of Set<Map.Entry<String,Object>> is there any way I can get a list of (or iterate over) the keys in this set without explicitly referring to a generic type?

e.g. something like:

for ( String keys : vars.entrySet().somethingMagic() ) {
    System.out.println( "  key = " + key );
}
Was it helpful?

Solution

Try either:

for ( Map.Entry entry : vars.entrySet() ) {
    System.out.println( "  key = " + entry.getKey() );
}

Or:

for ( String keys : vars.keySet()) {
    System.out.println( "  key = " + key );
}

OTHER TIPS

Assuming vars is a Map, you can call vars.keySet().

Just use your original code without generics:

for ( Map.Entry entry : vars.entrySet() ) {
    System.out.println( "  key = " + entry.getKey() );
}

Will probably give you a warning, but should still work

Use jsr223 + groovy in external file . This is better for:

  • performance

  • being up to date (you can use JAVA 6 syntax)

...

In upcoming 2.9 , you will be able to have caching by putting it in text area:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top