0

Yes. Exactly the same as for HashMap except that it's already sorted.
Have a look at the methods for TreeMap in the API documentation. You will find all kinds of methods for getting the keys, the values, iterators for looping through them etc. You can use those methods to access your data any way you want so you can send it to a file any way you want.
We all start by learning arrays, but in real life its much more common to use Lists and Maps etc because these are more powerful and easier to work with. You'll learn all about them sooner or later...

0

so i should delete this hashmap codes and change it to tree map?

0

hi james,

i did browse through but no api explained how to output it to text file?any suggestions?

0

its toString() method gives you a beautiful text version (its what you get when you print the TreeMap). Now you have a String its easy to write that to a file.

0

hi,

is hashmap allowing duplicate values? Thanks.

The codes still showing duplicated values.

0

hi,

its said that A Map cannot contain duplicate keys; each key can map to at most one value. but in my case it still allow duplicate values to be added in.

Any suggestions?thanks.

0

A Map does not allow duplicate keys. If you want multiple values for one key you have to wrap them in (eg) a List - which is exactly what we have done here. Ie: each key (bigram ) is unique and points to a single value that is an ArrayList containing all the words with that bigram in them.
Where exactly are your duplicates? I think your current code given a word like "winning" with a bigram on "in" would add "winning" twice to the ArrayList - but that's a piece of logic you can fix for yourself.

0

hi

how do i sort the hashmap values?thanks. using the treemap only?or should i iterate every single string again?thanks

0

hi,

i am implement prevent duplicates for the hashmap by using

//remove duplicates of words
            Set set = new HashSet();
            ArrayList newList = new ArrayList();
            for (Iterator iter = allwords.iterator(); iter.hasNext();) {
                Object element = iter.next();
                if (set.add(element)) {
                    newList.add(element);
                }
            }

            allwords.clear();
            allwords.addAll(newList);

some of the words never get duplicated when added into the hashmap but some words are duplicated and still being added in. any suggestion what is wrong? i lower case everything all the words.

0

hi,

does hash set take words : hello and HeLlo as two different words?it seems to be case..how should i remove the duplicate with such funny pattern HeLLo in hashset?thanks.

0

Yes, they are different Strings with different hash values. If the capitalisation doesn't matter you should convert them all to upper (or lower) case right at the start.

0

its seems very weird..because in one bigram for example "el" the welcome come out once only but in bigram "we" the welcome come out twice

so

el = welcome
we = welcome, welcome

any suggestions as what might be wrong in the reading of the words?

1

Hang on, this is going wrong, and it's my fault.
It's essential that you develop your own skills in analysing problems, researching, reading the API, and trying& debugging solutions. What's happening here is that you are asking me before exhausting those processes, and I'm encouraging that by answering too quickly.
For you own benefit I'm going to limit this to one question/answer per morning or afternoon (excluding follow-ups for clarification only). Sorry if that sounds harsh or unkind, but it's time for you to fly on your own wings.
J

Votes + Comments
Your patience is admirable.
0

alright james.

no problem. :) but can i ask one more question regarding the hashmap and its iteration?seem its very long to remove the duplicates. so is there any better algorithm out there that i can use?thanks..:)

0

I think that you didn't read some posts in this thread and linked tutorials, just for you, with intends stop this thread

links as I posted contains full example FindDups.java plus words saved in File

Edited by mKorbel: n/a

1

one more question regarding the hashmap

So this is this morning's quota answer:
re-read previous posts. I told you repeatedly what to do with your HashMaps, and asked repeatedly for clarification as to where the duplicates were happening (which you never answered properly). There's really nothing I can add regarding your duplicates at this point.
Either use your IDE's step execution / variables tracing, or add lots and lots of print statements to your code so you can see for yourself what's happening.

0

hi james,

Thanks for the feedback. Anyway this is sound stupid but lets say i have

public TextFileIndexer(String INDEX_COLLECTION) throws IOException {
        writer = new IndexWriter(INDEX_COLLECTION, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
    }

public static void searchIndex(String words) throws IOException, ParseException{

        System.out.println("Searching for '" + words + "'"  );
        Directory directory = FSDirectory.getDirectory(how to get variable INDEX_COLLECTION);

how do i get the variable INDEX_COLLECTION from TextFileIndexer and put it into searchIndex class?

1

Hello again.
I don't know what this is about - where does this code come from?
TextFileIndexer and searchIndex are not classes, they are methods. They will be defined inside a (or two) classes, but that info is not in your post.
INDEX_COLLECTION is not a variable, it's a parameter that has to be passed into the TextFileIndexer method. You can't "get it". You have to look for where that method is being called and see what value is being passed in.
Sorry, but without a lot more info on what this is all about I don't know what other help I can give.

ps: This seems like a completely different problem from the ArrayList / TreeMap thing we were discussing before. Maybe you should mark this thread as "solved" and start a new one for this new problem.

Edited by JamesCherrill: n/a

Votes + Comments
such patience. kudos. :D
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.