File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Sorted map -- can't get there from here? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorted map -- can Watch "Sorted map -- can New topic
Author

Sorted map -- can't get there from here?

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

Alright.... the last part of this school project is to create a method that uses a Map collection type where the key is the score and the value is the number of students who received that score.

The logic, I think, looks like this:


This is what I have:


I did some reading, and it looks like it's not going to work. the Map is listed like an array and well..... I need to know the address of the key before I can change the value, right?

I know where I am, and where I want to go....
Can I get there from here?
Janeice


When you do things right, people won't be sure you've done anything at all.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

The basic idea is just fine, it's just the implementation that's not right yet.
1) SortedMap is an interface, so you will need to find a class that implements it. Fortunately, the API provides one such class already for you, just check it out: java.util.SortedMap
2) There are no such methods as setValue and getValue. Instead, they are called put and get, and you need to provide the key (s.getScore()) for both.
3) This line is just fine, except it will always be executed, even if the score is already present. I think you need to put it in an else block.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Why are you using a SortedMap? Won't an ordinary Map work? I presume you are familiar with the Java™ Tutorials section? These are the two sections most relevant to Maps: 1. 2.

If you search this forum, you will find several people have used Maps for counting in the past. Very similar to what you are doing. It tends to be how many different letters there are in a piece of text. You use the put and get methods, usually.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

This is where I saw the getValue and setValue

.... I'm using a sorted list because the list needs to be sent to a file and I think that:

10 1
9 3
8 2
7 1

looks better than

8 2
9 3
7 1
10 1

i.e. I would like to sort by score so (perhaps) a teacher would be able to estimate whether a bell curve exists.

Thanks for the links! I will check them out. I'm glad to see I'm not too far off.....
Janeice
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

Do I have to do this to make this work:



??

Janeice

P.S. what the heck is the difference between "implements" and "extends" anyways?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
No. Your grade book will neither implement nor extend the Map interface. It will use a field of some class which implements the Map interface. Since SortedMap extends the Map interface, you can use the put and get methods on that.

Except in generic type declarations, you extend a class (one only). Classes implement an interface. Interfaces can extend other interfaces (one or more).
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

if I use the put method.... won't that create a new entry every time it goes around?

I mean.... I'm not putting a new entry in, I'm editing an entry that already exists. I don't want my map to look like

10 1
9 1
9 2
8 1
8 2
8 3
7 1

I want it to look like:
10 1
9 2
8 3
7 1

I think I'm not understanding why we're using put and not setValue
Janeice
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Have you done the search I suggested? This is very similar to counting letters in text. If you do a search you find things like this and this, and there were other links which didn't seem as useful.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

I have looked at the threads you linked to, as well as some that I agree didn't seem helpful....

Here's my question....

when you do this:



How is it not creating a new entry?
i.e. if you have ten students who got a 10 score, wouldn't you end up with:

10 1
10 2
10 3
10 4
10 5
etc... for all 10 entries?

Janeice

[EDIT] I was going to try the code above.... it doesn't compile.... says SortedMap is an abstract class and can't be instantiated.... so I changed to just Map.... same error.... what am I doing wrong?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
To the API! Note what sort of compilation type Map and SortedMap are. There are two likely classes: HashMap and LinkedHasMap.
Do you really want that Map to be a local variable? It will vanish when the method completes. And I see you have found the useful parts of those old threads

No, you would not end up with 10 1 10 2 10 3 . . .

Look at what it says about Map in the Java Tutorial link I posted earlier.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

I used:



as an instance declaration.... I don't really know why I used TreeMap instead of anything else.... or why it needed sorted map on the left and treemap on the right.......

It really doesn't matter if it disappears when the method ends.... it outputs to a file :-)

I guess I see why its different... it's not addressed like an array, it's addressed by key. That key can go to one and only one value. It's like a tiny database.

I got the whole thing working. I can relax a bit and work on the whys now.

Thanks a lot!
Janeice
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

About the duplication of keys, I suggest you read the API of the put very carefully.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

as an instance declaration.... I don't really know why I used TreeMap instead of anything else.... or why it needed sorted map on the left and treemap on the right.......


SortedMap is an interface. TreeMap is one of the implementations of that interface -- among other interfaces too.

Technically, you could have made both sides (left and right) of the expression TreeMap. The advantage here is that with this declaration, you can use a different sortedmap implementation (besides treemap) and you'll won't have to change lots of code.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Oh, TreeMap? I thought you would use LinkedHashMap. I also thought you would lose the details because you had a local variable, but you have taken care of that. Beware of using fields and local variables with similar names; you can get some dangerous confusion and not find out about it until you get a complaint about wrong results.

I have since yesterday realised that there is a much more object-oriented way of updating the count than put(score, get(score) + 1). See if you can work it out.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

Campbell -- I have been afraid of the term "hash" since I got that weird hash code output with my last application.....

I will think about your puzzle today.


..... as for all the variables..... I just dunno how to go about naming something different when there's multiple classes dealing with the same value. For example:

The Student class, the gradebook class and the quiz grading class.....

All of these classes "touch" at one point or another the grade of the student. I could only think of two words for grade: score and grade.

How the heck can I come up with unique names for them all?

And Henry.... I've heard these terms "interface" and "implementation". Am I to understand that you can't USE the interface without using it's implementation?

Thanks again!
Janeice
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
A hash is easy to understand; it is a number. You want the hash to be different from differing objects, and the same from identical objects, as far as possible.
You are stuck with terms like grade mark and score; there are only a few words available in the thesaurus and dictionary.
Go and find the original code for an interface (look in your Java installation directory for a file called src.zip and unzip it). You see all the methods look like thisTo make use of that interface you must find a class which implements it, which means it has something "real" in all those methods.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

Still thinking about the puzzle....

But here's my new question:

Howcome (and I'm sure there's good reason) I couldn't just make my OWN class implement the implementation for the interface:



Maybe I don't get WHY we put things in the class/method declarations..... I do get the public/protected/private, the name, the return type, the parameter types.... but what about the other things..... like static, final, and the implements and extends...... and why does the main method always have to be static with that weird String and args thing??

Just wonderin.... and this puzzle may need a clue....
--- can I find the answer in the API, or is it more of a logic concept, or both?

Janeice
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
No.

You are using a Map, not creating one. I don't even think it will compile, what you have written.

You will still use countMap.get(score) but what you get will not be an Integer.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

Campbell Ritchie wrote:Oh, TreeMap? I thought you would use LinkedHashMap.

**ahem** according to the API, the only classes that implement SortedMap are TreeMap and ConcurrentSkipListMap. I looked at the latter and it has a method called putIfAbsent...... sounds like there's a more OO way to handle THAT part of the app..... could take that whole if/else loop outta there.

Campbell Ritchie wrote:I have since yesterday realised that there is a much more object-oriented way of updating the count than put(score, get(score) + 1). See if you can work it out.


The only thing I can think of is:

John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
But here's my new question:

Howcome (and I'm sure there's good reason) I couldn't just make my OWN class implement the implementation for the interface:


Why do all that extra work if you don't need to? Do you expect to reuse your Student class? What would writing an implementation get you that using a TreeMap doesn't?

John.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

John de Michele wrote:

Why do all that extra work if you don't need to? Do you expect to reuse your Student class? What would writing an implementation get you that using a TreeMap doesn't?



I guess I just didn't know all it entailed. I thought just putting that little "implements whatever" on top it would be, you know, good.

.... and I just don't know enough yet to know how to work smarter not harder....

--Janeice
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Janeice:

Well, you're learning, and that's a good thing .

John.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
You haven't picked up my hint. You know you had the bit about Map<Something, Integer> which counts? Well, have you ever created a Counter class?
See how far that hints gets you.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

Let's see.... at the risk of being COMPLETELY off my rocker....



.... and no I didn't know counter classes existed until I googled them, just now.
Janeice
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

That's getting awfully close. In the first branch of the "if", though, ask youself whether you need to "put" anything into the map, since by definition the key already exists in the map. You really just want to "get" the Counter that already exists, yes? Given the grade, how would you "get" the existing Counter out of the map?


[Jess in Action][AskingGoodQuestions]
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

.... I thought because the value type was a Counter, it would know to increment that counter.... guess not.



Now this all, with the counter class, seems like a lot more work than using ints.... what is the benefit?

Janeice
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

That's looking good!

I'd make one stylistic change (although it doesn't change how the code works at all.) YOu've declared a "count" variable outside the loop, which implies that the value of "count" over time matters. In other words, it implies that the values of "count" from one loop iteration to the next are connected somehow. But they're not, really; each one is a different object, dealt with individually. So instead of declaring that variable once outside the loop, I'd declare it twice, once in each branch of the "if":



The generated bytecode should actually be identical, but the intent is clearer when reading this code.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12



I'm lucky I knew it would have to be done outside (or I guess in each branch, but I've never tried declaring something twice) or it won't compile.... I think I'm getting the hang of this.

But why this instead of the old way?

Janeice
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
That is exactly the sort of thing I was thinking of. Rather than messing around with boxing, you simply call a method on the Counter value in the Map and it counts. The increment method simply contains i++; or similar, so you are using simple arithmetic with ints, which will give a (slight) performance enhancement over boxing.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

I usually use the following code instead, when you modify an element that may not be present yet:
That way you can put any common code after the if - the only thing the if does is ensure the object is in the map.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1686
    
  12

As I hang around here more and more I realize there's so many good ways to do things! Both ways work -- and the more eyes you get on something the more it changes!


Janeice
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorted map -- can't get there from here?