Solved

Comparing ArrayList elements with Hash map Keys

Posted on 2009-07-01
1
2,850 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
throw exception 21 78
Running JavaFX on JDeveloper 12C 1 112
batch vs regular insert in spring DAO 2 50
Where to place postgres JDBC driver jar on tomcat 8 70
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

734 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