Extract data from a list based on elements in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mesut1
    New Member
    • Feb 2016
    • 1

    Extract data from a list based on elements in Java

    I have a list see below (orginalList), this coming from an oracle database, and contains elements

    Code:
    orginalList= [info:"F123",brand:"BMW",dID:3,price:40000, 
                        info:"F100",brand:"BMW",dID:3,price:40000, 
                        info:"F200",brand:"BMW",dID:3,price:40000, 
                        info:"C344",brand:"mercedes",dID:5,price:50000, 
                        info:"C354",brand:"mercedes",dID:5,price:50000, 
                        info:"D355",brand:"Opel",dID:7,price:30000]
    I would to filter this list and put the result into a new list like the list below (newList) as you can see, the BMW showing three times, mercedes two times, Opel one time and have the same dID, so I would like to put BMW into one row on the basis of dID , and one row for two mercedes and one row for the Opel. and the count the total, like below:

    Code:
    newList= [infos:"F123_F100_F200",brand:"BMW",dID:3,price:40000,total:3,                     
                    infos:"C344_C354",brand:"mercedes",dID:5,price:50000,total:2,                    
                    infos:"D355",brand:"Opel",dID:7,price:30000,total:1]
    
    I tried some thing like the below but this doesnt give the result I want:
    
    List<Export> result = []
            for (Object y : GetUniqueValues(orginalList.dID)){
    
                   Export export = new InvoiceExport()
                   export.dID = y
                   result << export
            }
    
     public static ArrayList GetUniqueValues(Collection values)
        {
            return new ArrayList(new HashSet(values));
        }
    Who can help and please with example. Thanks
    Last edited by Rabbit; Feb 20 '16, 04:24 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Make a class "Item" that contains setters and getters for brand, price, dID and info.

    Then make an arrayList of these items.

    Then use Collections.sor t on this arrayList to sort by dID

    As a next step, use a for-loop with the list iterator to go through the list. If the new dID is different from the old one, create a new result element and copy all infos (properties) into it. Then put it into a result list. if the dID is like the old one, just append the new properties to the old ones.

    Then you are done, just print out the result list.

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      There is a more efficient way:
      Create an empty result map that has the dID as key and an item as value.
      Then read through the original data and for each entry decide: if you can find the dId already in the map, append the new data to the item there. If you can't find the dId in the map, create a new item, copy all the data into this item and put this item into the map.

      Comment

      Working...