Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Regarding use of ArrayList as value in Hashtable

Thread view: 
u126561@yahoo.com.au - 06 May 2006 16:39 GMT
Hi all,
       could anyone who knows how to solve this problem please give me
some hints?

The question is:
I have set up a Hashtable and strings as keys and empty ArrayList as
values:
  Hashtable iword = new Hashtable();
  while ((inLine = inputStream.readLine()) != null) {
        input = inLine.split(" ",-2);
               for (int i = 0 ; i < input.length; i++){
           iword.put(input[i],new ArrayList());
        }
             }
------------------------------------------------
but, now, how can I insert more than one value for a specified
key?(i.e, insert string into the ArrayList)

Many Thanks!!!!!!
Andrew McDonagh - 06 May 2006 16:43 GMT
> Hi all,
>         could anyone who knows how to solve this problem please give me
[quoted text clipped - 15 lines]
>
> Many Thanks!!!!!!

you can't with a HashMap - the keys only map to a single value object.
u126561@yahoo.com.au - 06 May 2006 16:51 GMT
thanks for your reply.  ^_^

I only map one key to one value object, the just object is ArrayList,
say, for example,the value is a String array, with String array then we
should be able to put multi-values. Am I right?
Jeroen V. - 06 May 2006 17:19 GMT
> thanks for your reply.  ^_^
>
>  I only map one key to one value object, the just object is ArrayList,
> say, for example,the value is a String array, with String array then we
> should be able to put multi-values. Am I right?

Map<String, List<String>> map = new HashMap<String,List<String>>();

map.put("key1", new ArrayList<String>());
map.get("key1").add("value1");
map.get("key1").add("value2");
map.get("key1").add("value3");
...
Ken - 06 May 2006 17:50 GMT
> thanks for your reply.  ^_^
>
>  I only map one key to one value object, the just object is ArrayList,
> say, for example,the value is a String array, with String array then we
> should be able to put multi-values. Am I right?

There are several ways to do what you wish...  it is funny I am new to
Java and came here to ask the opposite: I have a structure much like
you have built but want to display it, however my structure is a
HashMap where the keys are strings and the values are further HashMaps,
LinkedLists, Integers and Strings.  Any ways I just want to print the
structure... (I'll make a separate thread in a moment).

My structure is a bit more complicated... but I will tell you roughly
how it was constructed and it will definitely provide a solution to
your issue:

1) I have a string of characters and those characters represents Maps,
Lists, Integers, and Strings.  The exact method of encoding is called
bencoding... if you are interested in a very simple method to make such
a representation see: [http://www.bittorrent.org/protocol.html] under
the section "The connectivity is as follows:"

2) An object splits the stream of characters into tokens of type: MAP,
LIST, INTEGER, STRING, END, and EOS (end of stream is so the system is
sure it has reached the end of the input, if EOS received in an illegal
condition (the middle of a string or such) then there is a way to find
the error). (This splitting is called Lexical Analysis)

(HERE IS WHERE YOU'RE PARTICULAR ISSUES MAY BE ADDRESSED)
The next step is to use the tokens to assemble the data structure.  If
you did read the bencoding definition note that only Maps and Lists
require END tokens (to specify where they end) Integers have an 'e' but
END is only needed to define the end of container since it is clear
where the end of a string or integer is.

3) I used a stack created with a LinkedList.  The last item on the
stack is the structure which is being manipulated.  Once you have
reached the terminating condition (in my case receiving a END token)
the stack is popped and you return to a higher level and are working
with the original object.

The trick of course is knowing what container you are working with...
since a Map and a List have different interfaces.  Clearly there must
be a method to determine what we are working with: My solution was
using an enum.

enum BContainers{
    DICTIONARY, LIST;
    private Object ref;
    private String key;

    public void setReference(Object ref){
        this.ref = ref;
    }

    public Object getReference(){
        return ref;
    }

    public void setKey(String key) throws BSyntaxException{
        if (this == BContainers.DICTIONARY)
            this.key = key;
        else
            throw new BSyntaxException("Programmer error - Attempt to set key
for LIST");
        }

    public String getKey() throws BSyntaxException{
        if (this == BContainers.DICTIONARY)
            return key;
        else
            throw new BSyntaxException("Programmer error - Attempt to get key
for LIST");
    }
}

*** Disclaimer: I all ready said I was new to Java and when solving
this the first thing I fell upon was an enum, making this an object
with a type() method would also work, reflection, RTTI would work too
somehow... but the sound of enums appeal to the C programmer in me.
Also the exceptions could be removed come to think about it if the
methods were moved under the DICTIONARY case I think...  enums with
methods... what will they think of next.

>From here it is easy to see a solution:
You first create your Map and push it to the stack... then make an add
method which takes elements of you string... the add method can use the
enum as a convenient way to split the logic between list and map
addition operations... when it comes time to add a list, add the list
to the map and then push the map the container stack.  The add method
automatically uses the last object on the container stack.

Methods in the enum: get and set reference holds the reference to the
container and must be set before it is pushed.

get and set key is in the case the container object is being used as a
dictionary of course...

I probably wrote more than I needed... hope it lets you see some light.
Andrew McDonagh - 06 May 2006 18:15 GMT
> thanks for your reply.  ^_^
>
>  I only map one key to one value object, the just object is ArrayList,
> say, for example,the value is a String array, with String array then we
> should be able to put multi-values. Am I right?

yep that would work - but you would have to add the strings to the
arraylist yourself.

as in....

List l = null;

if (myMap.containsKey("key") == false) {
  myMap.put("key", new ArrayList());
}

l = myMap.get("key");

l.add(stringToAdd)'
Rhino - 06 May 2006 17:39 GMT
> Hi all,
>        could anyone who knows how to solve this problem please give me
[quoted text clipped - 13 lines]
> but, now, how can I insert more than one value for a specified
> key?(i.e, insert string into the ArrayList)

This simple program illustrates the technique you need to use to populate
the ArrayList after it has been added to the Hashtable:

---------------------------------------------------------------------------------------------------------------------
package u126561;

import java.util.ArrayList;
import java.util.Hashtable;

public class HashArray {

   /**
    * @param args
    */
   public static void main(String[] args) {

       new HashArray();
   }

   @SuppressWarnings("unchecked")
   public HashArray() {

       /* The keys for the Hashtable. */
       String[] animals = {"cat", "dog"}; //$NON-NLS-1$ //$NON-NLS-2$

       /*
        * Create the Hashtable, assuming that each key will be the name of
an animal species and
        * that an ArrayList of names of animals of that species will
comprise the values of the
        * ArrayList. The names of the animals are not known so the
ArrayList will be initially
        * empty.
        */
       Hashtable<String, ArrayList> iword = new Hashtable<String,
ArrayList>();
       for (String oneAnimal : animals) {
           iword.put(oneAnimal, new ArrayList());
       }

       /* Get a reference to the ArrayList for cats; add cat names to the
ArrayList. */
       ArrayList<String> myArrayList = iword.get("cat"); //$NON-NLS-1$
       myArrayList.add("Fluffy"); //$NON-NLS-1$
       myArrayList.add("Smokey"); //$NON-NLS-1$
       myArrayList.add("Morris"); //$NON-NLS-1$

       /* Get a reference to the ArrayList for dogs; add dog names to the
ArrayList. */
       myArrayList = iword.get("dog"); //$NON-NLS-1$
       myArrayList.add("Rex"); //$NON-NLS-1$
       myArrayList.add("Fido"); //$NON-NLS-1$
       myArrayList.add("Lassie"); //$NON-NLS-1$

       /* Display the ArrayList for cats. */
       myArrayList = iword.get("cat"); //$NON-NLS-1$
       for (String name : myArrayList) {
           System.out.println("Cat: " + name); //$NON-NLS-1$
       }

       /* Display the ArrayList for dogs. */
       myArrayList = iword.get("dog"); //$NON-NLS-1$
       for (String name : myArrayList) {
           System.out.println("Dog: " + name); //$NON-NLS-1$
       }
   }
}
---------------------------------------------------------------------------------------------------------------------

--
Rhino
u126561@yahoo.com.au - 06 May 2006 19:29 GMT
Great, Rhino, thank you very much!
u126561@yahoo.com.au - 06 May 2006 19:33 GMT
Much thanks for the all guys here, my problem solved. ^_^
Rhino - 07 May 2006 14:21 GMT
> Great, Rhino, thank you very much!

You're welcome :-)

--
Rhino


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.