如何从此代码中删除重复的条目,其中重复的条目是与其他会话具有相同IP地址的会话?
Iterator<IHTTPStreamerSession> iterHttp = httpSessions.iterator();
while(iterHttp.hasNext())
{
IHTTPStreamerSession httpSession = iterHttp.next();
if (httpSession == null)
continue;
ret.append("<HTTPSession>");
ret.append("<IpAddress>"+httpSession.getIpAddress()+"</IpAddress>");
ret.append("<TimeRunning>"+httpSession.getTimeRunningSeconds()+"</TimeRunning>");
ret.append("</HTTPSession>");
}
我需要为每个<IpAddress>
答案 0 :(得分:0)
您可以使用Set检查IP地址是否与先前添加的条目匹配。像这样:
Set<String> ips = new HashSet();
Iterator<IHTTPStreamerSession> iterHttp = httpSessions.iterator();
while(iterHttp.hasNext()){
IHTTPStreamerSession httpSession = iterHttp.next();
if (httpSession == null)
continue;
String ip = httpSession.getIpAddress();
if(!ips.contains(ip)){
ret.append("<HTTPSession>");
ret.append("<IpAddress>"+ ip +"</IpAddress>");
ret.append("<TimeRunning>"+httpSession.getTimeRunningSeconds()+"</TimeRunning>");
ret.append("</HTTPSession>");
ips.add(ip);
}
}