简体   繁体   中英

Opposite of RangeMap data structure

I'm looking for a Java data structure S<K extends Comparable<?>, V> which allows the following operations:

  • put(K key, V value) adds a value (for example three items sold ) to a key ( on October the first )
  • Collection<V> get(Range<K> range) meaning get me all the items sold between August and November

I think I can misuse a SortedMap<K, V> but maybe you people know a better alternative.

An example instantiation would be new MyDataStructure<Instant, Integer> to describe the stock sold on various dates.

Sample answer what i understood, object inside Map can be tweaked.

1.) A scalable concurrent ConcurrentNavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

2.) subMap , (K fromKey,boolean fromInclusive,K toKey,boolean toInclusive)

SubMap will create a map based on given dates, whether to include them or not.

public static void main(String[] args) throws ParseException {
        ConcurrentSkipListMap<Date, Integer> myMap = new ConcurrentSkipListMap<Date, Integer>();

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -7);
        Date myDate1 = cal.getTime();
        System.out.println("Date1 = "+myDate1);
        myMap.put(myDate1, 10);

        Calendar cal1 = Calendar.getInstance();
        cal1.add(Calendar.DATE, -4);
        Date myDate2 = cal1.getTime();
        System.out.println("Date2 = "+myDate2);
        myMap.put(myDate2, 5);

        Date myDate3 = new Date();
        System.out.println("Date 3 "+ myDate3);
        myMap.put(myDate3, 2);

        SortedMap<Date, Integer> outputMap = myMap.subMap(myDate1, true,myDate3, false);
        System.out.println("Output Map from "+myDate1 +" to "+myDate2 + "is = "+outputMap);

    }

Output

Date1 = Thu Oct 01 16:48:48 IST 2015
Date2 = Sun Oct 04 16:48:48 IST 2015
Date 3 Thu Oct 08 16:48:48 IST 2015
Output Map from Thu Oct 01 16:48:48 IST 2015 to Sun Oct 04 16:48:48 IST 2015is = {Thu Oct 01 16:48:48 IST 2015=10, Sun Oct 04 16:48:48 IST 2015=5}

u may use nested map.

map<Object,map<object,object>>. 

This Structure fit in ur situation as follows

map<monthnameobject,map<itemcode(or sequence),itemvalue>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM