用于存储带有日期的对象的集合

发布于 2024-09-29 00:43:12 字数 470 浏览 2 评论 0 原文

我有一些带有日期参数的对象。什么集合最适合存储它们并稍后查询具有特定日期的一个或多个对象? (比如以 String 或 java.util.Date 格式给出)?


编辑:

我试图使用 TofuBear 的解决方案,但无法使其工作。假设我正在使用对象列表和 Date 对象调用我的函数(返回 Map)。接下来怎么办?我尝试了不同的方法,但由于 NetBeans 的错误,一切都变成了血红色:

public Map<Date, List<Person>> createDateList(Date date, List<Person> list){
    Map<Date, List<Person>> map = null;
}

但这并不能解决查询问题,因为我只是用一个对象创建一张地图。我需要在地图中列出所有对象(具有日期字段)及其日期。我的想法正确吗?

I have some objects with Date parameters. What collection will be best for storing them and later querying for object/objects with particular date ? (like given as a String or java.util.Date format) ?


EDIT:

I was trying to use TofuBear's solution, but cannot make it work. let's say I am calling my function (which returns Map) with a list of objects, and Date object. What next ? I was trying different methods but everything is just bloody red from NetBeans's errors:

public Map<Date, List<Person>> createDateList(Date date, List<Person> list){
    Map<Date, List<Person>> map = null;
}

This however does not solve problem of querying, cuz I'm just creating a map with one object. I need to have a list of all objects (which have Date field) and their dates in a map. Am I thinking correctly ?

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

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

发布评论

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

评论(5

日记撕了你也走了 2024-10-06 00:43:12

如果存在具有相同日期的多个值,则可能是 MapMap>

然后你会添加它们,如下所示:

map.put(object.getDate(), object);

根据评论进行编辑:

对于列表版本,我使用类似的内容(未经记忆测试......但很确定它是正确的):

List<WhateverTypeYouWant> list;

list = map.get(object.getDate())

if(list == null)
{
    list = new ArrayList<WhateverTypeYouWant>();
    map.put(object.getDate(), list);
}

list.add(object);

Probably a Map<Date, WhateverTypeYouWant> or Map<Date, List<WhateverTypeYouWant>> if there are multpile values with the same date.

Then you would add them something like this:

map.put(object.getDate(), object);

Edit based on the comment:

For the List version I use something like this (untested from memory... but pretty sure it is right):

List<WhateverTypeYouWant> list;

list = map.get(object.getDate())

if(list == null)
{
    list = new ArrayList<WhateverTypeYouWant>();
    map.put(object.getDate(), list);
}

list.add(object);
叹倦 2024-10-06 00:43:12

听起来像 Map< /code> (或者 Map 如果您愿意的话)就可以完成这项工作。

地图有不同的风格,最常用的是 哈希映射

Sounds like a Map<Date, YourObject> (or Map<String, YourObject> if you prefer so) would do the job.

Maps come in different flavours, the most generally used is HashMap.

萌梦深 2024-10-06 00:43:12

Map,正如其他人所说,但如果您不仅仅对获取与给定日期完全匹配的条目感兴趣,那么您可能需要考虑使用 NavigableMap。如果没有完全匹配的内容,可导航地图将允许您获取与您正在搜索的内容接近的条目。

Map<Date, Other>, as others have said, but if you are interested in more than getting an entry that matches a given date exactly then you would want to look into using a NavigableMap. A navigable map will allow you to get entries that are close to what you are searching for if nothing matches exactly.

筱武穆 2024-10-06 00:43:12

如果您像其他人建议的那样使用 Map ,您将只能进行精确搜索,并且如果您更改 SomeObject 中的日期,您将需要手动更新地图。如果您选择使用 Map>,则工作量会更大。

而是使用 List 并使用 Collections.binarySearch()。这就需要对Collection进行排序,并且需要编写自定义的java.util.Comparator。

private class SomeObjectComparator implements Comparator<SomeObject> {

    @Override
    public int compare(SomeObject o1, SomeObject o2) {
        // this breaks equality rule for Set
        // do not use in Sets
        return o1.date.compareTo(o2.date);
    }
}

然后像这样使用它(最好将其包装在辅助方法中):

List<SomeObject> someList =  new ArrayList<SomeObject>
Comparator comparator = new SomeObjectComparator();
Collections.sort(someList, comparator);
int resultIndex = Collections.binarySearch(someList, someSearchedObject, comparator)

给定此比较器,binarySearch() 将仅按 Date 搜索,而不按 SomeObject 的其他属性搜索。

还要在 Collections.binarySearch()

If you use Map<Date, SomeObject> like other have suggested, you will only be able to do exact searches and in case you change Date inside SomeObject you'll need to manually update the Map. Even more work if you choose to use Map<Date, List<SomeObject>>.

Instead use List<SomeObject> and use Collections.binarySearch(). This requires Collection to be sorted and you need to write custom java.util.Comparator.

private class SomeObjectComparator implements Comparator<SomeObject> {

    @Override
    public int compare(SomeObject o1, SomeObject o2) {
        // this breaks equality rule for Set
        // do not use in Sets
        return o1.date.compareTo(o2.date);
    }
}

then use it like this (preferably wrap it in helper method):

List<SomeObject> someList =  new ArrayList<SomeObject>
Comparator comparator = new SomeObjectComparator();
Collections.sort(someList, comparator);
int resultIndex = Collections.binarySearch(someList, someSearchedObject, comparator)

Given this comparator, binarySearch() will only search by Date not by other properties of SomeObject.

Also look for meaning of resultIndex in Collections.binarySearch()

汐鸠 2024-10-06 00:43:12

在集合 API 中,存储了需要对原始数据类型元素进行装箱的属性(如列表、链表)的引用地址,在其对应的包装类类型对象中,并且一个对象会自动升级为对象类型类(对象类是每个类的超类)并且此引用对象可以传递给任何其他程序(在相同或不同的包内),并在自己的日期进行多次切除(例如仅读取,删除,更新,仅读取一次) ,根据要求在列表中添加更多元素)。对于哪个参考对象仅对避免多重问题有用。

In side the collection API stored the reference addresses of the attribute (of like list, linked List ) for which required the boxing on primitive data type element, in its correspondence Wrapper class type object and that one object are automatically up-castes to Object type class (Object class is the Super class of every Class) And this reference object can pass for any other program (Inside same or different Package) with the Multiple Resection on the OWN date (Like only reading, removing, Updating, Reading only one time, adding more element inside list) according with requirement. for which reference object is only usefull to avoid multiple problems.

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