Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Element content of Java HashSet change issues     - System-level alias vs Oracle ADR functionality (Database)

- How to set IonCube Loaders in Ubuntu (Linux)

- Oracle 12c In-Memory Study (Database)

- Linux kernel network subsystem analysis (Programming)

- iscsiadm command usage (Linux)

- Php and MySQL command add to the environment variable method in Linux system (Linux)

- XenServer virtual machines installed in dual-card configuration (Server)

- Nginx + uWSGI + Django + Python Application Architecture Deployment (Server)

- ASP.NET 5 (vNext) Linux deployment (Server)

- Linux System Getting Started Learning: Fix ImportError: No module named scapy.all (Linux)

- Qt shared memory interprocess communication (Programming)

- The most simple drive to write and test procedures under linux (Programming)

- Linux common network tools: batch scanning of hosting services netcat (Linux)

- Several Ceph performance optimization of new methods and ideas (2015 SH Ceph Day after flu reference) (Server)

- 10 Nginx safety tips (Linux)

- Supervisor Installation and Configuration (Server)

- installation of Piwik under Ubuntu (Programming)

- Hadoop scheduling availability of workflow platform - Oozie (Server)

- Python substring format (Programming)

- Paint with Vim (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:      
 
- Java memory-mapped file MappedByteBuffer (Programming)
- Debian 7.6 install Nvidia graphics driver (Linux)
- Hadoop upload files error solved (Server)
- Android webView URL redirects affect goBack () (Programming)
- Parts of the partition is not recognized after Debian mount mobile hard disk (Linux)
- Why did not Oracle privileges can also log in with sysdba (Database)
- Different between Linux file path and the windows (Linux)
- Use HugePages optimize memory performance (Database)
- Windows environment Android Studio v1.0 Installation Guide (Linux)
- Configure the Linux kernel and use iptables to do port mapping (Linux)
- CentOS7 Kubernetes used on container management (Server)
- Oracle 11R2 Grid Infrastructure execute root.sh script rootcrs.pl execution failed treatment (Database)
- How a lot of blocking malicious IP address in Linux (Linux)
- Linux use chattr and lsattr commands to manage file and directory attributes (Linux)
- Linux open handle limit adjustment (Linux)
- gzip, bzip2, xz, tar, zip compression, archive Detailed (Linux)
- CentOS7 installation GAMIT and GMT (Linux)
- DataGuard a hardware issue warnings found (Database)
- Java String type time compare the size (Programming)
- Linux network security backdoor technology and advanced skill practice (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.