satyabrata25
asked on
Arraylist elements Compare with HashMap keys
ArrayList<String> superList = new ArrayList<String>();
superList.add("A");superLi
superList.add("C");superLi
HashMap hm = new HashMap();
hm.put("B","1111");
hm.put("D","1112");
hm.put("A","1113");
hm.put("E","1114");
I have to copmapre superList elements with the keys of HashMap hm where the matching key is there
there i have to pull the corresponding value of it and put it in a List.
Like
ArrayList al = new ArrayList();
al sud contain {1113,1111,1114,1112}
ArrayList<String> superList = new ArrayList<String>();
superList.add("A");superList.add("B");
superList.add("C");superList.add("E");superList.add("D");
HashMap hm = new HashMap();
hm.put("B","1111");
hm.put("D","1112");
hm.put("A","1113");
hm.put("E","1114");
I have to copmapre superList elements with the keys of HashMap hm where the matching key is there
there i have to pull the corresponding value of it and put it in a List.
Like
ArrayList al = new ArrayList();
={1113,1111,1114,1112}
ASKER
ArrayList<String> superList = new ArrayList<String>();
superList.add("A");superLi st.add("B" );superLis t.add("C") ;superList .add("E"); superList. add("D");
HashMap hm = new HashMap();
hm.put("B","1111");hm.put( "D","1112" );hm.put(" A","1113") ;hm.put("E ","1114");
Set<String> set =new HashSet<String>();
Iterator it = superList.iterator();
while (it.hasNext())
{
// Get element
String element = (String)it.next();
set.add(element);
}
System.out.println("list in set"+set);
Set common = new HashSet(set.keySet());
common.retainAll( hm.keySet());
System.out.println(common) ;
After converting my list to hashset the set.keySet() will not work right.
ur logic will not work in that case...how to get those values to fit into ur logic....
superList.add("A");superLi
HashMap hm = new HashMap();
hm.put("B","1111");hm.put(
Set<String> set =new HashSet<String>();
Iterator it = superList.iterator();
while (it.hasNext())
{
// Get element
String element = (String)it.next();
set.add(element);
}
System.out.println("list in set"+set);
Set common = new HashSet(set.keySet());
common.retainAll( hm.keySet());
System.out.println(common)
After converting my list to hashset the set.keySet() will not work right.
ur logic will not work in that case...how to get those values to fit into ur logic....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
With generics:
Set<String> common = new HashSet<String>(superList) ;
Set<String> common = new HashSet<String>(superList)
The rest of it would be as follows:
common.retainAll(hm.keySet());
List<String> valuesList = new ArrayList<String>(common.size());
for (String key : common) {
valuesList.add(hm.get(key));
}
System.out.println(valuesList);
:-)
ASKER
ITS FAILING>>>>
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("getDat
Set commonKeys = new HashSet(AMap.keySet());
ArrayList<String> ruleGroupIds = new ArrayList<String>(commonKe
commonKeys.retainAll( tempSet );
System.out.println(commonK
for (Object key : commonKeys)
{
ArrayList<String> value = (ArrayList<String>) AMap.get(key);
ruleGroupIds.addAll(value)
}
System.out.println("result
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")
superList.add("A");superLi
HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
hm.put("B",ruleGroupIdsss)
hm.put("C",ruleGroupIdsss)
test5 t = new test5();
t.getData(hm, compareList);
}
}
ITS FAILING.The result is
result [C, B, A, E, D, 1234, 12345, 1234, 12345, 1234, 12345] which is wrong.
It sud pull corresponding arraylist instead of keeping all keys and values..
can u check..
Haven't really got time to look in detail but i would put more debug printouts in to see what you're starting and ending with
ASKER
i modified the logic its working...no need to proceed...
https://www.experts-exchange.com/questions/24528143/Comparing-Hash-Tables-and-accessing-values-dynamically.html?cid=1131&anchorAnswerId=24731075#a24731075