tags:

views:

73

answers:

2

I've been able to read a four column text file into a hashmap and get it to write to a output file. However, I need to get the second column(distinct values) into a hashset and write to the output file. I've been able to create the hashset, but it is grabbing everything and not sorting. By the way I'm new, so please take this into consideration when you answer. Thanks

+4  A: 

Neither HashSet nor HashMap are meant to sort. They're fundamentally unsorted data structures. You should use an implementation of SortedSet, such as TreeSet.

Jon Skeet
+1 for being correct (and technically, 35 seconds before my now deleted answer)
Erick Robertson
I've implemented a treeset but i am still getting everything and the sort doesn't seem to be taking place. I think my placement of the treeset or implementation maybe the issue. Sample below of how I'm using it.
@user442471: Edit your *question* to show the code, please.
Jon Skeet
+1  A: 

Some guesses, related to mr Skeets answer and your apparent confusion...

Are you sure you are not inserting the whole line in the TreeSet? If you are going to use ONLY the second column, you will need to split() the strings (representing the lines) into columns - that's nothing that's done automatically.

Also, If you are actually trying to sort the whole file using the second column as key, You will need a TreeMap instead, and use the 2:nd column as key, and the whole line as data. But that won't solve the splitting, it only to keep the relation between the line and the key.

Edit: Here is some terminology for you, you might need it.

You have a Set. It's a collection of other objects - like String. You add other objects to it, and then you can fetch all objects in it by iterating through the set. Adding is done through the method add()and iterating can be done using the enhanced for loop syntax or using the iterator() method.

The set doesn't "grab" or "take" stuff; You add something to the set - in this case a String - Not an array of Strings which is written as String[]

(Its apparently possible to add array to a TreeSet (they are objects too) , but the order is not related to the contents of the String. Maybe thats what you are doing.)

String key = splittedLine[1]; // 2:nd element

"The second element of the keys" doesn't make sense at all. And what's the duplicates you're talking about. (note the correct use of apostrophes... :-)

KarlP
I am splitting the strings and taking only the second element. It seems when i use the treeset it is taking the second element of each of the key's. I think this is why i'm seeing the duplicates. I really just want the value once. As you stated, it is keeping the relation between the line and the key. This is how i'm splitting below. Pattern p = Pattern.compile("[\\s]+"); String[] str = p.split(strLine);
@user442471: What You are saying doesn't really make sense... Edit your question to show the code, and we'll try to explain whats going on.
KarlP