Java从IHTTPStreamersession中删除重复的条目以获取公共IP地址

时间:2016-03-28 23:50:15

标签: java arrays

如何从此代码中删除重复的条目,其中重复的条目是与其他会话具有相同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>

生成一个条目

1 个答案:

答案 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);
   }
}