Solved

mapping data into a collection or data structure (easy)

Posted on 2004-10-14
12
146 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
map related example 6 55
tomcat not starting 6 68
iterator/ListIterator approach 17 39
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO) 1 35
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

856 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