Solved

mapping data into a collection or data structure (easy)

Posted on 2004-10-14
12
143 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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…
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:

707 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

14 Experts available now in Live!

Get 1:1 Help Now