Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-08-2008, 01:29 PM
Member
 
Join Date: Jan 2008
Posts: 83
Preethi is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-08-2008, 09:31 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Hello Preethi

I've derived the following class from HashMap. It's constructor might be useful to you:
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:
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.

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.

However, you are not limited to strings. You can use any class that you desire. Good luck Preethi.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-11-2008, 10:13 AM
Member
 
Join Date: Jan 2008
Posts: 83
Preethi is on a distinguished road
comapring two ArrayList
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Project Trouble: Searching one ArrayList with another ArrayList BC2210 New To Java 2 04-21-2008 01:43 PM
Soft HashMap Java Tip java.lang 0 04-12-2008 10:45 PM
HashMap visual example jhetfield18 Advanced Java 1 12-12-2007 09:45 PM
Hashmap dirtycash New To Java 5 12-03-2007 04:58 AM
what is hashmap gabriel New To Java 5 08-03-2007 03:23 PM


All times are GMT +3. The time now is 04:41 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org