Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Element content of Java HashSet change issues     - CentOS7 complete step to install Hadoop2.7 (Server)

- Ubuntu deployment Flask + UWSGI + Nginx Comments (Server)

- Linux, MySQL / MariaDB Galera Cluster Setup Process (Database)

- How to Install Sticky Notes on Ubuntu and Derivatives (Linux)

- Linux install Samba file sharing server (Server)

- Ubuntu Linux installation GAMIT10.6 (Linux)

- Using VMware vSphere Client Linux virtual machine installation CentOS6.4 system (Linux)

- Ubuntu Slingscold (Linux)

- Four safety delete files under Linux tools (Linux)

- Ubuntu Series Installation Docker (Linux)

- Java memory area (Programming)

- Linux firewall anti-hacker disguise malicious attacks (Linux)

- Preview function to confirm the stop resource Oracle 12c new feature crsctl (Database)

- DB2 table space is redirected to restore the database combat (Database)

- To install Gitolite in Ubuntu / Fedora / CentOS (Linux)

- Elaborate .NET Multithreading: Using Task (Programming)

- RedHat command line and graphical interface switching (Linux)

- Redis 3.0.3 Cluster Setup (Database)

- CentOS Set the Mono environment variable (Server)

- Ubuntu install Eclipse can not find JAVA_HOME problem (Linux)

 
         
  Element content of Java HashSet change issues
     
  Add Date : 2018-11-21      
         
         
         
  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.
     
         
         
         
  More:      
 
- CentOS installation Docker series (Linux)
- java.net.NoRouteToHostException of Hadoop problem: no route to host (Server)
- The Java way to stop a thread of execution (Programming)
- Comparison of Nginx and Nginx + (Server)
- Linux iptables: basic principles and rules (Linux)
- Let Linux boot methods to enter characters interface and set FrameBuffer resolution methods (Linux)
- printf PHP string operations () built-in function usage (Programming)
- How to Upgrade Ubuntu GNOME 14.10 to GNOME 3.16 Desktop (Linux)
- Java generate two-dimensional code by Zxing (Programming)
- Manually compile Hadoop 2.6.0 under Ubuntu 14.04 (Server)
- Log analysis is done with Grafana Elasticsearch (Server)
- Do you know how to build the Linux kernel (Programming)
- CentOS7 build GlusterFS (Linux)
- 11G ASM disk group does not automatically handle MOUNT (Database)
- Let Git variety of colors and custom log format output (Linux)
- Setting grep Highlight Matches (Linux)
- MySQL partition summary (Database)
- Install Oracle 11g illustrations and dependent libraries under SUSE11 (Database)
- Redis master-slave replication switch (Database)
- CentOS7 yum install third-party source EPEL (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.