Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!nx02.iad01.newshosting.com!newshosting.com!newspump.sol.net!news-out.readnews.com!transit3.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!53ab2750!not-for-mail From: "Andreas Leitgeb" Subject: SortedMap: getting value for largest key less or equal a given Message-ID: <501D725E.56137.calajapr@time.synchro.net> X-Comment-To: All Newsgroups: comp.lang.java.programmer X-FTN-AREA: COMP.LANG.JAVA.PROGRAMMER X-FTN-MSGID: 1:261/38 bce51978 Content-Type: text/plain; charset=IBM437 Content-Transfer-Encoding: 8bit X-Gateway: time.synchro.net [Synchronet 3.16a-Win32 NewsLink 1.98] Lines: 72 Date: Sat, 04 Aug 2012 19:43:54 GMT NNTP-Posting-Host: 69.21.70.65 X-Complaints-To: news@tds.net X-Trace: newsreading01.news.tds.net 1344109434 69.21.70.65 (Sat, 04 Aug 2012 14:43:54 CDT) NNTP-Posting-Date: Sat, 04 Aug 2012 14:43:54 CDT Organization: tds.net X-Received-Bytes: 3464 Xref: csiph.com comp.lang.java.programmer:17178 From: "Andreas Leitgeb" From: "Andreas Leitgeb" From: "Andreas Leitgeb" From: Andreas Leitgeb I've got an approach like the following, but I'm not entirely happy with it. (see embedded comments) Error checking left out only for brevity-of-post's sake. class StepFunction { SortedMap m_map = new TreeMap(); public void put(K k,V v) { m_map.put(k,v); } // for demo-fill /** @return the value for the largest key in the map that is less OR equal to the given parameter. */ public V value(K k) { // not really correct for generic use. In my usecase, K is // actually Long, so I just add one to k to make it work. return m_map.get(m_map.headMap(k).lastKey()); // I'm a bit unhappy about headMap's "open end", // and also about the lack of some method like // lastEntry() or lastKeysValue() in SortedMap, // requiring one to look up the lastKey in the map, // although the map had "its finger on it" just before. // Did I miss something simple and obvious? } // demo-helper void checkVal(K k, V v) { System.out.println( map.value(k) + " should be " + v); } public static void main(String[] args) { StepFunction sf = new StepFunction<>() sf.put(Integer.MIN_VALUE, -1.0); sf.put(0, 0.0); sf.put(2, 1.0); sf.checkVal(-1 , -1.0); sf.checkVal( 0 , 0.0); sf.checkVal( 1 , 0.0); sf.checkVal( 2 , 1.0); sf.checkVal(Integer.MAX_VALUE , 1.0); } } PS: No need to offer "solutions" involving linear search. I could have come up with one, myself, if I wanted one. -+- BBBS/Li6 v4.10 Dada-1 + Origin: Prism bbs (1:261/38) -+- Synchronet 3.16a-Win32 NewsLink 1.98 Time Warp of the Future BBS - telnet://time.synchro.net:24 -+- BBBS/Li6 v4.10 Dada-1 + Origin: Prism bbs (1:261/38) -+- Synchronet 3.16a-Win32 NewsLink 1.98 Time Warp of the Future BBS - telnet://time.synchro.net:24 -+- BBBS/Li6 v4.10 Dada-1 + Origin: Prism bbs (1:261/38) -+- Synchronet 3.16a-Win32 NewsLink 1.98 Time Warp of the Future BBS - telnet://time.synchro.net:24 --- BBBS/Li6 v4.10 Dada-1 * Origin: Prism bbs (1:261/38) --- Synchronet 3.16a-Win32 NewsLink 1.98 Time Warp of the Future BBS - telnet://time.synchro.net:24