Solved

How can I retrieve data from a collection which is within a map?

Posted on 2004-10-12
11
198 Views
Last Modified: 2010-03-31
This function makes a call to listCustomerbyRef which then returns a map which includes 1) param   2) collection of customer details. i need to access these customer details from the collection and have the correct commands (commented out below at bottom of code) - but i cant retrieve these values

public void testListCustomersByRef() {
            
try{
                  TestableCustomerManager cm = new TestableCustomerManager();
                  Collection cusRef = new ArrayList();
                  Map result;
                  cusRef.add("0000564");
                  
                  result = cm.listCustomersByRef(cusRef,1);

                         //code missing to go here

                  
                  /*      cust.setRef((String) result.get("ref"));
                      cust.setName((String) result.get("name"));
                      cust.setRulesRow((Long) result.get("rules_row"));
                      cust.setLftCode((String) result.get("lftCode"));
                      cust.setLunCode((String) result.get("lunCode"));
                        System.out.println(cust.getName());
               
               
               
                CustContact custContact = new CustContact();
                custContact.setTitle((String) result.get("title"));
                custContact.setForename((String) result.get("forename"));
                custContact.setSurname((String) result.get("surname"));
                custContact.setTelNo((String) result.get("telNo"));
                custContact.setFaxNumber((String) result.get("faxNumber"));
                  System.out.println(custContact.getForename());
               
               
               
                Address address = new Address();
                address.setAddLine1((String) result.get("addLine1"));
                address.setAddLine2((String) result.get("addLine2"));
                address.setAddLine3((String) result.get("addLine3"));
                address.setAddLine4((String) result.get("addLine4"));
                address.setAddLine5((String) result.get("addLine5"));
                address.setPostTown((String) result.get("postTown"));
                address.setPostcode((String) result.get("postcode"));
                address.setCountry((String) result.get("country"));
               
                custContact.setAddress(address);
                //cust.setCustContacts((Set)custContact);
                  
                  System.out.println(cust.getName());
                  System.out.println("Tested listCustomersByRef method");*/
            }
            catch(ReadException e){
                  System.out.println(e.getMessage());
            }
            catch(Exception e){
                  System.out.println(e.getMessage());
            }
      }
0
Comment
Question by:kullyN
  • 6
  • 3
11 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 250 total points
ID: 12286736
>>... returns a map which includes 1) param   2) collection of customer details
What type of collection is that collection? List, Map, Set,...?

First you have to know which is the key (e.g. "coll")

List theCollection = (List) result.get("coll");
theCollection.get(0) gets the first element in that collection
0
 

Author Comment

by:kullyN
ID: 12286809
This is the code from the class which returns the map:


Collection coll = getCollectionFromSql(sQuery, params, true, pageno, true, true);
            HashMap result = new HashMap(2);
            result.put(CommonConstants.PARAM_RESULTS, coll);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12286939
Well, then this is how you retrieve that collection:

Collection coll = (Collection) result.get(CommonConstants.PARAM_RESULTS);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12286960
And afterwards:

Iterator it = coll.iterator();
while (it.hasNext()) {
   ItemType item = (ItemType) it.next();  // ItemType being the type of the items in the collection
}
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.

 
LVL 37

Expert Comment

by:zzynx
ID: 12286994
But Collection is just an interface
These are the classes that implement that interface:
AbstractCollection, AbstractList, AbstractSet, ArrayList, BeanContextServicesSupport, BeanContextSupport, HashSet, LinkedHashSet, LinkedList, TreeSet, Vector

It would be handy if you knew which one it was...
0
 

Author Comment

by:kullyN
ID: 12287480
none of these have worked, sorry
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12287547
>> none of these have worked
What do you get?

Does

    result.containsKey(CommonConstants.PARAM_RESULTS)

return true?

If not, the collection simply isn't available in the map.
0
 

Author Comment

by:kullyN
ID: 12288972
i get null print statements

TestableCustomerManager cm = new TestableCustomerManager();
                  Collection cusRef = new ArrayList();
                  Map result;
                  
                  cusRef.add("0000564");
                  
                  result = cm.listCustomersByRef(cusRef,1);
                  Collection customers = (Collection)result.get(CommonConstants.PARAM_RESULTS);
                  
                  Iterator it = customers.iterator();
                  while (it.hasNext()) {
                     String item = (String) it.next();  // ItemType being the type of the items in the collection
                     System.out.println(item);
                  }
                  
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12290802
What can I say?
If null's are coming out, that means that null's were added.
Time to critically investigate the working of

          result = cm.listCustomersByRef(cusRef,1);
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
thymeleaf natural templating vs JSP 2 65
eclipse shortcuts 9 45
Error trying to install RTMT Win7 5 38
web services creation SOAP vs REST 5 17
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

947 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