I would like to find the biggest number in HashSet and HashMap. Say I have the number [22,6763,32,42,33] in my HashSet and I want to find the largest number in my current HashSet..how would i do this? and Same thing for the HashMap as well. I hope you can help me with it. Thank you.
问题:
回答1:
You can use Collections.max(Collection)
to find the maximum element out of any collection.
Similarly, for a HashMap
, you can use the same method on its keySet()
or values()
, depending upon whether you want maximum key, or maximum value.
Also, if you want as such, you can use a TreeSet
and TreeMap
instead, that stores the elements in sorted key order.
回答2:
try
int max = Collections.max(set);
int maxKey = Collections.max(map.keySet());
int maxValue Collections.max(map.values());
回答3:
If you are forced to use a HashSet
/HashMap
, then you have to scan the whole HashSet
/HashMap
in order to find the maximum. Library functions like Collections.max()
will do like this.
If you want O(1)
retrieval of the maximum, and you are allowed to change the type of collection being used, use a sorted set/map (e.g. TreeSet
/TreeMap
).
回答4:
Something like this:
Set<Integer> values = new HashSet<Integer>() {{
add(22);
add(6763);
add(32);
add(42);
add(33);
}};
int maxValue = Integer.MIN_VALUE;
for (int value : values) {
if (value > maxValue) {
maxValue = value;
}
}
And this:
Map<String, Integer> values = new HashMap<String, Integer>() {{
put("0", 22);
put("1", 6763);
put("2", 32);
put("3", 42);
put("4", 33);
}};
int maxValue = Integer.MIN_VALUE;
for (int value : values.values()) {
if (value > maxValue) {
maxValue = value;
}
}
回答5:
In case of TreeMap, if you know the key/values are inserted randomly, the tree will be more or less balanced. Trees become unbalanced, if data is inserted in already sorted order, the capability to quickly find (or insert or delete) a given element is lost. In case of unbalanced tree, it will take time proportional to n, O(n) else O(1).
回答6:
Consider using Apache Commons Math. Here is the API docs.
The class of interest is SummaryStatistics. It works with double
s and computes max, min, mean etc. on the fly (as you add values to it). The data values are not stored in memory, so this class can be used to compute statistics for very large data streams.
回答7:
Here is a simple method which does what you are asking:
public String getMapKeyWithHighestValue(HashMap<String, Integer> map) {
String keyWithHighestVal = "";
// getting the maximum value in the Hashmap
int maxValueInMap = (Collections.max(map.values()));
//iterate through the map to get the key that corresponds to the maximum value in the Hashmap
for (Map.Entry<String, Integer> entry : map.entrySet()) { // Iterate through hashmap
if (entry.getValue() == maxValueInMap) {
keyWithHighestVal = entry.getKey(); // this is the key which has the max value
}
}
return keyWithHighestVal;
}
回答8:
Note : If you want to find the biggest value from Map try maxEntry.get().getValue() instead of maxEntry.get().getKey()
1. Using Stream
public <K, V extends Comparable<V>> V maxUsingStreamAndLambda(Map<K, V> map) {
Optional<Entry<K, V>> maxEntry = map.entrySet()
.stream()
.max((Entry<K, V> e1, Entry<K, V> e2) -> e1.getValue()
.compareTo(e2.getValue())
);
return maxEntry.get().getKey();
}
2. Using Collections.max() with a Lambda Expression
public <K, V extends Comparable<V>> V maxUsingCollectionsMaxAndLambda(Map<K, V> map) {
Entry<K, V> maxEntry = Collections.max(map.entrySet(), (Entry<K, V> e1, Entry<K, V> e2) -> e1.getValue()
.compareTo(e2.getValue()));
return maxEntry.getKey();
}
3. Using Stream with Method Reference
public <K, V extends Comparable<V>> V maxUsingStreamAndMethodReference(Map<K, V> map) {
Optional<Entry<K, V>> maxEntry = map.entrySet()
.stream()
.max(Comparator.comparing(Map.Entry::getValue));
return maxEntry.get()
.getKey();
}
4. Using Collections.max()
public <K, V extends Comparable<V>> V maxUsingCollectionsMax(Map<K, V> map) {
Entry<K, V> maxEntry = Collections.max(map.entrySet(), new Comparator<Entry<K, V>>() {
public int compare(Entry<K, V> e1, Entry<K, V> e2) {
return e1.getValue()
.compareTo(e2.getValue());
}
});
return maxEntry.getKey();
}
5. Using Simple Iteration
public <K, V extends Comparable<V>> V maxUsingIteration(Map<K, V> map) {
Map.Entry<K, V> maxEntry = null;
for (Map.Entry<K, V> entry : map.entrySet()) {
if (maxEntry == null || entry.getValue()
.compareTo(maxEntry.getValue()) > 0) {
maxEntry = entry;
}
}
return maxEntry.getKey();
}