Results 1 to 3 of 3
Thread: ArrayList into hashMap
- 02-08-2008, 11:29 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
- 02-08-2008, 07:31 PM #2
Hello Preethi
I've derived the following class from HashMap. It's constructor might be useful to you:
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. ;)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); } } }
For example:
Now, you can use test as a normal HashMap. :DJava 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()); }
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.
- 02-11-2008, 08:13 AM #3
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
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
-
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
Soft HashMap
By Java Tip in forum java.langReplies: 0Last Post: 04-12-2008, 08:45 PM -
HashMap visual example
By jhetfield18 in forum Advanced JavaReplies: 1Last Post: 12-12-2007, 07:45 PM -
Hashmap
By dirtycash in forum New To JavaReplies: 5Last Post: 12-03-2007, 02:58 AM -
what is hashmap
By gabriel in forum New To JavaReplies: 5Last Post: 08-03-2007, 01:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks