1

Recently I encountered the usage of the method getAt() in Java code. It is used to get the data from a URL (which is sent via GET method by form submit). The URL will be like:

http://192.168.27.55/flight/search?n=airchina

The method was used like name=params.getAt("n"). Then the data was passed to another function by search("n",name). Can any one please brief me how it works?

2 Answers 2

3

getAt() in Groovy has special meaning for collections. It allows one to access elements of the collection using the subscript operator.

Here's the documentation for Map and List: Map#getAt(key) List#getAt(index)

Since it's defined to support some syntactic sugar, you don't really see it ever called directly, since it enables you to instead do something like:

Map foo = [bar: 'baz']
assert foo['bar'] == 'baz'

In your particular case with params, you'd simply use:

params['n']

...to take advantage of getAt(). Alternatively, you could use:

params.n
// or
params.get('n')
Sign up to request clarification or add additional context in comments.

4 Comments

Can u guess what will be stored in the variable 'name' [name=params.getAt("n")] in the above question..?
It probably would have been airchina, although you could simply write a log or use a debugger to figure out what the value is.
Ya. But am confused why they again used "n" also (like search(n,name)) while passing the value to other function. They can simply use like search(name) right..?
Yes. It's difficult to say what the original author's intentions were here.
0
params.n

documentation for params

1 Comment

As far as i know params is a hashmap. in groovy its ok to write either params.n or params.getAt('n') Sry, im no native speaker, maybe i dont get your question right into my head :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.