如何在创建30天后从arrayList获取对象(How to get an object from the arrayList after 30 days of created)

编程入门 行业动态 更新时间:2024-10-27 22:33:21
如何在创建30天后从arrayList获取对象(How to get an object from the arrayList after 30 days of created)

我有一个包含一堆对象的数组列表,我为每个对象设置了一个日期字段,以便可以识别该对象的创建日期。 我希望在创建对象30天后显示该对象。

我试过了:

Object o2 = new Object("Mark", 23, "12/24/2015"); System.out.println(o2); //supposing that this is my object SimpleDateFormat formatDate = new SimpleDateFormat("MM/dd/yyyy"); // and this is a Simple format date. try { Date objDate = formatDate.parse(o2.getDate()); System.out.println(objDate); //And here i take form a string date into a date. } catch (ParseException ex) { System.out.println(ex.getMessage()); }

但我不知道如何在创建对象30天后显示它。

I have a array list with a bunch of objects, and I set a date field for each object so it can be recognize on what date that object was created. I want to display that object 30 days after it was created.

I tried:

Object o2 = new Object("Mark", 23, "12/24/2015"); System.out.println(o2); //supposing that this is my object SimpleDateFormat formatDate = new SimpleDateFormat("MM/dd/yyyy"); // and this is a Simple format date. try { Date objDate = formatDate.parse(o2.getDate()); System.out.println(objDate); //And here i take form a string date into a date. } catch (ParseException ex) { System.out.println(ex.getMessage()); }

But I don't know how display the object 30 days after it was created.

最满意答案

您不应该使用arraylist来实现此需求。 但是PriorityQueue会这样做。 您可以使用PriorityQueue存储您的对象,您的对象需要实现一个Comparator,它通过createdTime进行比较。

public class MyObject { private String name; private int id; private long time; } public static Comparator<MyObject> COMPARE = new Comparator<MyObject>() { @Override public int compare(MyObject o1, MyObject o2) { return (int) (o1.time - o2.time); } }; public PriorityQueue<MyObject> queue = new PriorityQueue<>(COMPARE);

然后创建一个任务以从队列中轮询对象,如果创建了30天则显示该对象,否则再次添加到队列。

private class MyTask implements Runnable { @Override public void run() { while (true) { MyObject object = queue.poll(); if (object.time + DateUtils.MILLIS_PER_DAY * 30 > System.currentTimeMillis()) { display(object); } else { queue.add(object); } } } }

您还可以使用redis的zadd和zrangeByScoreWithScores来获取按时间计算的socre数据。 所以,不需要轮询和添加多次。

You should not use arraylist to implement this demand. However PriorityQueue does. You can use PriorityQueue to store your object, your object need implement a Comparator which compare by their createdTime.

public class MyObject { private String name; private int id; private long time; } public static Comparator<MyObject> COMPARE = new Comparator<MyObject>() { @Override public int compare(MyObject o1, MyObject o2) { return (int) (o1.time - o2.time); } }; public PriorityQueue<MyObject> queue = new PriorityQueue<>(COMPARE);

then create a task to poll object from queue, display the object if created 30 days, or else add to queue again.

private class MyTask implements Runnable { @Override public void run() { while (true) { MyObject object = queue.poll(); if (object.time + DateUtils.MILLIS_PER_DAY * 30 > System.currentTimeMillis()) { display(object); } else { queue.add(object); } } } }

You also can use redis's zadd and zrangeByScoreWithScores to get data with socre computed by time. So that, do not need to poll and add multi times.

更多推荐

本文发布于:2023-07-14 17:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106061.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:天后   对象   如何在   arrayList   days

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!