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 Using the class methods of something in a TreeMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using the class methods of something in a TreeMap" Watch "Using the class methods of something in a TreeMap" New topic
Author

Using the class methods of something in a TreeMap

David Burt
Greenhorn

Joined: Mar 18, 2013
Posts: 10
I know there are already topics on this exact thing but none of them actually answer my question. is there a way to do this?
if I have a TreeMap that uses strings as the keys and objects of the TreeSet class as the values, is there a way that I can add some int to a set that is associated with a specific key?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7674
    
  19

David Burt wrote:I know there are already topics on this exact thing but none of them actually answer my question. is there a way to do this?
if I have a TreeMap that uses strings as the keys and objects of the TreeSet class as the values, is there a way that I can add some int to a set that is associated with a specific key?

Of course; but I think we need a bit more information in order to advise you properly.

The general paradigm would be:
map.get(key).add(value);

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
David Burt
Greenhorn

Joined: Mar 18, 2013
Posts: 10
Well what I'm supposed to do is make a concordance from a text file using the TreeMap and TreeSet class. my plan is this use the TreeMap keys as the words in the text file and the values will be sets of line numbers on which the word appears. So you step through the text file and every time you get a word you check the TreeMap to see if you already have that key and if you don't you add it in and create a new TreeSet of line numbers starting with the one you are on. If you already have it then you just add the line number to the set. So you see what I need to do is access the .add() function of the set

something like



I know that doesn't work but how do I do it?
I mean if there is an easier way to do what I'm trying to do I'd be happy to do that instead, but I would still like to know how to do it this way just for you know learning and experience and all that.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14113
    
  16

Your plan on how to do it sounds good. So, what exactly is your question about? Do you have a question on how to implement it in Java code? Did you try it yourself? Show us your code. Where did you get stuck?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
David Burt
Greenhorn

Joined: Mar 18, 2013
Posts: 10
I haven't really done much since I got stuck and this code doesn't work. what I started doing is making a class called concordanceMap that has a TreeMap as one of the instance variables. I did this cause I thought it would make it easier to create a seperate TreeSet for every key in the TreeMap.



the part I'm stuck on really are the insertIdentifier() method and the insertValue() method. I've told you what I'm trying to do with them already. I want to use the insertIdentifier method when I find a word in the text file that isn't already in the Map, and the insertValue method to add the line number to the set at that time or if I find a word that is already in the TreeMap
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7674
    
  19

David Burt wrote:something like

And just to add to Jesper's good advice, good names can help you out an awful lot.

So, instead of 'identifier', how about 'word'?

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7674
    
  19

David Burt wrote:I haven't really done much since I got stuck and this code doesn't work...

Unsurprising, because it doesn't seem like you've actually done very much.

What is AnyType?

You're not defining a generic class (in which case it should be <T> anyway), you're defining a concordance for words. And what are they?

Winston
David Burt
Greenhorn

Joined: Mar 18, 2013
Posts: 10
well, I suppose I could use word, instead of identifier, but the project does say to make a concordance of "identifiers" where an identifier is defined as a string that begins with a letter (A-Z, a-z) and is followed by zero or more other characters that are a letter or a digit (0-9), so that's why i used identifier
David Burt
Greenhorn

Joined: Mar 18, 2013
Posts: 10
in the other part of the project we had to use an AvlTree class from our textbook to do the same thing. AnyType is how the author of that code wrote it. our professor wants us to write code that is intentionally as reusable as possible, so even though in this project we are using it to make a concordance it should be written so that it can be used for any number of things, so that's why I used <AnyType>. to be honest I don't really know what the possible ramifications are of using that and I don't really know how it works. but I guess I'm still learning.

This idea of reusable code is also why I'm leaving it up to the Main to define what is meant by a word or identifier

again if I'm being stupid and making grievous errors please tell me flat out, I'm new at this and the last thing I need is for you to pull your punches so give it to me straight.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7674
    
  19

David Burt wrote:in the other part of the project we had to use an AvlTree class from our textbook to do the same thing. AnyType is how the author of that code wrote it. our professor wants us to write code that is intentionally as reusable as possible, so even though in this project we are using it to make a concordance it should be written so that it can be used for any number of things, so that's why I used <AnyType>. to be honest I don't really know what the possible ramifications are of using that and I don't really know how it works. but I guess I'm still learning.

This idea of reusable code is also why I'm leaving it up to the Main to define what is meant by a word or identifier

again if I'm being stupid and making grievous errors please tell me flat out, I'm new at this and the last thing I need is for you to pull your punches so give it to me straight.

OK: you're doing it wrong.

Actually, since we have very little to go on in terms of code, I have absolutely no idea whether you're doing it wrong or not. What I can tell you is that everything your prof has told you is exactly what I would have told you; I suspect that you're extrapolating that advice in ways s/he didn't intend.

Also: you can tie yourself in knots trying to think about a problem in totally generic terms. What has he asked you to do? Split a file into "words" that you can then index. The only thing that's tough about that is:
1. What is a "word"?
2. How do you "split" them?
Ploughing them into a HashMap for retrieval should be relatively simple after that; and my suggestion would be that you use a text file with real words in it to do your testing.

Worry about the "generic" part once you've got a real-life version working. And that will involve reading up on Java generics - and, of course, what <T> means.

HIH

Winston
David Burt
Greenhorn

Joined: Mar 18, 2013
Posts: 10
Thanks I appreciate the help. I went back and rewrote my code into one class, I got rid of the generics, you're right I need to read up on them before trying to use them. here is the code I have right now, it isn't reusable and only really works for this application but it does work which is the important thing right now. the txt file I'm using to test it is a text file of the King James version of the Bible that I downloaded



probably not the most efficient code in the world but it does work. now I must ask this how can I use HashMap and HashSet to do the same thing this code does?
David Burt
Greenhorn

Joined: Mar 18, 2013
Posts: 10
forget about that last part haha I found out that HashMap and HashSet are the same as TreeMap and TreeSet just using different data structures so I just replaced all the Trees with Hash and it works
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7674
    
  19

David Burt wrote:Thanks I appreciate the help. I went back and rewrote my code into one class, I got rid of the generics, you're right I need to read up on them before trying to use them. here is the code I have right now, it isn't reusable and only really works for this application but it does work which is the important thing right now.

Great. And I'm glad that you're using the KJV and not one of those horrible American things that call themselves Bibles (my contempt is purely linguistic and antiquarian, you understand - as the owner of a "Breeches" Bible).

One piece of advice: Get rid of all that timing crap. It has absolutely nothing to do with solving your problem. If you need it, put it somewhere else and call the relevant methods of your class.

Winston
David Burt
Greenhorn

Joined: Mar 18, 2013
Posts: 10
one last thing. If I use this same code for a HashMap and HashSet just change all the instances of TreeMap to HashMap and all the instances of TreeSet to HashSet, how do I get them alphabetized?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7674
    
  19

David Burt wrote:one last thing. If I use this same code for a HashMap and HashSet just change all the instances of TreeMap to HashMap and all the instances of TreeSet to HashSet, how do I get them alphabetized?

You don't. And that wasn't your original question. If you want contents "alphabetized" (by which I assume you mean alphabetical order) and you want to use an existing structure, then you have two choices - ConcurrentSkipListMap / -Set or TreeMap / -Set - and, despite its mouthful of a name, I favour the first; particularly if you have any worries about concurrency.

Alternatively, look at Arrays.sort().

Winston
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14113
    
  16

David Burt wrote:... how do I get them alphabetized?

Sets and Maps are unordered collections. That means that conceptually, they do not store their elements in any particular order. They are like a bag - the items in a bag are not in any particular order.

If you look at the API documentation, you'll see that TreeSet and TreeMap implement interfaces SortedSet and SortedMap - these do store their elements in a specific order. To store objects in a TreeSet or to use them as keys in a TreeMap, the objects must either implement interface Comparable, or you must create the TreeSet or TreeMap by passing a Comparator to the constructor of TreeSet or TreeMap. This is necessary so that the TreeSet or TreeMap can determine how to sort the objects.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using the class methods of something in a TreeMap