Results 1 to 3 of 3
  1. #1
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default ArrayList into hashMap

    I have two arrays..Now i want to convert it into hashMap..one array as 'value' and another array as 'key'

    Is it possible to do it?
    *Can anyone say me,how to do?

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default

    Hello Preethi

    I've derived the following class from HashMap. It's constructor might be useful to you:
    Java Code:
    public class HashMapAuto <K, V> extends HashMap{
    	@SuppressWarnings("unchecked")
    	public HashMapAuto(List<K> keys, List<V> values) throws Exception{
    		super();
    		if (keys.size() != values.size()) throw new Exception("'keys' list and 'values' list differs in size");
    		for (int i = 0; i < keys.size(); i++){
    			Object newKey = keys.get(i);
    			Object newValue = values.get(i);
    			this.put(newKey, newValue);
    		}
    	}
    }
    You can use it as a normal HashMap and put it in HashMap variables. The constructor is the nice part. If you have some lists, say ArrayList instances, for the keys and values respectively, then you can pass them to the constructor of HashMapAuto. ;)
    For example:
    Java Code:
    ArrayList<String> keys = createKeys();
    ArrayList<String> values = createValues();
    try{
    	HashMap<String, String> test = new HashMapAuto<String, String>(keys, values);
    } catch (Exception e){
    	System.out.println(e.getMessage());
    }
    Now, you can use test as a normal HashMap. :D

    I hope this helps. This is the first time that I used generics and I used the SuppressWarnings annotation. I'm not very happy with this. Maybe someone can show me a better may of using generics when extending classes. :p

    However, you are not limited to strings. You can use any class that you desire. Good luck Preethi. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default comapring two ArrayList

    Java Code:
    I want to find the similar values present in the list with respect to another...and my code for this is,
    
    
      for(int i=0;i<res_mseg.size();i++)
            {
                for(int j=0;j<rev_list.size();j++)
                {
                    int ind = res_mseg.indexOf(i);
                    if(rev_list.listIterator(j).equals(res_mseg.listIterator(i)))
                   {
                    	 System.out.println(rev_list.get(j));
                    }
                }
            }
    
    But its not producing the o/p

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  2. Soft HashMap
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 09:45 PM
  3. HashMap visual example
    By jhetfield18 in forum Advanced Java
    Replies: 1
    Last Post: 12-12-2007, 08:45 PM
  4. Hashmap
    By dirtycash in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 03:58 AM
  5. what is hashmap
    By gabriel in forum New To Java
    Replies: 5
    Last Post: 08-03-2007, 02:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •