简体   繁体   中英

Can I Get A List Of Keys In An entrySet In Java Without Using Generics?

I'm using BeanShell in JMeter. BeanShell is a Java interpreter that does not support generics. As soon as I enter an angle bracket (eg "<") 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?

eg something like:

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

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

假设vars是Map,则可以调用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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM