Solved

mapping data into a collection or data structure (easy)

Posted on 2004-10-14
12
144 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
  • 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
 

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
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: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

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
groupSumClump challenge 9 103
reasons why a "inside Menu" would not appear for a web server 6 56
base64 decode encode 12 122
simple java question 3 44
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

863 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

26 Experts available now in Live!

Get 1:1 Help Now