Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Hashmap inside a Arraylist

Posted on 2012-09-06
7
Medium Priority
?
687 Views
Last Modified: 2012-09-17
hI,

I have a hashmap which has username and password information.
  Map mapDetail = new HashMap();
           mapDetail.put("username",username);
           mapDetail.put("password",password);
HashMap is embedded in a arraylist like below
  List missingList = new ArrayList();
 missingList.add(mapDetail);


I need to read the username and password information to send it to one other java program. How can I achieve it.Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
7 Comments
 
LVL 12

Assisted Solution

by:Sharon Seth
Sharon Seth earned 500 total points
ID: 38372286
1. Get each hashmap from the list - use enhanced for loop
2. Once hashMap is obtained , lookup the username & password and pass it to the other java program
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 38372406
You really need two loops (let the List variable be 'maps'):

        for (Map<String, String> m : maps) {
            for (Map.Entry<String, String> e : m.entrySet()) {
                String uname = e.getKey();
                String pword = e.getValue();
                System.out.printf("uname=%s,pword=%s\n", uname, pword);
            }
        }

Open in new window

0
 
LVL 19

Assisted Solution

by:Ken Butters
Ken Butters earned 500 total points
ID: 38372436
I agree with CEHJ that the Hashmap should probably have a key/value based on userid being the key... but not the author's question wasn't really framed that way...

here is example of reading the data back out of the array/hash based on original design:
package com.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class testHash {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 HashMap<String,String> mapDetail = new HashMap<String,String>();
         String username = "Fred Flintstone";
         String password = "MyPassword";
		 mapDetail.put("username",username);
         mapDetail.put("password",password);
/*         HashMap is embedded in a arraylist like below */
         
         List<HashMap<String,String>> missingList = new ArrayList<HashMap<String,String>>();
         
         missingList.add(mapDetail);


         for(HashMap<String,String> newMapDetail : missingList) {
        	 String myUseridToPass = newMapDetail.get("username");
        	 String myPasswordToPass = newMapDetail.get("password");
        	 
        	 /* now just send myUseridToPass / myPasswordToPass to whatever method you desire
        	  * 
        	  */
         }
        	 
	}

}

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 7

Author Comment

by:gudii9
ID: 38374178
where can I find more of these kind of examples. I need to become more knowledgeable in this area of collection where passing 2D collection object into 1D collection object  and reading back again. please advise
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38374405
My example should be more generic really:
      for (Map<String, String> m : maps) {
            for (Map.Entry<String, String> e : m.entrySet()) {
                String key = e.getKey();
                String val = e.getValue();
                System.out.printf("key=%s,value=%s\n", key, val);
            }
        }

Open in new window

0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 500 total points
ID: 38375291
0
 
LVL 37

Expert Comment

by:zzynx
ID: 38405844
Thanx 4 axxepting
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

577 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