Solved

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

Posted on 2013-05-29
10
306 Views
Last Modified: 2013-05-31
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 ?
0
Comment
Question by:Rose_Taylor
  • 5
  • 5
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39205829
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
0
 

Author Comment

by:Rose_Taylor
ID: 39205934
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...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39206304
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

?
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:Rose_Taylor
ID: 39206333
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39206352
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

0
 

Author Comment

by:Rose_Taylor
ID: 39206366
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39206966
Sorry - i'm confused - you seem to have repeated the question, which i've answered ...
0
 

Author Comment

by:Rose_Taylor
ID: 39209395
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 ?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39210026
I think i've now answered this elsewhere
0
 

Author Closing Comment

by:Rose_Taylor
ID: 39211474
Good
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question