Solved

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

Posted on 2013-05-29
10
304 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 68
GUI builder for Eclipse? 8 27
stackato and cloud 4 65
Apps blocked by Java 9 59
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

914 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now