开发者

Help need in creating a hashset from a hashmap

开发者 https://www.devze.com 2023-01-15 12:56 出处:网络
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 t

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


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


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... :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消