Java 如何使用Joda Time循环几个月?
以前,我在某个时期的几天之间必须循环,我使用如下循环:Java 如何使用Joda Time循环几个月?,java,loops,jodatime,treemap,Java,Loops,Jodatime,Treemap,以前,我在某个时期的几天之间必须循环,我使用如下循环: for(LocalDate iDate = gv.firstDate; iDate.isBefore(gv.lastDate); iDate = iDate.plusDays(1)) { ... } TreeMap<LocalDate, ArrayList<Email>> dates; Map<LocalDate, ArrayList<Email>> filteredDates =
for(LocalDate iDate = gv.firstDate; iDate.isBefore(gv.lastDate); iDate = iDate.plusDays(1)) {
...
}
TreeMap<LocalDate, ArrayList<Email>> dates;
Map<LocalDate, ArrayList<Email>> filteredDates = Maps.filterKeys(dates, new Predicate<LocalDate>() {
public boolean apply(LocalDate key) {
if (entry.getKey().isBefore(gv.firstDate())) {
return false;
}
if (entry.getKey().isAfter(gv.lastDate())) {
return false;
}
return true;
}
});
// process the emails
processEmails(filteredDates);
现在我有一个TreeMap
如下:
for(LocalDate iDate = gv.firstDate; iDate.isBefore(gv.lastDate); iDate = iDate.plusDays(1)) {
...
}
TreeMap<LocalDate, ArrayList<Email>> dates;
Map<LocalDate, ArrayList<Email>> filteredDates = Maps.filterKeys(dates, new Predicate<LocalDate>() {
public boolean apply(LocalDate key) {
if (entry.getKey().isBefore(gv.firstDate())) {
return false;
}
if (entry.getKey().isAfter(gv.lastDate())) {
return false;
}
return true;
}
});
// process the emails
processEmails(filteredDates);
当您使用树形图时,可以使用
NavigableMap子贴图(K fromKey,
布尔从包含,
K托克伊,
布尔值(包括在内)
返回此贴图中关键帧范围从fromKey到toKey的部分的视图
如果定义间隔的键不能保证在映射中,则可以通过执行以下操作获得仅包含所需值的映射
for(List<Email> emails : dates.tailMap(gv.firstDate).headMap(gv.lastDate).values()) {
for(Email email : emails) {
// do something
}
}
for(列出电子邮件:dates.tailMap(gv.firstDate).headMap(gv.lastDate).values()){
用于(电子邮件:电子邮件){
//做点什么
}
}
在使用树形图时,可以使用
NavigableMap子贴图(K fromKey,
布尔从包含,
K托克伊,
布尔值(包括在内)
返回此贴图中关键帧范围从fromKey到toKey的部分的视图
如果定义间隔的键不能保证在映射中,则可以通过执行以下操作获得仅包含所需值的映射
for(List<Email> emails : dates.tailMap(gv.firstDate).headMap(gv.lastDate).values()) {
for(Email email : emails) {
// do something
}
}
for(列出电子邮件:dates.tailMap(gv.firstDate).headMap(gv.lastDate).values()){
用于(电子邮件:电子邮件){
//做点什么
}
}
您可以执行类似的操作:
for (Map.Entry<LocalDate, ArrayList<Email>> entry : dates) {
if (entry.getKey().isBefore(gv.firstDate())) {
continue;
}
if (entry.getKey().isAfter(gv.lastDate())) {
break;
}
// process the emails
processEmails(entry.getValue());
}
for(Map.Entry:dates){
if(entry.getKey().isBefore(gv.firstDate())){
继续;
}
if(entry.getKey().isAfter(gv.lastDate())){
打破
}
//处理电子邮件
处理电子邮件(entry.getValue());
}
如果你有使用谷歌番石榴的自由,你可以这样做:
for(LocalDate iDate = gv.firstDate; iDate.isBefore(gv.lastDate); iDate = iDate.plusDays(1)) {
...
}
TreeMap<LocalDate, ArrayList<Email>> dates;
Map<LocalDate, ArrayList<Email>> filteredDates = Maps.filterKeys(dates, new Predicate<LocalDate>() {
public boolean apply(LocalDate key) {
if (entry.getKey().isBefore(gv.firstDate())) {
return false;
}
if (entry.getKey().isAfter(gv.lastDate())) {
return false;
}
return true;
}
});
// process the emails
processEmails(filteredDates);
Map filteredDates=Maps.filterKeys(日期,新谓词(){
公共布尔应用(LocalDate键){
if(entry.getKey().isBefore(gv.firstDate())){
返回false;
}
if(entry.getKey().isAfter(gv.lastDate())){
返回false;
}
返回true;
}
});
//处理电子邮件
处理电子邮件(过滤数据);
您可以执行类似的操作:
for (Map.Entry<LocalDate, ArrayList<Email>> entry : dates) {
if (entry.getKey().isBefore(gv.firstDate())) {
continue;
}
if (entry.getKey().isAfter(gv.lastDate())) {
break;
}
// process the emails
processEmails(entry.getValue());
}
for(Map.Entry:dates){
if(entry.getKey().isBefore(gv.firstDate())){
继续;
}
if(entry.getKey().isAfter(gv.lastDate())){
打破
}
//处理电子邮件
处理电子邮件(entry.getValue());
}
如果你有使用谷歌番石榴的自由,你可以这样做:
for(LocalDate iDate = gv.firstDate; iDate.isBefore(gv.lastDate); iDate = iDate.plusDays(1)) {
...
}
TreeMap<LocalDate, ArrayList<Email>> dates;
Map<LocalDate, ArrayList<Email>> filteredDates = Maps.filterKeys(dates, new Predicate<LocalDate>() {
public boolean apply(LocalDate key) {
if (entry.getKey().isBefore(gv.firstDate())) {
return false;
}
if (entry.getKey().isAfter(gv.lastDate())) {
return false;
}
return true;
}
});
// process the emails
processEmails(filteredDates);
Map filteredDates=Maps.filterKeys(日期,新谓词(){
公共布尔应用(LocalDate键){
if(entry.getKey().isBefore(gv.firstDate())){
返回false;
}
if(entry.getKey().isAfter(gv.lastDate())){
返回false;
}
返回true;
}
});
//处理电子邮件
处理电子邮件(过滤数据);
我认为这不能回答问题。这取决于数据集。如果间隔不总是对应于现有的键,则可以使用headMap和tailMap方法。我认为这不起作用,因为gv.firstDate
和gv.lastDate
可能不在键集中。我现在无法检查树映射代码,以查看这些方法是否真的支持映射中不存在的键,但即使没有,floorKey/ceilingKey方法也允许获取地图中存在的对应键。它避免了通过映射中的所有条目,有效地利用了TreeMap相对于HashMap的优势。我认为这并不能回答问题。这取决于数据集。如果间隔不总是对应于现有的键,则可以使用headMap和tailMap方法。我认为这不起作用,因为gv.firstDate
和gv.lastDate
可能不在键集中。我现在无法检查树映射代码,以查看这些方法是否真的支持映射中不存在的键,但即使没有,floorKey/ceilingKey方法也允许获取地图中存在的对应键。它避免了遍历地图中的所有条目,有效地使用树形图优于Hasmap。考虑创建子图(或TraceMult/Head Mmap),最终使用CELIGIGKEY/FLUROKEY方法在该映射中获得间隔的键。通过利用树形图特有的特性,它将避免在每个地图条目上进行迭代。谢谢,番石榴的方式看起来很有希望。我只得到了一堆错误,其中之一是void不能返回值。也应该有一个processEmails功能吗?我怎样才能说出第一次和最后一次约会?gv.firstDate就像2004年的某个地方,gv.lastDate就像2011年的某个地方,我仍然需要每个月循环抛出。@clankill3r我认为纪尧姆使用headMap
和tailMap
的解决方案更有意义:它更简洁,应该更快。关于无效错误,这是我的错误。apply
函数应返回boolean
。你应该自己编写过程邮件。考虑创建一个子图(或尾图/头图),最后使用CELIGIGKIK/FLUROKEY方法在该图中获取键的间隔。通过利用树形图特有的特性,它将避免在每个地图条目上进行迭代。谢谢,番石榴的方式看起来很有希望。我只得到了一堆错误,其中之一是void不能返回值。也应该有一个processEmails功能吗?我怎样才能说出第一次和最后一次约会?gv.firstDate就像2004年的某个地方,gv.lastDate就像2011年的某个地方,我仍然需要每个月循环抛出。@clankill3r我认为纪尧姆使用headMap
和tailMap
的解决方案更有意义:它更简洁,应该更快。关于无效错误,这是我的错误。apply
函数应返回boolean
。你应该自己写电子邮件。