这是方案,在我的源列表中,它包含所有Users对象。每个用户对象都有id
,event
和timestamp
。我需要创建一个目标列表,以包含具有每个id的最新时间戳记录的所有用户对象。如下面的例子
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.vincent.object.User;
public class Test {
public static void main(String[] args) throws Exception {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
User u1 = new User("1", "55", dateFormat.parse("2017-10-01 10:11:01.111"));
User u2 = new User("1", "105", dateFormat.parse("2017-10-01 10:11:02.111"));
User u3 = new User("2", "55", dateFormat.parse("2017-10-01 10:11:03.111"));
List<User> sources = new ArrayList<>();
sources.add(u1);
sources.add(u2);
sources.add(u3);
List<User> destination = new ArrayList<>();
// I want my destination array only contains following 2 result:
// u2 and u3 from the source
}
}
我该如何处理?
编辑:这是用户类
import java.util.Date;
public class User {
private String id;
private String reason;
private Date date;
public User(String id, String reason, Date date) {
super();
this.id = id;
this.reason = reason;
this.date = date;
}
// getter setter
}
答案 0 :(得分:3)
您可以使用Java 8流。
public class Test {
public static void main(String[] args) throws Exception {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
User u1 = new User("1", "55", dateFormat.parse("2017-10-01 10:11:01.111"));
User u2 = new User("1", "105", dateFormat.parse("2017-10-01 10:11:02.111"));
User u3 = new User("2", "55", dateFormat.parse("2017-10-01 10:11:03.111"));
User u4 = new User("2", "105", dateFormat.parse("2017-10-01 10:11:04.111"));
List<User> sources = new ArrayList<>();
sources.add(u1);
sources.add(u2);
sources.add(u3);
sources.add(u4);
List<User> destination = sources.stream()
.collect(Collectors.groupingBy(User::getId, Collectors.maxBy(Comparator.comparing(User::getDate))))
.values()
.stream()
.map(o -> o.get())
.collect(Collectors.toList());
System.out.println(destination);
}
}
此处我在{i} {m} {m} gruping并获取max日期的用户。
输出(显然覆盖用户类的toString):
[用户{id =&#39; 1&#39 ;, reason =&#39; 105&#39;,date = Sun Oct 01 10:11:02 BOT 2017},
用户{id =&#39; 2&#39 ;, reason =&#39; 105&#39;,date = Sun Oct 01 10:11:04 BOT 2017}]
答案 1 :(得分:0)
我假设您不需要像在源列表中那样保持用户的顺序。 这个想法如下:
返回地图的密钥集。
public static void main(String[] args)
{
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
User u1 = new User("1", "55", dateFormat.parse("2017-10-01 10:11:01.111"));
User u2 = new User("1", "105", dateFormat.parse("2017-10-01 10:11:02.111"));
User u3 = new User("2", "55", dateFormat.parse("2017-10-01 10:11:03.111"));
List<User> sources = new ArrayList<>();
sources.add(u1);
sources.add(u2);
sources.add(u3);
Set<User> uniqUsers = new HashSet<>();
for(User u : sources)
{
if(uniqUsers.containsKey(u))
{
User oldUser = uniqUsers.get(u);
long oldTimeStamp = uniqUsers.get(u).getDate().getTime();
long currentTimeStamp = u.getDate().getTime();
if(currentTimeStamp > oldTimeStamp)
uniqUsers.remove(oldUser);
uniqUsers.add(u);
}
}
List<User> destination = new ArrayList<>(uniqUsers);
}
必须以这种方式修改User类(为了正确使用HashSet。
public class User
{
private String id;
private String reason;
private Date date;
public User(String id, String reason, Date date)
{
super();
this.id = id;
this.reason = reason;
this.date = date;
}
// getter setter
@Override
public int hashCode()
{
return id.hashCode();
}
@Override
public boolean equals(Object obj)
{
return obj instanceof User && ((User) obj).id.equals(id);
}
}