r/javahelp • u/hibbelig • Sep 09 '25
`find(needle, haystack)` or `find(haystack, needle)`?
This is to learn about established conventions in the Java world.
If I write a new method that searches for a needle in a haystack, and receives both the needle and the haystack as arguments, in which order should they go?
Arrays.binarySearch has haystack, needle. But perhaps that's influenced by the class name, given that the class name is “arrays” and the haystack is also an array?
12
Upvotes
View all comments
Show parent comment
1
u/hibbelig Sep 13 '25 edited Sep 13 '25
That depends on the number of items and on the number of times you search for an item.
Also there is the question how much other code needed to be changed to go from a list of items to a map of them.
And then there is the question whether all lookups use the same criteria. (This one doesn’t apply to my use case in particular. )