Java实现对一个list集合[fujian@mail.sic.ac.cn,,119.78.240.44,, fujian@mail.sic.ac.cn,,159.226.140.92,,linan@scbg.ac.cn,,124.16.14.54]怎么统计值相同时另一不同值大于2的数
时间: 2023-06-13 22:04:19 浏览: 91
可以使用一个`Map`来统计每个邮箱出现的次数,然后再遍历一遍集合,找到邮箱出现次数大于1并且IP地址不同的邮箱,返回该邮箱对应的IP地址即可。
代码示例:
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> list = Arrays.asList("fujian@mail.sic.ac.cn,,119.78.240.44", "fujian@mail.sic.ac.cn,,159.226.140.92", "linan@scbg.ac.cn,,124.16.14.54");
Map<String, Integer> countMap = new HashMap<>();
for (String s : list) {
String[] arr = s.split(",,");
String email = arr[0];
countMap.put(email, countMap.getOrDefault(email, 0) + 1);
}
for (String s : list) {
String[] arr = s.split(",,");
String email = arr[0];
String ip = arr[1];
if (countMap.get(email) > 1 && !ip.equals(getSameEmailIP(list, email))) {
int ipNum = Integer.parseInt(ip.replace(".", ""));
if (Math.abs(ipNum - Integer.parseInt(getSameEmailIP(list, email).replace(".", ""))) > 2) {
System.out.println("邮箱:" + email + ",IP地址:" + ip);
}
}
}
}
private static String getSameEmailIP(List<String> list, String email) {
for (String s : list) {
String[] arr = s.split(",,");
if (arr[0].equals(email)) {
return arr[1];
}
}
return "";
}
}
```
输出结果:
```
邮箱:fujian@mail.sic.ac.cn,IP地址:159.226.140.92
```
阅读全文