Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Element content of Java HashSet change issues     - Shell scripts to copy all directories under the current directory of a certain type of file to the same directory (Linux)

- Tomcat itself through simple movement separation (Server)

- Linux dynamic libraries and Guide (Programming)

- How to use GRUB2 files directly from the hard disk to run ISO (Linux)

- Linux basic introductory tutorial ---- regex basis (Linux)

- CentOS 7 open ports (Linux)

- Download Google Android source code under Ubuntu 4.4 (Linux)

- See Shell Script Linux Server network traffic (Server)

- How to Create a file can not be changed under Linux (Linux)

- VMware ghost Linux card error (Linux)

- CentOS6 5 source compiler installation Hadoop2.5.1 (Server)

- Ora-1092: OPI colleague K aborting process --- killed by OO Well killer (Database)

- Java rewrite the hashcode method (Programming)

- To create someone else can not afford to delete the administrator user (Linux)

- Introduction to Linux system process monitoring tools (Linux)

- RealVNC Server 5.2.3 Installation and Configuration In Fedora (Server)

- Linux Mint 17 set up the Ruby environment (Linux)

- Java Foundation - implicit conversion vs cast (Programming)

- How to install the Linux text editor Atom 0.124.0 (Linux)

- Hardware Firewall Basics (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 7.0 running Docker kernel error solution (Server)
- Use Bash script write CVS version control (Server)
- 10 example of the detection memory usage free Linux commands (Linux)
- Linux install Eclipse for C / C ++ Development (Linux)
- The Linux OOM Terminator (Server)
- using the ssh command to check the socket / Network Connections (Linux)
- Process monitoring tools Supervisor start MongoDB (Database)
- Hadoop connection failed or stuck processing (Server)
- How to use OpenVPN and PrivacyIDEA build two-factor authentication for remote access (Server)
- CentOS 6.5 installation and simple configuration Nginx (Server)
- Some common regular expressions (Linux)
- Sublime Text Add instructions to insert the current time zone (Linux)
- Import and export myloader accelerate mydumper (Database)
- Quickly build and install Linux KVM system (Linux)
- Install Open vSwitch under CentOS 6.5 (Linux)
- How to install the client sqlplus under linux (Database)
- C language preprocessor command (Programming)
- Ubuntu 14.04 to install file editor KKEdit 0.1.5 version (Linux)
- How to use the beta / unstable version of the software in Debian library (Linux)
- How to configure FirewallD in RHEL / CentOS 7 and Fedora in (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.