Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Element content of Java HashSet change issues     - Install Unity 8 preview version of the desktop in Ubuntu (Linux)

- Linux upgrade GCC 4.8.1 clear and concise tutorials (Ubuntu 12.04 64-bit version as an example) (Linux)

- Hibernate learning introductory tutorial (Programming)

- Ubuntu UFW firewall settings Introduction (Linux)

- To restore the last time applications running when Ubuntu user log in again (Linux)

- The security configuration of Linux (Linux)

- How to Use Nmap security scanner tool on Linux (Linux)

- Ant command-line compiler Android project (Programming)

- Use 3G wireless network card under Linux (Linux)

- APT-GET commonly used Linux command (Linux)

- Linux command line under HTTP traffic sniffing tool: httpry (Linux)

- These days have been tossing in the Linux under the ASP.NET 5, on the next in the other operating systems in the ASP.NET 5 or. NET applications, in order to complete the MS VM (CoreCLR) run is not far Reach, the effect of the application.

Cur
(Server)

- Linux NFS FTP use (Server)

- CentOS 6.5 installation and deployment SVN 1.8.10 (Linux)

- tar command to extract a file error: stdin has more than one entry (Linux)

- Change CentOS 7 NIC name eno16777736 to eth0 (Linux)

- Parameters of the extra port MySQL 5.6 (Database)

- Git large file storage will help handle large binary files (Linux)

- Linux iptables firewall settings whitelist (RHEL 6 and CentOS 7) (Linux)

- Linux landing 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:      
 
- Lenovo E431 notebook CentOS system is installed wireless network card driver (Linux)
- Use SocketServer write a sockets program (Programming)
- CentOS 6.5 start ActiveMQ being given to solve (Server)
- Effective Java - lazy initialization (Programming)
- Ubuntu buffalo wzr-hp-300nh brush DD-WRT router system (Linux)
- Create a DLL using MinGW and Attention (Programming)
- CentOS install Memcached (Server)
- Teach you how to ensure password security under the Linux operating system (Linux)
- SHELL script to use anti SSH brute force and vsftpd (Linux)
- How to build a container cluster (Server)
- Linux kernel IPv6 protocol closed manner (Linux)
- Source compiler install Nginx (Server)
- Detailed installation of CentOS 6.x (Linux)
- Linux platform to prevent hackers to share practical skills (Linux)
- JavaScript function closures Quick Start (Programming)
- Oracle delete table space prompted ORA-00604 and ORA-38301 (Database)
- C ++ implementation of the list of basic operations and test cases (Programming)
- [SHELL] MySQL primary recovery solution from + Keepalived online (Server)
- Summary of Docker mounted directory (Server)
- Linux environment has been running Tomcat how to deploy the new Tomcat (Server)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.