views:

60

answers:

4

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 ?

A: 

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.

Péter Török
+1  A: 

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);
TofuBeer
@mastodon : if you use List<WhateverTypeYouWant> don't forgot to create thus lists.
Stas
could you explain how to use it ? I'm struggling and nothing good comes of it.
mastodon
A: 

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.

mlaw
A: 

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

Peter Knego