Java 8 and enum, very concise solution


  • 5
    class LogSystem {
        Map<Integer,String> map = new HashMap<>();
    
        public  enum Index {
            Year(4), Month(7), Day(10), Hour(13), Minute(16), Second(19);
            int idx;   Index(int i) {this.idx = i;}
        }
    
        public void put(int id, String timestamp) { map.put(id,timestamp); }
    
        public List<Integer> retrieve(final String s, final String e, String gra) {
            Function<String, String> fn = str -> str.substring(0, Index.valueOf(gra).idx);
            return map.entrySet().stream().filter(
                    x -> fn.apply(x.getValue()).compareTo(fn.apply(s)) >= 0 && fn.apply(x.getValue()).compareTo(fn.apply(e)) <= 0)
                    .collect(mapping(v -> v.getKey(), toList()));
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.