Arjun Reddy wrote:
I want to find if my carrots have one with a black color and retrieve it without writing a for loop.
Not possible.
You'd have to use some other data structure. One approach would be to use a Map whose keys are Colors and whose values are either Carrot (if there can only be one Carrot of a given Color) or some Collection<Carrot>, such as List<Carrot> or Set<Carrot>, if there can be more than one Carrot of the same color. Other more complex options are also available, such as a database or more complex
Java data structure.
Or you could keep the list sorted by Color, and have a separate structure that maps Color to the index at which the given Color starts in your List.