This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes TreeMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "TreeMap" Watch "TreeMap" New topic
Author

TreeMap

Donald Wynn
Greenhorn

Joined: Dec 04, 2002
Posts: 29
I trying to use the TreeMap to sort some data I getting from a database but I also want to store the data in array for later processing. How can I do this?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Donald,
You can do something like this:

The first returns an array of Objects (duh) the second returns an array of the specified Object type (Foo).
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

I guess I don't understand how a TreeMap can be converted to an ArrayList. TreeMap holds associative data ('name' = 'value') and I don't see how that type of data could be converted to an ArrayList. Looking for the ctor for ArrayList I don't see one which accepts TreeMap.


Please ignore post, I have no idea what I am talking about.
Donald Wynn
Greenhorn

Joined: Dec 04, 2002
Posts: 29
How would I store my ResultSet in those arrays?
Donald Wynn
Greenhorn

Joined: Dec 04, 2002
Posts: 29
How would I store my ResultSet in those arrays?
Donald Wynn
Greenhorn

Joined: Dec 04, 2002
Posts: 29
Wil--
So I know a TreeMap holds associative data
('name' = 'value'). My question is there a way for me to set up an array of 'values' for a 'name'?
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

That's my question also.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

You say you want to store the data for later processing. If the later processing is going to happen without the program ending you can just leave the data in the TreeMap.
If by later processing you mean you need to save it to a file than I guess you can "serialize" it and save it that way.
Ashish Mahajan
Ranch Hand

Joined: Feb 19, 2003
Posts: 77
Hi Donald,
It's bit confusing to me. U said u wanna sort the entries and then put them in array for later processing. I think u mean using SortedSet implementation TreeSet which is simple collection like ArrayList with exception that it sorts the data. It's not key-value collection but i think ur requirement suggests TreeSet.
One more thing if u wanna process the data later then why to convert existing (usable) TreeSet to Array? U can access TreeSet's internal children more safely by Iterator than loop.
Final and most important, do not store Result Sets in collections instead wrap the data (Emp SSN, Name, Salary, etc..) in Object in this case Employee This practice is also called DAO (data access objects)
Hope this helps


The best teams have no specialists, only general contributors with special skills
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

I guess I don't understand how a TreeMap can be converted to an ArrayList.

I kinda looked before I leaped on that one. I was reading TreeMap and thinking TreeSet .
Michael Morris
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
I think that Michael was thinking that TreeMap was part of the Collection interface - which it is not.
You can use
Object myNames[] = myTreeMap.keySet().toArray(); //returns an array containing all the keys ('names')
Object myValues[] = myTreeMap.entrySet().toArray(); //returns a set containing all the values
[ February 27, 2003: Message edited by: Cindy Glass ]

"JavaRanch, where the deer and the Certified play" - David O'Meara
Donald Wynn
Greenhorn

Joined: Dec 04, 2002
Posts: 29
Uhh....I don't think I worded my question right.
I want to know if its possibe to have a array of values for a name. Just one array for values. Also what I mean by later processing is I wanted to pass my TreeMap to another function. Example:
Lets say I was storing student info(name and grades for each class that person took and I wanted to sort them by highest grade or by name).
Donald Wynn
Greenhorn

Joined: Dec 04, 2002
Posts: 29
Also I would like to display all the grades of a student by reference of 'name'
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

I want to know if its possibe to have a array of values for a name. Just one array for values.

You have exactly one name and you want one array for values associated with that name?
Or you have one or more names and for each name you want one array for values associated with that name?
Or you have one or more names and you want only one array to hold all the values?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Originally posted by Cindy Glass:

I think that Michael was thinking that TreeMap was part of the Collection interface - which it is not.

The key word here is thinking. My left brain wasn't paying attention to what my right brain was writing.

Originally posted by Donald Wynn:

I want to know if its possibe to have a array of values for a name. Just one array for values. Also what I mean by later processing is I wanted to pass my TreeMap to another function. Example:
Lets say I was storing student info(name and grades for each class that person took and I wanted to sort them by highest grade or by name).

You can map an array indexed to a name. Look at this:

Of course, if you want the arrays to be dynamic (able to add or delete elements) then instead of mapping the student to an array you could use a Vector or List instead.
Hope I got it right this time
Michael Morris
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Also what I mean by later processing is I wanted to pass my TreeMap to another function.
Example: Lets say I was storing student info(name and grades for each class that person took and I wanted to sort them by highest grade or by name).

Since you already have the data stored, and sorted, I would be included to just step through tree and extract each item. As I extracted it I would insert it into another data structure, Vector maybe, in the order it is needed. So grab it, and sort it on the run. So you are left with a Vector of the sorted data, which you can print out and than throw away.
Ashish Mahajan
Ranch Hand

Joined: Feb 19, 2003
Posts: 77
Hi Donald,
Am catching on the posts. I'll go thru it in detail. Right now am not at work, borwsing from Cyber cafe.
So will post on Monday night. Thanx.
Ashish Mahajan
Ranch Hand

Joined: Feb 19, 2003
Posts: 77
Hi Donald,
I think now i got ur prob straight. After querying the database u r getting a result set with the info of the required students with their names and grades etc. And the solution that u r trying to figure out is to put the student names in TreeMap as keys and their grades in array as it's values.
Ofcouse u can do it with TreeMap by putting student name as key and it's grades in array as values since TreeMap can take any object as it's key value.
But i would suggest u with the following design decision :-
1. As i said previously think of wrapping the student related info in a class named in Student. In the loop create new Student object and put them in ArrayList. We have chosen array List cause the requirement is that we need to sort the data differently (sometimes by name, sometimes by grades) in different situations.
Wrapping the data in Student is called Data Access Objects. Unfortunately such JDBC patterns have not yet been integrated if anybody does in the future this will be the first one. Gurus from JDBC forum can best guide us. As an example read the "Time Keeping Machine" case study in Patterns In Java vol 1 by Mark Grand and see how he wraps Employee data in Employee objects.
Always encapsulate tightly related data in a meaningful object. That way we can assure of integraty betn related data. U know this is the same reason why java don't have multiple return types.
2. Second is use array only for the static type of data. For dynamic iterator is the best to go. Please read iterator pattern from the famous GoF book Design Patterns
Also read Iterator javadoc.
Now in this case if the client is concerned with the students only and not how many students etc. The class managing database queries should return Iterator of Students objects. If the client wanna know details like no. of students etc then better pass Collection in our case ArrayList. But always access the internal data with Iterator pattern.
Just a note. Since there is a requirement to sort the student data in different ways. It is best to declare public static inner Comparator classes within student class. i.e. different Comparator class for different sorting routine other than natural ordering. This design principle is kindly suggested by our list mate James Robertson. The reason being since student class is in the best position to know the internal data structure of it thus it is in best position to provide different sorting routine. Also Moreover how the client can know about the internal data structure and thus sorting routies.
Hope this helps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TreeMap