Link to home
Start Free TrialLog in
Avatar of Rose_Taylor
Rose_TaylorFlag for United States of America

asked on

How to find the values from List based on another id ?

Hi,
I have list result set and have to find out the list of values based on another id.
For example : Fields attibutes are:productcode , descr , itemcode..etc
and one productcode has multiple itemcodes so i need to find list of itemcode which assoicated productcode.

Iterator<SearchResult> iterator = partitems.iterator();
while(iterator.hasNext())
{
SearchResult Sr = (SearchResult)iterator.next();
System.out.println(sr.getprdcode());            
}

How to find itemcodes which assoicated product code using same result set ?
or do need to do again query to get the result ?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can only extract the details you need out of a query that has been done using a join. From that you need to build a List of beans that itself encapsulates a Collection
Avatar of Rose_Taylor

ASKER

In my result set i have all the values , but when displaying i need to display one productcode with multiple itemscode.

Example :
productcode : 102
itemcode : 12 , 41 , 52 , 63

These four itemcode associated with productcode 102. These values are same row in the database . I have 1000's of productcode with multiple item codes.When displaying productcode need to display itemcode as well.

Expected output :
product code : 102
itemcode : 12,41,52,63

productcode : 105
itemcode : 11 ,78 , 1 ,7 ,9 ,20

like above...
You're saying the ResultSet comes out like
102,12,41,52,63

Open in new window

and not like
102,12
102,41
102,52
102,63

Open in new window

?
Please find below SQL return values :

product   itemcode  Desc
6799        8205        OR
6799        8206        OR
6799        8208        OR
6799        8422        OR
6799        8424        OR
6799        1381        OR
6799        1485        OR
6799        1563        OR

Expected output :
8205 OR 8206 OR 8208 OR8422 OR as like
OK. I would

a. Make a Product bean
b. Make an ItemInfo bean with attributes of at least 'code' and 'description'
c. Give Product a List<ItemInfo>
d. Give it an addItemInfo method
e. Create a Set<Product>

The algo should go something like

a. read the row
b. if Product(id) not in the Set<Product>, create it, else get() it
c. For the rest of the row call
product.addItemInfo(new ItemInfo(rs.getString("itemcode"), rs.getString("description")));

Open in new window

Yes, your correct.

using below code , i can able to get all the values.
Iterator<SearchResult> iterator = partitems.iterator();
while(iterator.hasNext())
{
SearchResult Sr = (SearchResult)iterator.next();
sr.getprdcode());            
sr.getitecode();
//how to merge the two fields values in single row as like column3  ?
}


column1   column2    column3  
6799         item1         8205 OR 8206 OR 8208 OR8422
6799         item2         8205 OR 8206 OR 8208 OR8422
6799         item3         8205 OR 8206 OR 8208 OR8422
6799         item4         8205 OR 8206 OR 8208 OR8422
6799         item5         8205 OR 8206 OR 8208 OR8422
Sorry - i'm confused - you seem to have repeated the question, which i've answered ...
Iterator<Search> iterator = products.iterator();
MultiHashMap lists = new MultiHashMap();
StringBuilder sb = new StringBuilder();
List list = null;

while(iterator.hasNext())
{
    Search req = (Search)iterator.next();
    String product = req.getProduct();
    sb.append(req.getpartitem()); //appending multiple items in the string
    sb.append(" ");
    sb.append(req.getdesc()));
    sb.append(" ");
    lists.put(product, sb.toString());
   sb.setlength(0);
    req.getProduct();
    req.getDescr();
    req.getreqspartno();
    req.getreqproddescr();          
}

Set set = dependencies.entrySet();
Iterator i = set.iterator();

while(i.hasNext())
{
     Map.Entry me = (Map.Entry)i.next();
     list=(List)lists.get(me.getKey());
     for(int j=0;j<list.size();j++)
     {
        list.get(j).toString()));
     }
}

How to split the values ?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good