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 there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Merge lists without duplicates

  • 04-09-2014 8: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, Registered Users 2 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, Registered Users 2 Posts: 2,040 ✭✭✭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, Registered Users 2 Posts: 151 ✭✭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, Registered Users 2 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, Registered Users 2 Posts: 27,370 ✭✭✭✭GreeBo


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

    Merge, then sort then filter.


  • Registered Users, Registered Users 2 Posts: 4,792 ✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 27,370 ✭✭✭✭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, Registered Users 2 Posts: 2,040 ✭✭✭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, Registered Users 2 Posts: 27,370 ✭✭✭✭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, Registered Users 2 Posts: 85 ✭✭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, Registered Users 2 Posts: 2,021 ✭✭✭ChRoMe


    Is it homework season already? Or you changing contracts?


  • Registered Users, Registered Users 2 Posts: 27,370 ✭✭✭✭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, Registered Users 2 Posts: 85 ✭✭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