1. HashSet 是基于HashMap实现,其中涉及到成员对象的等价比较和hashCode;
因此,要重写成员对象的equals方法和hashCode方法;
HashSetTestIp.java
import java.util.*;
class Ip {
private int ip;
public Ip(int ip) {
this.ip = ip;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o.getClass() == Ip.class) {
Ip that = (Ip) o;
return that.ip == this.ip;
}
return false;
}
public int hashCode()
{
return this.ip;
}
public String toString() {
return ""
+ ((ip >> 24) & 0xff) + "."
+ ((ip >> 16) & 0xff) + "."
+ ((ip >> 8) & 0xff) + "."
+ ((ip >> 0) & 0xff);
}
}
public class HashSetTestIp {
public static void main(String[] args) {
HashSet<Ip> set = new HashSet<Ip>();
set.add(new Ip(20));
set.add(new Ip(20));
set.add(new Ip(20));
set.add(new Ip(0xFF000000));
set.add(new Ip(0xFF000000));
set.add(new Ip(0xFF000000));
System.out.println("size: " + set.size());
System.out.println(set);
System.out.println(set.contains(new Ip(20)));
System.out.println(set.contains(new Ip(0xFF000000)));
Iterator<Ip> iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
HashSet<Ip> s = (HashSet<Ip>) set.clone();
System.out.println("clone:" + s);
s.remove(new Ip(20));
System.out.println(s);
System.out.println(set);
set.remove(new Ip(20));
System.out.println(set);
set.add(new Ip(10));
set.add(new Ip(30));
set.add(new Ip(0xFF000000));
set.add(new Ip(0xFF0000FF));
System.out.println("size: " + set.size());
System.out.println(set);
set.clear();
if (set.isEmpty()) {
System.out.println("size: " + set.size());
System.out.println(set);
}
}
}
2. 基于HashSet 实现IpTable;
1. Ip.java : 重载其equals方法和hashCode方法:
public class Ip {
private int ip;
public Ip(int ip) {
this.ip = ip;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o.getClass() == Ip.class) {
Ip that = (Ip) o;
return that.ip == this.ip;
}
return false;
}
public int hashCode()
{
return this.ip;
}
public String toString() {
return ""
+ ((ip >> 24) & 0xff) + "."
+ ((ip >> 16) & 0xff) + "."
+ ((ip >> 8) & 0xff) + "."
+ ((ip >> 0) & 0xff);
}
}
2.IpTable.java : 实现对HashSet 的封装,工厂模式;
import java.util.HashSet;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
public class IpTable {
private HashSet<Ip> table;
private int ipNum;
private IpTable() {
}
public static IpTable getIpTable(String ipTableFile) {
IpTable ipTable = new IpTable();
//ipTable.table = new HashSet<Ip>(1 << 16); // 64K
ipTable.table = new HashSet<Ip>(); // 64K
ipTable.ipNum = 0;
try {
File file = new File(ipTableFile);
InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
BufferedReader br = new BufferedReader(reader);
String line = "";
while ((line = br.readLine()) != null) {
int ip = ipToInt(line);
ipTable.addIp(ip);
}
br.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return ipTable;
}
public static int ipToInt(String ip) {
String[] ip_bytes = ip.split("\\.");
return ((Integer.parseInt(ip_bytes[0]) << 24) |
(Integer.parseInt(ip_bytes[1]) << 16) |
(Integer.parseInt(ip_bytes[2]) << 8) |
(Integer.parseInt(ip_bytes[3]) << 0)) & 0xffffffff;
}
public void addIp(int ip) {
if (!this.hasIp(ip)) {
ipNum++;
}
table.add(new Ip(ip));
}
public void removeIp(int ip) {
if (this.hasIp(ip)) {
ipNum--;
}
table.remove(new Ip(ip));
}
public boolean hasIp(int ip) {
return table.contains(new Ip(ip));
}
public int getValidIpNum() {
return ipNum;
}
public static void main(String[] args) {
IpTable ipTable = IpTable.getIpTable("ip.txt");
System.out.println("ipTable has valid ip num: " + ipTable.getValidIpNum());
String ip = "192.168.128.10";
if (ipTable.hasIp(IpTable.ipToInt(ip))) {
System.out.println("ip table has ip: " + ip);
}
}
}