开发者

Collection for storing objects with date

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开发者_如何学C 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 ?


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);


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.


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.


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()


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜