与依赖于其他项目在Java stucture删除条目
问题描述:
假设我有以下结构:(以JSON为例)与依赖于其他项目在Java stucture删除条目
...
{event: Web, action:Video, timestamp:1320},
{event: Web, action:Play, timestamp: 1320},
{event: Web, action:Download, timestamp: 1320},
{event: Web, action:Play, timestamp: 1321}
...
现在我想重复的结构,并删除与“行动的所有行:播放“,其中的动作是:下载具有相同的时间戳。
那么结果会是这样的:
...
{event: Web, action:Video, timestamp:1320},
{event: Web, action:Download, timestamp: 1320},
{event: Web, action:Play, timestamp: 1321}
...
我不确定,我是谁应该在Java中实现这一点。我的问题是在这里:我应该使用type(List,Map,...)和Algo来解决这个问题吗?
答
您需要定义一个变量一类像时间戳
下面是一个例子:
static class MediaPlayer{
String event;
String action;
int timestamp;
public MediaPlayer(String event, String action,int timestamp){
this.event=event;
this.action=action;
this.timestamp=timestamp;
}
public String getEvent(){
return event;
}
public String getAction(){
return action;
}
public int getTimeStamp(){
return timestamp;
}
}
然后在主要的类名对象您可以创建一个列表。遍历它们并使用removeAll()方法删除它们。
public static void main(String args[]){
List<MediaPlayer> mp=new ArrayList<>();
List<MediaPlayer> remove=new ArrayList<>();//I have made this for demostration purpose that you could even collect these objects somewhere and do something with them!
mp.add(new MediaPlayer("web","Video",1320));
mp.add(new MediaPlayer("web","Play",1320));
mp.add(new MediaPlayer("web","Download",1320));
mp.add(new MediaPlayer("web","Play",1321));
mp.forEach((it)->{
System.out.println("Event: "+it.getEvent()+" Action: "+it.getAction()+" TimeStamp: "+it.getTimeStamp());
});
Iterator<MediaPlayer> iter = mp.iterator();
Iterator<MediaPlayer> iter1 = mp.iterator();
for(MediaPlayer it:mp){
for(MediaPlayer itr:mp){
if(it.getAction().equals("Download") && it.getTimeStamp()==itr.getTimeStamp() && itr.getAction().equals("Play")){
remove.add(itr);// I add the objects to be removed in remove list first. This way I will even have the list of deleted objects
}
}
}
mp.removeAll(remove);//Then finally I remove them from main list
System.out.println("\n");
mp.forEach((it)->{
System.out.println("Event: "+it.getEvent()+" Action: "+it.getAction()+" TimeStamp: "+it.getTimeStamp());
});
}
你有什么试过的?这些“结构”到底是什么?他们是上课吗? – UnholySheep
这就是问题,我应该在那里使用哪种结构? – domi13
我只有一个建议:边做边学。如果你不知道如何从这个问题开始,那么首先找到一个更容易解决的问题。 – EasterBunnyBugSmasher