问题描述
HashSet 包含某Object=true,但是remove该Object就报错。Object RecordNumber重写了toString hashCode equals 方法,估计是这里有问题。请教如何解决HashSet的remove方法报错问题!以下是代码,请帮忙测试更正remove报错问题,谢谢!import java.text.ParseException; import java.util.Date; //数据源 表名 时间 (记录数量) public class RecordNumber { public final static int CSUDF_SYS_CELL_HOUR_G = (int) 0; public final static int CSUDF_SYS_RTF_HOUR_G = (int) 1; public final static int CSUDF_SYS_GPROC_HOUR_G = (int) 2; private String OMC; private String TableName; private Date DateTime; public RecordNumber(String DataSource, String TableName, Date DateTime) { this.OMC = DataSource; this.TableName = TableName; this.DateTime = DateTime; } public String getOMC() { return this.OMC; } public String getTableName() { return this.TableName; } public Date getDateTime() { return this.DateTime; } public int getTableNameInt() { int x = 1000; if (TableName.equals("CSUDF_SYS_CELL_HOUR_G")) { x = (int) CSUDF_SYS_CELL_HOUR_G; } if (TableName.equals("CSUDF_SYS_RTF_HOUR_G")) { x = CSUDF_SYS_RTF_HOUR_G; } if (TableName.equals("CSUDF_SYS_GPROC_HOUR_G")) { x = CSUDF_SYS_GPROC_HOUR_G; } return x; } @Override public String toString() { return (this.OMC + "," + this.TableName + "," + this.DateTime); } @Override public boolean equals(Object o) { return (o instanceof RecordNumber) && toString().equals(((RecordNumber) o).toString()); } @Override public int hashCode() { int hash = 7; hash = 37 * hash + (this.OMC != null ? this.OMC.hashCode() : 0); hash = 37 * hash + (this.TableName != null ? this.TableName.hashCode() : 0); hash = 37 * hash + (this.DateTime != null ? this.DateTime.hashCode() : 0); return hash; } //单元测试 public static void main(String[] args) throws ParseException { java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm"); RecordNumber rn = new RecordNumber("OMCSP9", "CSUDF_SYS_CELL_HOUR_G", df.parse("2010-10-26 07:00")); java.util.Set<RecordNumber> testSet = new java.util.LinkedHashSet<RecordNumber>(); testSet.add(rn); rn = new RecordNumber("OMCSP1", "CSUDF_SYS_CELL_HOUR_G", df.parse("2010-10-26 07:00")); testSet.add(rn); rn = new RecordNumber("OMCSP11", "CSUDF_SYS_CELL_HOUR_G", df.parse("2010-10-26 07:00")); testSet.add(rn); System.out.println(testSet.size()); for (RecordNumber x : testSet) { System.out.println(x); System.out.println("testSet包含该RecordNumber:" + testSet.contains(x)); testSet.remove(x); System.out.println(testSet.size()); } } } run: 3 OMCSP9,CSUDF_SYS_CELL_HOUR_G,Tue Oct 26 07:00:00 CST 2010 testSet包含该RecordNumber:true 2 Exception in thread "main" java.util.ConcurrentModificationException at java.util.LinkedHashMap$LinkedHash-terator.nextEntry(LinkedHashMap.java:373) at java.util.LinkedHashMap$KeyIterator.next(LinkedHashMap.java:384) at datatransfer.RecordNumber.main(RecordNumber.java:79) Java Result: 1 成功生成(总时间:0 秒)
解决方案
可以使用如下方式:for (Iterator<RecordNumber> it = testSet.iterator(); it.hasNext();) { RecordNumber rn = it .next(); System.out.println(rn); System.out.println("testSet包含该RecordNumber:" + testSet.contains(rn)); it.remove();}
解决方案二:
LinkedHashSet是非线程同步的,不允许在遍历的同时去删除或修改里面的元素。你可以在JDK文档里面查到
解决方案三:
HashSet是一个快速失败的,不能这样迭代的删除元素。