0

I have a list of entries in a HashMap, a String as a Key, and a Long as a Value. I'm trying to check if the Value is greater than a certain number, but I'm not sure how I can get the value and check it.

I know this is incorrect, but this may show what I mean better than I can explain:

long offlineTimeLimit = (offlineTimeLimitConfig*1000);
long limit = (offlineTimeLimit + System.currentTimeMillis());
Long playerName = playerTimeCheck.get(p);

if (playerTimeCheck.containsValue( > limit)) {
}

This gets the amount of time a player is allowed offline, offlimeTimeLimitConfig, then multiplies by 1000 (so it's in milliseconds), then adds the current time. I want to check if any of the values in the hashmap are greater than the current time plus the time limit, then execute a code.

I've been doing research, and I've found other ways to store my data (like a TreeMap) and I'm not sure if that may be a better way to store the data.

Here's the rest of the code:

String p = event.getPlayer().getName();
HashMap<String, Long> playerTimeCheck = new HashMap<String, Long>();
ConfigurationSection section = getConfig().getConfigurationSection("PlayerTime");
long currentTime = System.currentTimeMillis();
String playerCheck = getConfig().getString("PlayerTime");

for (String key : section.getKeys(false)) {
            playerTimeCheck.put(key, section.getLong(key));
        }

Thanks for the help, Colby

kaisernahid
  • 341
  • 2
  • 13
  • You just need to know if any value in the map is greater than some number? I would just use a treemap, sort in descending order, and then check if the value of the first item is > your value or not... – jcampos8782 Aug 20 '13 at 03:14
  • @jcampos8782 treemap is sorted by key, it won't work here. – criszhao Aug 20 '13 at 03:20
  • Oh yea thats right... just use a comparator that sorts by value: http://stackoverflow.com/questions/1448369/how-to-sort-a-treemap-based-on-its-values – jcampos8782 Aug 20 '13 at 03:32

2 Answers2

0
Map<String,Long> playerTimeChcek = new TreeMap<>(new ValueComparator());
...

if(!playerTimeCheck.isEmpty() && playerTimeCheck.firstEntry().getValue() > limit) {
    ...
}

Then just implement the value comparator class as seen How to sort a treemap based on its values?

Community
  • 1
  • 1
jcampos8782
  • 3,956
  • 4
  • 22
  • 23
0

TreeMap is sorted by key, but you can provide a comparator. see How to sort a Map on the values

The code below is convert a map to linkedHashMap sorted by values.

public static <K, V> LinkedHashMap<K, V> sort(Map<K, V> map,
        final Comparator<Entry<K, V>> comparator) {
    List<Entry<K, V>> list = Lists.newArrayList(map.entrySet());

    Collections.sort(list, comparator);
    LinkedHashMap<K, V> sortedMap = new LinkedHashMap<K, V>();
    for (Entry<K, V> entry : list) {
        sortedMap.put(entry.getKey(), entry.getValue());
    }
    return sortedMap;
}

the comparator used here:

Comparator<Entry<String, Integer>> comparator = new Comparator<Entry<String, Integer> () {

                @Override
                public int compare(Entry<String, Integer> o1,
                        Entry<String, Integer> o2) {
                    return o1.getValue().compareTo(o2.getValue());
                }
            }

or just use this:

public static <K, V> LinkedHashMap<K, V> sort(Map<K, V> map) {
    List<Entry<K, V>> list = Lists.newArrayList(map.entrySet());

    Collections.sort(list, new Comparator<Entry<K, V>>() {

        @SuppressWarnings("unchecked")
        @Override
        public int compare(Entry<K, V> o1, Entry<K, V> o2) {
            return ((Comparable<V>) o1.getValue()).compareTo(o2.getValue());
        }
    });
    LinkedHashMap<K, V> sortedMap = new LinkedHashMap<K, V>();
    for (Entry<K, V> entry : list) {
        sortedMap.put(entry.getKey(), entry.getValue());
    }
    return sortedMap;
}
Community
  • 1
  • 1
criszhao
  • 134
  • 6