Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

Merge lists without duplicates

Options
  • 04-09-2014 9:41pm
    #1
    Closed Accounts Posts: 6,075 ✭✭✭


    I have 2 lists of HotelRoom objects. I want to merge the 2 lists without duplicates. If 2 HotelRooms are equal, I only want to add the cheapest room, ignoring the other.

    2 HotelRooms are equal based on the description.

    Can anyone give me a steer on how to do this?


Comments

  • Registered Users Posts: 306 ✭✭yes there


    Make a new array list. Start filing it from one with the other list nested from the original, check for equality and fiill up the new list or dont. Oh if they are equal just check what is cheapest and add that to the new list.


  • Registered Users Posts: 2,015 ✭✭✭Colonel Panic


    But would that be elegant and conform to design patterns?


  • Closed Accounts Posts: 6,075 ✭✭✭IamtheWalrus


    But would that be elegant and conform to design patterns?

    I'm not overly fussed about elegance. My program is only temporary.


  • Registered Users Posts: 141 ✭✭cdev


    If you're using Java you could try something along these lines...

    - Change the HotelRoom class so that it implements java.lang.Comparable.
    - Collections.sort each list. Then call the commons CollectionUtils.collate with the includeDuplicates parameter set to false.


  • Registered Users Posts: 306 ✭✭yes there


    cdev wrote: »
    If you're using Java you could try something along these lines...

    - Change the HotelRoom class so that it implements java.lang.Comparable.
    - Collections.sort each list. Then call the commons CollectionUtils.collate with the includeDuplicates parameter set to false.

    Finding the cheapest of the duplicates would be an issue then.


  • Advertisement
  • Registered Users Posts: 27,031 ✭✭✭✭GreeBo


    yes there wrote: »
    Finding the cheapest of the duplicates would be an issue then.

    Merge, then sort then filter.


  • Registered Users Posts: 4,757 ✭✭✭cython


    cdev wrote: »
    If you're using Java you could try something along these lines...

    - Change the HotelRoom class so that it implements java.lang.Comparable.
    - Collections.sort each list. Then call the commons CollectionUtils.collate with the includeDuplicates parameter set to false.
    yes there wrote: »
    Finding the cheapest of the duplicates would be an issue then.

    How about implementing Comparable based on the price, merge the lists, and then iterate through the merged list to add them to a Set? A set shouldn't allow duplicates based on .equals(), so if you sort and add them from low to high the more expensive ones should be ignored?


  • Registered Users Posts: 306 ✭✭yes there


    cython wrote: »
    How about implementing Comparable based on the price, merge the lists, and then iterate through the merged list to add them to a Set? A set shouldn't allow duplicates based on .equals(), so if you sort and add them from low to high the more expensive ones should be ignored?

    Yes, hash sets dont allow duplicates but i have had unreolved trouble reading data from hash sets before. That will work in theory


  • Registered Users Posts: 27,031 ✭✭✭✭GreeBo


    yes there wrote: »
    Yes, hash sets dont allow duplicates but i have had unreolved trouble reading data from hash sets before. That will work in theory

    How large are the lists and is the sort being done real-time for an end user?

    Add the longest list to a map, iterate the second list doing a .containsKey against first map.
    If it does contain it do a get, compare prices (via comparable interface) and then overwrite or not based on the result.


  • Closed Accounts Posts: 6,075 ✭✭✭IamtheWalrus


    Anyone know how to find an object in a map when I don't have access to the objects source code to override equals() and hashcode()?


  • Advertisement
  • Registered Users Posts: 2,015 ✭✭✭Colonel Panic


    for item in map
       check if it's the right one
    

    Or if you want to get pointless. Subclass the items and implement those methods?


  • Registered Users Posts: 27,031 ✭✭✭✭GreeBo


    Anyone know how to find an object in a map when I don't have access to the objects source code to override equals() and hashcode()?

    just do a get and implement a method that takes two of the objects and compares them based on whatever fields/values you deem to be important for equals.


  • Registered Users Posts: 84 ✭✭shawnxxiong


    1. Create a hotel object
    2. Create a TreeMap<String, HotelObj>, the key should be hotel id.
    3. Now, before putting the new entry into the tree map, use if(TreeMap.containsKey(key)) to decide if you need to do the comparison of the price. If it the hotel already exists in the map, then get that object by treemap.get(hotel id) to do the comparison.


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    Is it homework season already? Or you changing contracts?


  • Registered Users Posts: 27,031 ✭✭✭✭GreeBo


    1. Create a hotel object
    2. Create a TreeMap<String, HotelObj>, the key should be hotel id.
    3. Now, before putting the new entry into the tree map, use if(TreeMap.containsKey(key)) to decide if you need to do the comparison of the price. If it the hotel already exists in the map, then get that object by treemap.get(hotel id) to do the comparison.

    Why would you use a TreeMap in this case?


  • Registered Users Posts: 84 ✭✭shawnxxiong


    GreeBo wrote: »
    Why would you use a TreeMap in this case?
    sorry, no, treemap shouldn't be used, it should be hashmap or linkedhashmap depending on the further operation on the collection.


Advertisement