String output of a Data Structure

K

Ken

Hello Java Experts,

I am quite new to Java.

I have created a Structure of HashMaps, LinkedLists, Strings and
Integers.

I want to know how to display this structure but toString() does not
seem to be the answer. I need the string output: it will do something
funny like
{this object}, 5

Here is the Structure:
The Head Element is a HashMap all hashmaps use Strings for keys.
The values of a hashmap maybe of type: Map (Implemented as Hashmap but
I will change over to a LinkedHashMap as order is a requirement),
List(Implemented as LinkedList), String, or Integer.

LinkedLists may contain: Maps, Lists, Strings, Integers.

What I want to do is spit out the data in some format which I can use
to validate that I've constructed the data structure correctly.

If there are no recommendations I am sure later tonight the NetBeans
IDE will show me if the obect has been created correctly. However I
would find it interesting if there is a simple solution.

Any help much appreciated,
Ken.
 
P

Philipp Leitner

Hi Ken,

altough I cannot currently find it I remember that there was a very
similar question just a few days ago in this forum or in c.l.j.help .
Maybe you search this thread if it already answers your question.

As for your problem: I do not think that there is a standard way of
visualizing data structures like what you described, but it should be
rather easy to do on yourself. Especially if you're new to Java it might
be a good exercise to start with.

good luck,
Philipp
 
M

Matt Humphrey

Ken said:
Hello Java Experts,

I am quite new to Java.

I have created a Structure of HashMaps, LinkedLists, Strings and
Integers.

I want to know how to display this structure but toString() does not
seem to be the answer. I need the string output: it will do something
funny like
{this object}, 5

The XStream serializer http://xstream.codehaus.org/ will give you an XML
representation of your object graph. You can download the jar and use it
something like this:

Object obj = ...
XStream xs = new XStream ();
String resultStr = xs.toXML (obj);

The resulting string mimics your object structure and is quite readable.
You can print, store, whatever the resulting string. You can even save the
string to a file, edit it and reload it to reconstruct a new object.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com./
 
K

Ken

Matt:
Wow... I'm going to try that XStream thing out right away.

Philipp:
About making it an exersise... you are right it would be a good one.
But I have enough of an exersise as it is right now. I found an answer
in TIJ when I read about RTTI... All I need to do is extract objects
and down cast to my Four data types... simple as that, if it does not
work an exception will be thrown and I will advance to the next cast
attempt until I find a cast that works then itterate throught the data,
Well I would allso need to write a display method for each type... and
some method of formatting. Well another day.

I will get back if this XStream thing works because I see a nicely
formated XML file being more than I even imagined and it would be most
useful in the future.
 
K

Ken

Matt:
Wow... I'm going to try that XStream thing out right away.

Philipp:
About making it an exercise... you are right it would be a good one.
But I have enough of an exercise as it is right now. I found an answer
in TIJ when I read about RTTI... All I need to do is extract objects
and down cast to my Four data types... simple as that, if it does not
work an exception will be thrown and I will advance to the next cast
attempt until I find a cast that works then iterate through the data,
Well I would all so need to write a display method for each type... and
some method of formatting. Well another day.

I will get back if this XStream thing works because I see a nicely
formatted XML file being more than I even imagined and it would be most
useful in the future.
 
J

Jochen Schulz

* Ken:
I have created a Structure of HashMaps, LinkedLists, Strings and
Integers.

I want to know how to display this structure but toString() does not
seem to be the answer. I need the string output: it will do something
funny like
{this object}, 5

If you are searching for a graphical solution, take a look at graphviz
(<http://www.graphviz.org/>). It can draw graphs in various file
formats. You just have to print out your structure in the DOT language
(which is a very easy thing to do) and feed it into the "dot" program.

I just did this for a recursive tree structure which I implemented. I
really like the results.

J.
 
M

Matt Humphrey

Ken said:
Matt:
Wow... I'm going to try that XStream thing out right away.

Philipp:
About making it an exercise... you are right it would be a good one.
But I have enough of an exercise as it is right now. I found an answer
in TIJ when I read about RTTI... All I need to do is extract objects
and down cast to my Four data types... simple as that, if it does not
work an exception will be thrown and I will advance to the next cast
attempt until I find a cast that works then iterate through the data,
Well I would all so need to write a display method for each type... and
some method of formatting. Well another day.

Using progressive casting is an extremely ugly technique. A better (but
still poor) solution is to use instanceof, but this still ties your code to
a specific collection of classes. For whatever you plan on doing with your
different kinds of objects (visual display, string formatter / parser,
editor, whatever), you should consider creating a uniform interface. Then
make up a handler that implements this interface for each of your different
kinds of objects. Put these handlers into a HashMap, indexed by the
object's class. When you get an object, lookup the class in the map and use
the appropriate handler. If you have to add new kinds of classes or need to
extend or change how the handers are used you just add / change then handler
and don't have to track down the class tests.

To illustrate this point in a very rudimentary way:

// Uniform interface for handling all my kinds of objects
Interface MyHandlerInterface {
void doThis (Object o)
String format (Object o);
}

// Specific handler for just Foo objects
class MyFooHandler implements MyHandlerInterface {
void doThis (Object o) {
Foo foo = (Foo)o;
// Do the thing with foo
}
String format (Object o) {
Foo foo = (Foo)o;
// Return a string format of foo
}
}

Map handlerMap = new HashMap ();

handlerMap.put (Foo.class, new MyFooHandler ());

Object o = // Get an object from somewhere
MyHandlerInterface handler =
(MyHandlerInterface)handlerMap.get(o.getClass());
if (handler == null) handler = DefaultHandler;

handler.doThis (o);
String s = handler.format(o);

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 

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,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top