Solved

mapping data into a collection or data structure (easy)

Posted on 2004-10-14
12
148 Views
Last Modified: 2010-03-31
i have data like below:

licNo                 cusRef          cusName
1                         "005"             "gdgd"
2                         "006"             "llkm"
3                          "007"               "lkjlk"

i need to map this data into a structure where i can access each element data through the licNo
0
Comment
Question by:kullyN
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
12 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 12306798
Map m = new HashMap();
CustomerData cd = new CustomerData("005", "gdgd", 1);
m.put(cd.getLicenceNumber(), cd);


...


CustomerData x = (CustomerData)m.get(new Integer(1));
// 'x' now holds 'cd'




............


class CustomerData {
      private String reference;
      private String name;
      private Integer licenceNumber;
      
      public CustomerData(String reference, String name, int licenceNumber) {
            this.reference = reference;
            this.name = name;
            this.licenceNumber = new Integer(licenceNumber);
      }

      public void setLicenceNumber(Integer licenceNumber) {
            this.licenceNumber = licenceNumber;
      }

      public Integer getLicenceNumber() {
            return licenceNumber;
      }


      public void setReference(String reference) {
            this.reference = reference;
      }

      public String getReference() {
            return reference;
      }

      public void setName(String name) {
            this.name = name;
      }

      public String getName() {
            return name;
      }

}
0
 

Author Comment

by:kullyN
ID: 12306907
I have a java class Customer already but this does not include the licNo attribute - the LicNo belongs to Licences class (table in  DB)
In the Object Relational Mapping the Licences are linked to the Customer through the attribute cusRef

im making a copy of data from the DB and then testing a function which retrieves customer via parsing in the licNo.

thus i need this testData structure to check the values retrived from the test
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12306941
OK - just omit the key from the class then. My code will still work - it's just that you won't hold the licNo in the class itself
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kullyN
ID: 12307117
how will i be able to map the licNo to the newly created Customer in the map.

at runtime i will run a test which will retrun a set of customer with licNo's
i then need to do a quick check to see which of the licNo match with the data i have and then from there retrieve the results map linked to the licNo

does this make sense?
0
 

Author Comment

by:kullyN
ID: 12307128
what i have so far

                     Map testData = new HashMap();
                Address add1 = new Address(80005023, "UK");
                Customer cus1 = new Customer("0044234", "academics", 9905504, "N", "CNV", add1, null);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12307207
Not sure what you're posting now kullyN - that's just creating two different objects that don't seem to be related
0
 

Author Comment

by:kullyN
ID: 12307257
is this any better?                  

  Map testData = new HashMap();
               
                Object[] cus1 = {"81","0044234", "academics", "9905504", "N", "CNV", null, null};      // customer 1
                Object[] cus2 = {"1","0044234", "academics", "9905504", "N", "CNV", null, null};        // customer 2
               
                Collection coll = new ArrayList();
                coll.add(cus1);
                coll.add(cus2);
               
                testData.put(CommonConstants.PARAM_RESULTS, coll);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12310889
Your customer should be represented by its own abstraction (class)
0
 
LVL 92

Expert Comment

by:objects
ID: 12313557
 testData.put(cus1licNo, cus1);
  testData.put(cus2licNo, cus2);
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
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:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

739 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