java程序 自动排序课程,在Java中按值映射自动排序

I need to have an automatically sorted-by-values map in Java - so that It keeps being sorted at any time while I'm adding new key-value pairs or update the value of an existing key-value pair, or even delete some entry.

Please also have in mind that this map is going to be really big (100's of thousands, or even 10's of millions of entries in size).

So basically I'm looking for the following functionality:

Supposed that we had a class 'SortedByValuesMap' that implements the aforementioned functionality

and we have the following code:

SortedByValuesMap sorted_map = new SortedByValuesMap();

sorted_map.put("apples", 4);

sorted_map.put("oranges", 2);

sorted_map.put("bananas", 1);

sorted_map.put("lemons", 3);

sorted_map.put("bananas", 6);

for (String key : sorted_map.keySet()) {

System.out.println(key + ":" + sorted_map.get(key));

}

the output should be:

bananas:6

apples:4

lemons:3

oranges:2

In particular, what's really important for me, is to be able to get the entry with the

lowest value at any time - using a command like:

smallestItem = sorted_map.lastEntry();

which should give me the 'oranges' entry

EDIT: I am a Java newbie so please elaborate a bit in your answers - thanks

EDIT2: This might help: I am using this for counting words (for those who are familiar: n-grams in particular) in huge text files. So I need to build a map where keys are words and values are the frequencies of those words. However, due to limitations (like RAM), I want to keep only the X most frequent words - but you can't know beforehand which are going to be the most frequent words of course. So, the way I thought it might work (as an approximation) is to start counting words and when the map reaches a top-limit (like 1 mil entries) , the least frequent entry will be deleted so as to keep the map's size to 1 mil always.

解决方案

Keep 2 data structures:

A dictionary of words -> count. Just use an ordinary HashMap.

An "array" to keep track of order, such that list[count] holds a Set of words with that count.

I'm writing this as though it were an array as a notational convenience. In fact, you probably don't know an upper bound on the number of occurrences, so you need a resizable data structure. Implement using a Map>. Or, if that uses too much memory, use an ArrayList> (you'll have to test for count == size() - 1, and if so, use add() instead of set(count + 1)).

To increment the number of occurrences for a word (pseudocode):

// assumes data structures are in instance variables dict and arr

public void tally(final String word)

{

final long count = this.dict.get(word) or 0 if absent;

this.dict.put(word, count + 1);

// move word up one place in arr

this.arr[count].remove(word); // This is why we use a Set: for fast deletion here.

this.arr[count + 1].add(word);

}

To iterate over words in order (pseudocode):

for(int count = 0; count < arr.size; count++)

for(final String word : this.arr[count])

process(word, count);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值