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
Solved

Arraylist elements Compare with HashMap keys

Posted on 2009-06-30
9
572 Views
Last Modified: 2013-12-29

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();
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}

Open in new window

0
Comment
Question by:satyabrata25
  • 6
  • 3
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24745804
0
 

Author Comment

by:satyabrata25
ID: 24746374
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");
            
            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....
            
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 24746517
>>Set common = new HashSet(set.keySet());

should be

Set common = new HashSet(superList);
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:CEHJ
ID: 24746521
With generics:

Set<String> common = new HashSet<String>(superList);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24748386
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);

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24752768
:-)
0
 

Author Comment

by:satyabrata25
ID: 24753020

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("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);
      }
}


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..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24753369
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
0
 

Author Comment

by:satyabrata25
ID: 24754040
i modified the logic its working...no need to proceed...
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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
servlet filter example 37 71
Configure a Bean in an XML file 4 42
Need Help! Getting a syntax error and don't understand why 3 31
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

808 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