基于HashSet 实现 IpTable

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

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值