• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

searching object having certain attribute from arraylist

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Experts ! I'm working on shopping-cart and trying to write a function to search a specific product based on its "p_id" field.

So please help writing Product bean and that method to search it from arraylist of an object....
I'm listing my code below....

Product bean


ProductDAO


I have some idea....

1. SHould I implement Comparator interface....

2. Should I use arraylist.binarySearch()

Or Something else....?

Please help me guys....
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bhavesh Sangwan wrote:Hi Experts ! I'm working on shopping-cart and trying to write a function to search a specific product based on its "p_id" field.
...
I have some idea....
1. SHould I implement Comparator interface....


Sounds reasonable to me. Alternatively, you could implement Comparable based on its p_id field; but my slight preference would be the Comparator.

2. Should I use arraylist.binarySearch()


If you can sort your List appropriately, absolutely.

Or Something else....?


Sounds like you're on the right track to me; especially if you plan on using Lists.
However, another possibility is to put your products into a HashMap<Integer, Product>, which may well be quicker for searches.

HIH

Winston
 
Bhavesh Sangwan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Winston...!

You mean I should collect my product object into HashMap instead of ArrayList for better performance..... Am I Right...? If it is, then I have to store objects into cart in form of HashMap... hmm..? and also I have to return a HashMap from any function returning my product information when needed. So... let me be ready for this stuff.. And also you please keep looking on this post....

Thanks Again...!

OK I'm going to implement using HashMap
 
Bhavesh Sangwan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir ! According to my knowledge, Comparator and Comparable interface helps us in SORTING an object from any collection...

So please clear me that how they can help us in ***"SEARCHING"*** an object from any collection....... ?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once a list (or array, or whatever) has been sorted, we can use a binary search to quickly find an element in that list--much more quickly than a simple linear search.
 
Bhavesh Sangwan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK then for each search - first sort arraylist and then apply binarysearch method on that sorted list.....Right ?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bhavesh Sangwan wrote:OK then for each search - first sort arraylist and then apply binarysearch method on that sorted list.....Right ?


Right.

Sir ! According to my knowledge, Comparator and Comparable interface helps us in SORTING an object from any collection...


That's one thing they're used for.

Specifically, what they do is to define an order for a set of objects; now that could be used to sort a List, but it can also be used to define the order for datasets that maintain their objects in a specific sequence (eg, a TreeSet). Hashed collections like HashSet and HashMap work differently: they are only interested in whether objects are equal() or not.

So: if you're only interested in searching, chances are a HashMap is what you want; if you also need to have your objects in a specific order, then you're going to need Comparable or Comparator.

Winston

BTW: Please don't put lots of bolding in your posts because it comes across as shouting!!. I suggest you read the KeepItDown (←click) page.
Thanks.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read about object ordering here. If you need to keep the order in your list unchanged, you will have to use linear search, which Lists already support, or copy the List and sort it. Remember sorting runs in O(nlogn) time at best, and searching in O(n) time unless you do something daft.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If you need to keep the order in your list unchanged, you will have to use linear search, which Lists already support, or copy the List and sort it.


<nitpick>
Actually, you don't necessarily have to. LinkedHashSet and LinkedHashMap will keep objects//keys in entry order as well; they just don't allow duplicates.
</nitpick>

Winston
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But he said he was searching a List.

I do not believe that LinkedHashSet is an entity, I believe it is a combination of a List and a Set. But that is only my own eccentric opionion
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:But he said he was searching a List.


Oh, OK. I guess we were talking at cross-purposes.

I do not believe that LinkedHashSet is an entity, I believe it is a combination of a List and a Set. But that is only my own eccentric opionion


And I suspect that, like a few other Sets, it's actually a wrapped LinkedHashMap<E, Boolean> in disguise. I should take a look some time...

I have to admit that LHM is a darn useful dataset to know about; I wonder why it isn't taught about more?

Winston
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . it's actually a wrapped LinkedHashMap<E, Boolean> in disguise.

Quite probably. Most of the other collections seem to be a hash map in disguise, so why change the habits of a lifetime

Actually more likely <E, Object>, using a dummy Object object, like an ordinary hash set.

. . . I wonder why it isn't taught about more?

Winston

I don't know what they teach our UGs about collections here, but I was only taught about ArrayList for my MSc. Any other Collections, I had to teach myself. I think they do actually teach the UGs about the collections framework now.
 
This tiny ad is wafer thin:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic