anthonyms
anthonyms

Reputation: 948

What is the best way of using two comparators?

I currently have the following structure used to get OHLC data over an interval

class MarketDataItem{
    ....

    static class DateComparator implements Comparator<MarketDataItem>{

    }

    static class PriceComparator implements Comparator<MarketDataItem> {

    }   
}

class MarketDataGroup{
    private TreeSet<MarketDataItem> sortedByDate = Sets.newTreeSet(new MarketDataItem.DateComparator());
    private TreeSet<MarketDataItem> sortedByPrice = Sets.newTreeSet(new MarketDataItem.PriceComparator());  

}

Is it better/nicer/faster/lessmemory to have the comparators in the marketDataItem or in the marketDataGroup?

Upvotes: 4

Views: 181

Answers (2)

pauljwilliams
pauljwilliams

Reputation: 19225

Nope. I think your implementation is optimal. We've faced similar requirements and done it this way.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500815

It makes no difference - they're static nested classes, so it's as if they were completely independent types. There are differences to accessibility from various classes, but the efficiency should be the same.

Upvotes: 2

Related Questions