Java 从将对象存储为值的hashmap获取最小值
我想从HashMap中的数据中获取对象中某个值的最小值,如果可能的话,无需执行for循环等操作,以获得我当前拥有的:Java 从将对象存储为值的hashmap获取最小值,java,object,hashmap,Java,Object,Hashmap,我想从HashMap中的数据中获取对象中某个值的最小值,如果可能的话,无需执行for循环等操作,以获得我当前拥有的: HashMap<String, Country> biggestCountries = new HashMap<String, Country>(); 到目前为止,我已经尝试使用此方法获取最小值,但仅当我用整数替换对象国家时,它才起作用: Collections.min(biggestCountries.entrySet(), Map.Entry.com
HashMap<String, Country> biggestCountries = new HashMap<String, Country>();
到目前为止,我已经尝试使用此方法获取最小值,但仅当我用整数替换对象国家时,它才起作用:
Collections.min(biggestCountries.entrySet(), Map.Entry.comparingByValue()).getKey();
有人可能知道一个好方法来实现这一点吗?您可以创建一个基于
区域进行排序的比较器
,并从映射中获取条目
Comparator<Map.Entry<String, Country>> comp =
Comparator.comparing((Entry<String, Country> entry)->entry.getValue().getArea());
使用流
Entry<String, Country> entry = biggestCountries.entrySet()
.stream()
.min(comp)
.orElse(null);
Country minCountry = biggestCountries.values()
.stream()
.min(Comparator.comparing(Country::getArea))
.orElse(null);
或使用Collections.min
Collections.min(biggestCountries.entrySet(),comp);
Country minCountry = Collections.min(biggestCountries.values(),
Comparator.comparing(Country::getArea));
您可以使用以下值和流尝试类似的操作:
Country country = biggestCountries.values()
.stream()
.sorted(Comparator.comparing(c -> c.getArea()))
.findFirst()
.orElse(null);
这将按区域升序对值进行排序,并返回找到的第一个值,如果没有,则返回null
正如其他人所建议的那样:
Country country = biggestCountries.values()
.stream()
.min(Comparator.comparing(c -> c.getArea()))
.orElse(null);
它将返回找到的最小值或null。这是获取具有最小面积的国家/地区的完整代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionMinExample {
public static void main(String args[]){
List<Country> countries = new ArrayList<>();
countries.add(new Country("IN", 25000));
countries.add(new Country("US", 45000));
countries.add(new Country("SL", 14000));
countries.add(new Country("CH", 24000));
Country minAreaCountry = Collections.min(countries, (c1, c2) -> c1.getArea().compareTo(c2.getArea()));
System.out.println("Country with minimum Area: "+minAreaCountry.getName());
}
}
class Country{
private String name;
private Integer area;
public Country(String name, Integer area){
this.name = name;
this.area = area;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getArea() {
return area;
}
public void setArea(Integer salary) {
this.area = salary;
}
}
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
公共类集合Mine示例{
公共静态void main(字符串参数[]){
列表国家=新的ArrayList();
添加(新国家(“IN”,25000));
增加(新国家(“美国”),45000);
添加(新国家(“SL”,14000));
添加(新国家(“CH”,24000));
Country minAreaCountry=Collections.min(国家,(c1,c2)->c1.getArea().compareTo(c2.getArea());
System.out.println(“具有最小面积的国家:+minAreaCountry.getName());
}
}
阶级国家{
私有字符串名称;
私人整数区;
公共国家(字符串名称,整数区域){
this.name=名称;
这个面积=面积;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共整数getArea(){
返回区;
}
公共无效设置区域(整数工资){
该区域=工资;
}
}
或通过以下方式直接获取最小值:.values().stream().min(Comparator.comparing(Country::getArea)).orElse(null)代码>
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionMinExample {
public static void main(String args[]){
List<Country> countries = new ArrayList<>();
countries.add(new Country("IN", 25000));
countries.add(new Country("US", 45000));
countries.add(new Country("SL", 14000));
countries.add(new Country("CH", 24000));
Country minAreaCountry = Collections.min(countries, (c1, c2) -> c1.getArea().compareTo(c2.getArea()));
System.out.println("Country with minimum Area: "+minAreaCountry.getName());
}
}
class Country{
private String name;
private Integer area;
public Country(String name, Integer area){
this.name = name;
this.area = area;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getArea() {
return area;
}
public void setArea(Integer salary) {
this.area = salary;
}
}