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.