• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I get an object in an arraylist of certain value without loping it  RSS feed

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





ps: I love carrots

Thanks
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't you override equals() (and hashCode()) for the Carrot class to define two carrots with the same color as being equal then use

I'm not saying it's a robust solution...and it does use a loop; it's just not a loop the user has to be aware of because it's done within the ArrayList.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tina Smith wrote:Couldn't you override equals() (and hashCode()) for the Carrot class to define two carrots with the same color as being equal then use


Ah, perhaps I misread. I thought he wanted to find all Carrots of a given color, not just find whether one exists or not.

I'm not saying it's a robust solution...and it does use a loop; it's just not a loop the user has to be aware of because it's done within the ArrayList.


Which brings up another point: @OP: WHY do you want to avoid looping?

  • If it's because you don't want to have to write the extra code, it's only a few lines, and if you're doing it more than once, that's what methods are for.


  • If it's because you think that calling a built-in API method is faster than looping, it's not. As Tina points out, the contains() method loops. There's nothing magical about it though. It's no different than any code you would write to do the same thing (assuming you're moderately skilled). The fact that it's part of the core API doesn't make it better or faster. (But if it fits your requirements, it makes more sense to use an existing method than to re-invent the wheel.)


  • If it's because you think looping is slow, well, that can be a valid point, but it needs more than just "it's slow." Finding something in a Collection by iteration is an O(n) operation. If you do it infrequently enough or your Collection is small enough (under 1,000 items, depending), that might be fine. If the is bigger and you're searching for a lot of items in rapid succession, then you might need to employ one of the suggestions I gave previously.
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!