473,320 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Extract data from a list based on elements in Java

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

Expand|Select|Wrap|Line Numbers
  1. orginalList= [info:"F123",brand:"BMW",dID:3,price:40000, 
  2.                     info:"F100",brand:"BMW",dID:3,price:40000, 
  3.                     info:"F200",brand:"BMW",dID:3,price:40000, 
  4.                     info:"C344",brand:"mercedes",dID:5,price:50000, 
  5.                     info:"C354",brand:"mercedes",dID:5,price:50000, 
  6.                     info:"D355",brand:"Opel",dID:7,price:30000]
  7.  
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:

Expand|Select|Wrap|Line Numbers
  1. newList= [infos:"F123_F100_F200",brand:"BMW",dID:3,price:40000,total:3,                     
  2.                 infos:"C344_C354",brand:"mercedes",dID:5,price:50000,total:2,                    
  3.                 infos:"D355",brand:"Opel",dID:7,price:30000,total:1]
  4.  
  5. I tried some thing like the below but this doesnt give the result I want:
  6.  
  7. List<Export> result = []
  8.         for (Object y : GetUniqueValues(orginalList.dID)){
  9.  
  10.                Export export = new InvoiceExport()
  11.                export.dID = y
  12.                result << export
  13.         }
  14.  
  15.  public static ArrayList GetUniqueValues(Collection values)
  16.     {
  17.         return new ArrayList(new HashSet(values));
  18.     }
Who can help and please with example. Thanks
Feb 20 '16 #1
2 1673
chaarmann
785 Expert 512MB
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.sort 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.
Feb 24 '16 #2
chaarmann
785 Expert 512MB
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.
Feb 24 '16 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Bart Nessux | last post by:
Hi, I am generating a list of file names... some of the files are locked by the OS (Windows XP) and I know the names of these files (NTUSER.DAT, ntuser.dat.LOG, etc.) But, I don't know their...
0
by: Ashok | last post by:
Hi, is it possible to extract data from a web based java applet in order to enter that data in mysql? for example, something that would let me extract the data shown in applet on...
8
by: nick | last post by:
Hi all can any one please tell me what is wrong in this code?? I'm new to deal with text files and extract data. i'm trying to look for data in a text file (3~4 pages) some lines start with a...
1
by: basyarie | last post by:
Hello All, I`d like to introduce myself. I`m basyarie, now is student of university. Nice to meet you all. I`m beginner in this discussion community. Just want to ask about VB6 for GPS...
2
by: missolsr | last post by:
hi, I am using jpcap to capture OLSR topology control (udp) packets. Does anyone know how to extract data (the way ethereal does it) from the olsr packet? There are methods to extract data...
4
by: bootzwiz | last post by:
Hi, I want to extract data between tags. For example i have following code <span class="t_t_12_b_b">Cast</span><span class="t_t_12_b">: <a href="/test/">Mel Gibb</a></span> I want to...
1
by: bibie | last post by:
How to extract data from mssql and then convert it to mysql using VB6.0. How to connect the mssql..I know a little bit of VB6.0 but only create an interface using STANDARD EXE. Someone told me to...
5
by: ElTipo | last post by:
Hello People, I made a data base with secure wizard to provide to users a PID and Passwords. I need to extract data from Crystal Reports 7 in this data base but Crystal Reports send me a message...
5
by: =?Utf-8?B?aWxy?= | last post by:
Hi This is probably fairly simple but I am newish at programming and was wondering if someone can give me some advice on handling the following. I have an array with a large number of elements...
0
by: Ed Dror | last post by:
Hi there, I'm using Wisual Studio 2005 Pro with SQL server 2005 Dev on XP SP2 I have two tables Generic Item table (GN_ID) PK Color Table (Color_ID) PK and GN_ID FK very simple one to many...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.