我有一些带有日期参数的对象。什么集合最适合存储它们并稍后查询具有特定日期的一个或多个对象? (比如以 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 ?
发布评论
评论(5)
如果存在具有相同日期的多个值,则可能是
Map
或Map>
。然后你会添加它们,如下所示:
根据评论进行编辑:
对于列表版本,我使用类似的内容(未经记忆测试......但很确定它是正确的):
Probably a
Map<Date, WhateverTypeYouWant>
orMap<Date, List<WhateverTypeYouWant>>
if there are multpile values with the same date.Then you would add them something like this:
Edit based on the comment:
For the List version I use something like this (untested from memory... but pretty sure it is right):
听起来像
Map< /code>
(或者
Map
如果您愿意的话)就可以完成这项工作。地图有不同的风格,最常用的是
哈希映射
。Sounds like a
Map<Date, YourObject>
(orMap<String, YourObject>
if you prefer so) would do the job.Maps come in different flavours, the most generally used is
HashMap
.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.
如果您像其他人建议的那样使用
Map
,您将只能进行精确搜索,并且如果您更改 SomeObject 中的日期,您将需要手动更新地图。如果您选择使用Map>
,则工作量会更大。而是使用
List
并使用Collections.binarySearch()
。这就需要对Collection进行排序,并且需要编写自定义的java.util.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 useMap<Date, List<SomeObject>>
.Instead use
List<SomeObject>
and useCollections.binarySearch()
. This requires Collection to be sorted and you need to write custom java.util.Comparator.then use it like this (preferably wrap it in helper method):
Given this comparator, binarySearch() will only search by Date not by other properties of SomeObject.
Also look for meaning of
resultIndex
in Collections.binarySearch()在集合 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.