Solved

Comparing ArrayList elements with Hash map Keys

Posted on 2009-07-01
1
2,766 Views
Last Modified: 2013-12-22
I have a arraylist and hash map .
I want wen ever there is matching in arraylst elemtns and hashmap keys
corresponding vales sud be put in a new list.
The values of HashMap is list.
My given codeis failing somewhere between.
Please check..
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
 
public class test5
{
	public ArrayList<String> getData(HashMap<String, ArrayList<String>> AMap,ArrayList<String> temp)
	{
		
		System.out.println(" Invloking getData ");
		Set<String> tempSet = new HashSet<String>(temp);
        System.out.println("getData "+tempSet);
        Set commonKeys = new HashSet(AMap.keySet());
        ArrayList<String> ruleGroupIds = new ArrayList<String>(commonKeys);
        commonKeys.retainAll( tempSet );
 	    System.out.println(commonKeys);
	 	for (Object key : commonKeys) 
	     {
		      ArrayList<String>  value = (ArrayList<String>) AMap.get(key);
	 	      ruleGroupIds.addAll(value);
  	     }
	 	System.out.println("result " + ruleGroupIds);
 	    return ruleGroupIds;
	}
	public static void main(String args[])
	{
		ArrayList<String> compareList = new ArrayList<String>();
		compareList.add("A");	compareList.add("B");	compareList.add("D");
		ArrayList<String> superList = new ArrayList<String>();
		ArrayList<String> ruleGroupIds = new ArrayList<String>();
		ArrayList<String> ruleGroupIdsss = new ArrayList<String>();
		ruleGroupIdsss.add("1234");ruleGroupIdsss.add("12345");
		superList.add("A");superList.add("B");superList.add("C");superList.add("E");//superList.add("D");
 
		HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
		hm.put("B",ruleGroupIdsss);hm.put("D",ruleGroupIdsss);hm.put("A",ruleGroupIdsss);hm.put("E",ruleGroupIdsss);
		hm.put("C",ruleGroupIdsss);
		
		test5 t = new test5();
		t.getData(hm, compareList);
	}
}

Open in new window

0
Comment
Question by:satyabrata25
1 Comment
 
LVL 3

Accepted Solution

by:
serrutom earned 250 total points
ID: 24753291

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
 
public class Test5 {
	public static void main(String args[]) {
		ArrayList<String> listA = new ArrayList<String>();
		listA.add("A");
		listA.add("B");
		listA.add("D");
 
		HashMap<String, String> listB = new HashMap<String, String>();
		listB.put("A", "Test A");
		listB.put("B", "Test B");
		listB.put("C", "Test C");
		listB.put("D", "Test D");
		listB.put("E", "Test E");
 
		Test5 t = new Test5();
 
		List<String> newList = new ArrayList();
		Set<String> keys = listB.keySet();
		for (String key : keys) {
			if(listA.contains(key)) {
				newList.add(listB.get(key));
			}
		}
		
		for(String s : newList) {
			System.out.println(s);
		}
	}
}

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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:

679 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