This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Treenode from Object  RSS feed

 
Manuel Kampert
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

This is more a general question about datastrucutre and objects. However as there is only a Tree structure in the swing packages - and my question is about the jTree I hope it's ok to post my question here...

I have a list of vehicle objects with the following attributes:


Now I would like to transform this to a tree like that:

At the moment I am looping thru the object list and generate "Set"s of
the different available attributes. Then I put together "Vector"s of the
specific attribute path. I.e. vector of all red cabrio cars.

However this seems a little unnatural to me and I ask myself if there
is a better solution. If I would have a list of thousands of objects
then I think looping the whole for about 20 time for 20 attributes is "a little" overkill...

Any suggestions?

Best Regards,
Manuel
[ May 22, 2008: Message edited by: Manuel Kampert ]
 
Campbell Ritchie
Sheriff
Posts: 55329
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Manuel

A bit off topic maybe, but that looks like a database question
SELECT * FROM cars WHERE colour = 'red' AND make LIKE 'Mer%' ORDER BY registration_number;
 
Manuel Kampert
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

Sorry, but it hasn't to do anything with databases. But even if it where - how would you statement help here? I were in the same situation if I would process an recordset.

Best Regards,
Manuel
[ May 22, 2008: Message edited by: Manuel Kampert ]
 
Campbell Ritchie
Sheriff
Posts: 55329
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A ResultSet would have the results you want rather than your having to traverse the whole tree.

It looks as though you will have to do it the hard way, writing a VehicleRoot with nodes for Cars Buses etc. It will be a nightmare to traverse.
 
Campbell Ritchie
Sheriff
Posts: 55329
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not at all convinced that is what JTree was designed to handle. You will have
Car-VW-red
and
Bus-Mercedes-green
so your "tree" will expand with exponential complexity.
 
Manuel Kampert
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about creating a adapter object that implements comparable?

Then I can add the vehicles in an ordered list. With an odered list I
can then start



Feels not really object oriented - more like group changing algorithm in reports that I have done in the 90s :-)

However it would decrease the exponential complexity to linear complexity ?!

Regards
Manuel
[ May 22, 2008: Message edited by: Manuel Kampert ]
 
Campbell Ritchie
Sheriff
Posts: 55329
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know. Only one way to find out-try it!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!