This is the 12th day of my participation in Gwen Challenge
Java error point 3
If there is any misunderstanding, please correct us!!
Exception handling
Exception handling keywords try, catch, and finally. Is the return in the try module executed before finally, or after finally?
The sample code
package com.wangscaler;
public class TestException {
public static int a = 1;
public static void main(String[] args) {
int b = test();
System.out.println(b);
System.out.println(a);
}
public static int test(a) {
try {
return a;
} catch (Exception e) {
System.out.println(e);
} finally {
++a;
}
return 999999; }}Copy the code
The execution result
1
2
Copy the code
conclusion
In return a; Before executing finally, the value of a will be stored ina local variable, and finally will be executed. Even though the value of a will be 2 after finally is executed, a return will return the value of the variable saved before finally is executed. If you use a return in finally, the value will be overwritten. So avoid using returns in finally. In addition to return,break, continue, etc., should also not be used.
Hash
-
A HashMap:
-
The Map interface is implemented
-
Store key-value pairs
-
Calculate hashCode values based on key-value pairs to store data.
-
Fast access speed.
-
The key of one record is allowed to be NULL, and the value of multiple records is allowed to be NULL.
-
Non-thread-safe, where multiple threads are writing a HashMap at any one time, can result in inconsistent data. To be thread-safe, use the SynchronizedMap method of Collections or use ConcurrentHashMap.
-
Values with the same key will be replaced.
-
Permutation order cannot be preserved
-
-
HashSet
- The Set interface is implemented
- Store the object
- The hashCode value is calculated based on the members of the object, and the hashCode value of the two objects may be the same.
- Slower than HashMap access
- Duplicate values are not allowed in the collection
-
HashTable
- HashTable is a legacy class, similar to HashMap, except that it inherits Dictionary.
- Thread-safe, only one thread can write a HashTable at any time.
- Null keys and values are not acceptable.
-
LinkedHashMap
- A subclass of HashMap that holds the insertion order of records
-
TreeMap
- SortedMap interface is implemented, sort by key (key), the default is to sort by the ascending order of the key value, can also specify the sort comparator.
The HashMap Key calls the hashCode() method, which uses hashCode as the index of the hash table. If the current contents are not empty, the Value is replaced with a new Value based on equals. If the same Key is not found, the list at the current position is moved back and the new Entry array is placed in the head of the list. If the current content is empty, encapsulate the Key and Value into an Entry array.
To add an element to a HashSet, call the element’s hashCode method to get the hash value of the element, and use the hash value to get the position of the element in the hash table, if there is no element written directly to that position. If they already exist, they are compared using equals. If they are the same, they are not allowed to be added. If they are different, add them.
HashMap source code
The sample code
People object
package com.wangscaler;
import java.util.Objects;
public class People {
private int age;
private String name;
public People(int age, String name) {
this.age = age;
this.name = name;
}
public People(a) {}public int getAge(a) {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public String word = "people";
@Override
public String toString(a) {
return "People{" +
"age=" + age +
", name='" + name + '\' ' +
'} ';
}
public static void eat(a) {
System.out.println("Eat");
}
public void sleep(a) {
System.out.println("Sleep");
}
@Override
public boolean equals(Object obj) {
if (obj instanceof People) {
People people = (People) obj;
return Objects.equals(this.age, people.age) && Objects.equals(this.name, people.name);
}
return false;
}
@Override
public int hashCode(a) {
returnObjects.hash(age, name); }}Copy the code
The main function
package com.wangscaler;
import java.util.HashSet;
public class TestHash {
public static void main(String[] args) {
HashSet<People> peoples = new HashSet<>();
People people = new People(18."wang");
peoples.add(people);
System.out.println("The current hashcode value is:" + people.hashCode());
people.setName("wangscaler");
System.out.println("Changed hashcode value:" + people.hashCode());
System.out.println(peoples.contains(people));
peoples.remove(people);
System.out.println(people.getName());
System.out.println(people.getAge());
for(People p : peoples ) { System.out.println(p.hashCode()); System.out.println(p.getAge()); System.out.println(p.getName()); }}}Copy the code
The execution result
The current hashcode value is:3643378The changed hashcode value is:1901259290
false
wangscaler
18
1901259290
18
wangscaler
Copy the code
conclusion
After an object has been Hash, changing hashCode will result in the object being unable to be found in the collection and therefore unable to be deleted, resulting in a memory leak.