Regarding use of ArrayList as value in Hashtable

U

u126561

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,new ArrayList());
}
}
 
A

Andrew McDonagh

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,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!!!!!!


you can't with a HashMap - the keys only map to a single value object.
 
U

u126561

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?
 
J

Jeroen V.

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");
....
 
R

Rhino

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,new 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$
}
}
}
 
K

Ken

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.
 
A

Andrew McDonagh

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)'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top