• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About the duplication of keys, I suggest you read the API of the put very carefully.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Janeice:

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

John.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.... 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic