如何在java中从给定的映射值中查找最新日期
我有一个带有以下值的散列映射,这些值是我用字符串数据类型date得到的。我想比较map中所有可用的日期,只提取一个最近日期的键值 我想与值而不是键进行比较 我已经包括了下面的代码如何在java中从给定的映射值中查找最新日期,java,date,lambda,java-8,java-stream,Java,Date,Lambda,Java 8,Java Stream,我有一个带有以下值的散列映射,这些值是我用字符串数据类型date得到的。我想比较map中所有可用的日期,只提取一个最近日期的键值 我想与值而不是键进行比较 我已经包括了下面的代码 import java.util.HashMap; import java.util.Map; public class Test { public static void main(String[] args) { Map<String, String> map = new Hash
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("1", "1999-01-01");
map.put("2", "2013-10-11");
map.put("3", "2011-02-20");
map.put("4", "2014-09-09");
map.forEach((k, v) -> System.out.println("Key : " + k + " Value : " + v));
}
}
import java.util.HashMap;
导入java.util.Map;
公开课考试{
公共静态void main(字符串[]args){
Map Map=newhashmap();
地图放置(“1”、“1999-01-01”);
地图放置(“2”、“2013-10-11”);
地图放置(“3”、“2011-02-20”);
地图放置(“4”、“2014-09-09”);
map.forEach((k,v)->System.out.println(“键:+k+”值:+v));
}
}
这一次的预期输出是:
键4值2014-09-09这应提供与其他日期相比的最新(又名最大)日期
String max=map.values().stream().reduce(“0000-00-00”,
(a,b)->b.与(a)>=0?b相比
:a);
如果还需要密钥,请执行此操作并返回Map.Entry。需要Java 9+
Entry=
map.entrySet().stream().reduce(map.entry(“0”,“0000-00-00”),
(a,b)->b.getValue().compareTo(a.getValue())>=0?b
:a);
System.out.println(ent.getKey()+“->”ent.getValue());
这假定您的地图不是空的。如果为空,则返回null。使用Java8+
Entry ent=map.entrySet().stream().reduce(
(a,b)->b.getValue().compareTo(a.getValue())>=0?b
:a).orElseGet(()->null);
这应该行得通
Optional<Map.Entry<String, String>> result = map.entrySet().stream().max(Comparator.comparing(Map.Entry::getValue));
System.out.println(result);
Optional result=map.entrySet().stream().max(Comparator.comparing(map.Entry::getValue));
系统输出打印项次(结果);
输出是可选的[4=2014-09-09]与
Entry max=Collections.max(map.entrySet(),map.Entry.comparingByValue());
或
Entry max=Collections.max(map.entrySet(),
新比较器(){
@凌驾
公共整数比较(条目e1、条目e2){
返回LocalDate.parse(e1.getValue()).compareTo(LocalDate.parse(e2.getValue());
}
});
乍一看,使用字符串文字来表示日期不是一个好方法,这会使它更加脆弱和容易出错。首先,您宁愿使用LocalDate
。但是,假设您对该数据格式没有任何控制权(例如,它来自另一个第三方系统),我们仍然可以设计一种解决当前问题的方法。这是它的样子
Entry<String, String> maxEntry = map.entrySet().stream()
.max(Comparator.comparing(e -> LocalDate.parse(e.getValue())))
.orElseThrow(IllegalArgumentException::new);
如果您只需要像上面建议的那样省去日期的字符串表示,那么您就可以使上面的解决方案更加简单和简洁
Entry<String, LocalDate> maxEntry = map.entrySet().stream()
.max(Map.Entry.comparingByValue())
.orElseThrow(IllegalArgumentException::new);
Entry maxEntry=map.entrySet().stream()
.max(Map.Entry.comparingByValue())
.orelsetrow(IllegalArgumentException::new);
您可以将字符串解析为LocalDate
并按相反顺序排序
Entry<String, String> res = map.entrySet()
.stream()
.sorted(Comparator.comparing((Entry<String, String> entry)->LocalDate.parse(entry.getValue())).reversed())
.findFirst()
.orElse(null); // if not present return null or empty Entry
Entry res=map.entrySet()
.stream()
.sorted(Comparator.comparing((Entry)->LocalDate.parse(Entry.getValue())).reversed())
.findFirst()
.orElse(空);//如果不存在,则返回null或空条目
您可以执行以下任一操作:
import java.util.Map.Entry;
Entry<String, String> maxEntry = map.entrySet()
.stream()
.max(Entry.comparingByValue());
.orElseThrow(NoSuchElementException::new);
import java.util.Map.Entry;
Entry maxEntry=map.entrySet()
.stream()
.max(Entry.comparingByValue());
.orelsetrow(NoSuchElementException::new);
或:
Entry max=Collections.max(map.entrySet(),Entry.comparingByValue());
两者将产生相同的结果。他需要的是最大值,而不是键OK,请尝试设计错误。不要将日期作为字符串放入地图中。放置
LocalDate
对象。其余的代码可能是相同的,保存映射类型的声明。相关:(在堆栈交换代码审查中)。如何获得密钥?请参阅我的添加。我以为你只是想要价值。这是我的错。我在你的新代码-Map.entry(“0”,“0000-00-00”)-中发现了一个错误-它说类型映射的方法Map.entry未定义。你必须运行比Java 9更旧的东西(添加时)。我会提供一个替代方案并解释它。是的,我正在使用Java 8谢谢,亲爱的。。但是我正在查找result中的键Map.Entry.comparingByValue()
很漂亮(从Java8开始)。Map.Entry.comparingByValue()
很漂亮(从Java8开始)。
Entry<String, LocalDate> maxEntry = map.entrySet().stream()
.max(Map.Entry.comparingByValue())
.orElseThrow(IllegalArgumentException::new);
Entry<String, String> res = map.entrySet()
.stream()
.sorted(Comparator.comparing((Entry<String, String> entry)->LocalDate.parse(entry.getValue())).reversed())
.findFirst()
.orElse(null); // if not present return null or empty Entry
import java.util.Map.Entry;
Entry<String, String> maxEntry = map.entrySet()
.stream()
.max(Entry.comparingByValue());
.orElseThrow(NoSuchElementException::new);
Entry<String, String> max = Collections.max(map.entrySet(), Entry.comparingByValue());