获取 TreeMap 的最后 3 个值

发布于 2024-11-27 11:34:16 字数 508 浏览 1 评论 0原文

我有一个日期/字符串 TreeMap,我想循环遍历最后 N 个条目。

TreeMap<Date,String> map = new TreeMap<Date,String>();
map.put(new Date(2011,1,1), "32,1");
map.put(new Date(2011,3,1), "35");
map.put(new Date(2011,4,5), "38,9");
map.put(new Date(2011,8,2), "57!!");

然后我就迷路了。我发现了这个:

NavigableSet<Date> dates = donnees.descendingKeySet();

然后我不知道该怎么说:

for(key in dates and i ; i from 0 to N)
{ do something }

有帮助吗?

I have a Date/String TreeMap and I want to loop through the N last entries.

TreeMap<Date,String> map = new TreeMap<Date,String>();
map.put(new Date(2011,1,1), "32,1");
map.put(new Date(2011,3,1), "35");
map.put(new Date(2011,4,5), "38,9");
map.put(new Date(2011,8,2), "57!!");

Then I'm lost. I've found this :

NavigableSet<Date> dates = donnees.descendingKeySet();

Then I don't know how to say something like :

for(key in dates and i ; i from 0 to N)
{ do something }

Any help ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

终陌 2024-12-04 11:34:16

看起来您想迭代 descendingMap()

private static final int N = 3;
...
int i = 0;
for (Map.Entry entry : map.descendingMap().entrySet()) {
    if (i++ < N) {
        System.out.println(entry);
    }
}

控制台:

Fri Sep 02 11:39:05 EDT 2011=57!!
Thu May 05 11:39:05 EDT 2011=38,9
Fri Apr 01 11:39:05 EDT 2011=35

附录:以下是在默认区域设置中使用 Calendar 的示例:

private static Date createDate(int year, int month, int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, day);
    return calendar.getTime();
}
...
map.put(createDate(2011, 5, 3), "3-Jun-2011");

It looks like you want to iterate though the descendingMap():

private static final int N = 3;
...
int i = 0;
for (Map.Entry entry : map.descendingMap().entrySet()) {
    if (i++ < N) {
        System.out.println(entry);
    }
}

Console:

Fri Sep 02 11:39:05 EDT 2011=57!!
Thu May 05 11:39:05 EDT 2011=38,9
Fri Apr 01 11:39:05 EDT 2011=35

Addendum: Here's an example using Calendar in the default locale:

private static Date createDate(int year, int month, int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, day);
    return calendar.getTime();
}
...
map.put(createDate(2011, 5, 3), "3-Jun-2011");
∞觅青森が 2024-12-04 11:34:16
int i=0;
for(Iterator<Date> it = dates.iterator(); it.hasNext() && i<3;) {
  Date date = it.next();
  doSomething();
  i++
}

或者(等效):

int i=0;
Iterator<Date> it = dates.iterator();
while(i<3 && it.hasNext()) {
  Date date = it.next();
  doSomething();
  i++
}
int i=0;
for(Iterator<Date> it = dates.iterator(); it.hasNext() && i<3;) {
  Date date = it.next();
  doSomething();
  i++
}

Or (equivalent):

int i=0;
Iterator<Date> it = dates.iterator();
while(i<3 && it.hasNext()) {
  Date date = it.next();
  doSomething();
  i++
}
趴在窗边数星星i 2024-12-04 11:34:16

你关心树的状态吗?如果没有,那么您可以执行 pollLastEntry()。这将从树上删除最后一个条目并将其交给您。做3次就完成了。或者您可以将树展平为 arrayList 并只返回最后 3 个元素。

Do you care about the state of the tree? If not, then you could do pollLastEntry(). This will remove the last entry off the tree and give it to you. Do 3 times and you're done. Or you could flatten the tree into an arrayList and just return the last 3 elements.

独木成林 2024-12-04 11:34:16

听起来您只需要循环遍历前三个项目:

    int i=0;
    for(Date date: dates){

        //Do something

        if(i++ > 2){
            break;
        }
    }

Sounds like you just need to loop through the items for the first three:

    int i=0;
    for(Date date: dates){

        //Do something

        if(i++ > 2){
            break;
        }
    }
赤濁 2024-12-04 11:34:16
  • TreeMap 实现 NavigableMap。
  • NavigableMap.descendingMap()。
  • 降序 Map.entrySet().iterator() // 前 3 个条目当然是
    “最后 3”因为它是从后到前的。

您可以只实现 Comparator 并将其传递给 TreeMap,以便“最旧”的日期位于 TreeMap 的头部。

  • TreeMap implements NavigableMap.
  • NavigableMap.descendingMap().
  • descending Map.entrySet().iterator() // the first 3 entries which are of course
    the "last 3" because it is back to front.

You could just implement Comparator and pass it to the TreeMap so the "oldest" dates are at the head of the TreeMap.

£冰雨忧蓝° 2024-12-04 11:34:16

TreeMap 到 Array 并使用 for 循环读取数组,条件为 i < 3

TreeMap to Array and read the array with a for loop with the condition i < 3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文