Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Element content of Java HashSet change issues     - Apache Web Security Linux systems (Linux)

- Hibernate Search and Lucene quick introduction (Linux)

- Ubuntu 14.04 install PostgreSQL 9.2 (Database)

- Linux itself disguised illusion strengthen security (Linux)

- CentOS modify yum update source (Linux)

- Use $ BASH ENV variable to mention the right way under Linux (Linux)

- CentOS 6.6 permanent method to modify the DNS address (Linux)

- 10 Best Swift Tutorial examples (Programming)

- CentOS 6.5 platform offline compile and install PHP5.6.6 (Server)

- Shared directory settings between Linux and Linux (Linux)

- MBR partitions under Linux (Linux)

- CentOS 6.5 set under Oracle 12c at startup (Database)

- The most concise explanation of JavaScript closures (Programming)

- Installation and configuration under Linux SVN server - Backup - Recovery - Scheduled Tasks (Server)

- Based LNMP + uWSGI deploy Django applications (Server)

- Configuring Allatori code confusion when developing general Java applications in NetBeans (Programming)

- Zabbix installation under Linux (Server)

- Linux create and delete user step (Linux)

- PHP 5.3 New Features Detail (Linux)

- Linux operating system Samba server configuration and use (Server)

 
         
  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:      
 
- Snort build a secure Linux server (Linux)
- Linux performance optimization tools perf top (Linux)
- Oracle create user authorization and in PLSQL (Database)
- CentOS 7 repair MBR and GRUB (Linux)
- MySQL query optimization: profile function (Database)
- Performance Optimization: Using Ramlog transfer log files to memory (Linux)
- Tsung first test installation (Linux)
- Iptables small summary (Linux)
- Ubuntu system safe and caution sudo su command (Linux)
- impdp error ORA-31631, ORA-39122 (Database)
- Qt for file splitting and fusion gadgets (Programming)
- Analysis of potential problems through custom Orabbix monitoring Oracle (Database)
- Attic-- delete duplicate data backup program (Linux)
- Ubuntu 15.04 and CentOS 7 to deploy .NET, Mono and DNX (Server)
- ORA-12154 TNS could not resolve the specified identifier (Database)
- Use regular expressions to check whether the input box to enter a URL (Programming)
- Ubuntu compiler installation R Full Record (Linux)
- MongoDB study notes - polymerization (Database)
- BackTrack (BT3, BT4) Linux installation tutorial (Linux)
- Linux command binlog by filtering out the complete SQL statement (Database)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.