Comparing ArrayList elements with Hash map Keys

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 );
	 	for (Object key : commonKeys) 
		      ArrayList<String>  value = (ArrayList<String>) AMap.get(key);
	 	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>();
		HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
		test5 t = new test5();
		t.getData(hm, compareList);

Open in new window

Who is Participating?
serrutomConnect With a Mentor Commented:

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>();
		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)) {
		for(String s : newList) {

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.