|
Outline
Content HashSet element referenced object changes, cause problems "element does not belong to the collections". In fact, this element is also in the collection, but the call contains the method to judge, the result is false.
text
About Change
Here talked about change is the change in the content element references an object, but the object is still the object. For example, we define the following field
private Set < Set < Integer >> cache = new HashSet < Set < Integer >> ();
We plan cache where each element is a Set collection. If we remove one element cache, and then to add a set of elements that Integer element. For cache, this element or that element, but its content has changed.
About calibration standards
/ **
* Check.
* Remove the element from the collection but not part of the collection was invalid.
* @return
* /
private boolean validate () {
boolean flag = true;
for (Set ele: cache) {
if (! cache.contains (ele)) {
flag = false;
System.out.println ( "Invalid element:" + ele);
}
}
return flag;
}
test
We divided into three test cases: data initialization test directly update the test, remove the new test.
First, the data initialization test
1. Data initialization
/ **
* Initialize data.
* /
private void init () {
Integer [] [] data = {{1, 2}, {3, 4}, {5}};
for (Integer [] ele: data) {
List < Integer> eleList = Arrays.asList (ele);
Set < Integer> eleSet = new HashSet < Integer> (eleList.size ());
eleSet.addAll (eleList);
cache.add (eleSet);
}
System.out.println (cache);
}
2. Test
@Test
public void testInit () {
init ();
boolean flag = validate ();
System.out.println ( "initialization data validation result:" + flag);
}
3. Output Results
[[2, 1], [5], [4, 3]]
Initialization data validation result: true
Second, the direct testing update
1. Update method
/ **
* Modified directly.
* /
private void update () {
for (Set < Integer> ele: cache) {
if (ele.contains (5)) {
ele.add (6);
break;
}
}
System.out.println (cache);
}
2. Test
@Test
public void testUpdate () {
init ();
update ();
boolean flag = validate ();
System.out.println ( "direct modification of data validation, the results:" + flag);
}
3. Output Results
[[2, 1], [5], [4, 3]]
[[2, 1], [6, 5], [4, 3]]
Invalid elements: [6, 5]
To directly modify the data validation result: false
Third, remove the new test
1. Remove the new
/ **
* Removed added.
* /
private void removeThenAdd () {
for (Set < Integer> ele: cache) {
if (ele.contains (5)) {
cache.remove (ele);
ele.add (6);
cache.add (ele);
break;
}
}
System.out.println (cache);
}
2. Test
@Test
public void testRA () {
init ();
removeThenAdd ();
boolean flag = validate ();
System.out.println ( "to remove an added data validation, the results:" + flag);
}
3. Output Results
[[2, 1], [5], [4, 3]]
[[2, 1], [4, 3], [6, 5]]
To remove an added data validation, result: true
in conclusion
I think HashSet traversing element and determine whether the elements in the collection mechanism is different, the HashSet element has a different hashcode, we directly modify the elements, resulting in its content and its hashcode do not correspond, it will have the above-mentioned problems. |
|
|
|