1, Map
- Data is stored as key value to situation (key – value)
- Key is unique (cannot be repeated)
Relationship between HashSet and HashMap
The underlying implementation of HashSet is actually a HashMap
HashSet depends on HashMap
The value added to the HashSet is equivalent to the location added to the Key in the Map
HashSet and TreeSet
The de reordering is equivalent to the key de operation of map
HashSet to repeat: override the HashCode() and equals () methods
HashSet to repeat: override the HashCode() and equals () methods
// Add to
public static void fun1(){
// Key key name
// value age
Map<String , Integer> map = new HashMap<>();
// Save key value pair
// Adding a key value to the return value of a method returns the part of the value being overridden
Integer num1 = map.put("Zhang San", 18);
Integer num2 = map.put("Li Si", 22);
Integer num3 = map.put("Wang Wu", 31);
Integer num4 = map.put("Zhao Liu", 11);
// Test the return value of the add method
Integer num5 = map.put("Zhang San", 250);
System.out.println(num5);
System.out.println(num1);
System.out.println(num2);
System.out.println(num3);
System.out.println(num4);
// Print map with only four key value pairs
System.out.println(map);
}
public static void fun2() {
/*
* test
* clear()
* containsKey(Object key)
* containsValue(Object value)
* isEmpty()
* remove(Object key)
* size()
*/
Map<String, Integer> map = new HashMap<>();
// Add put
Integer num1 = map.put("Zhang San", 18);
Integer num2 = map.put("Li Si", 22);
Integer num3 = map.put("Wang Wu", 31);
Integer num4 = map.put("Zhao Liu", 11);
// The return value of deletion is the value corresponding to the deleted key
Integer remove = map.remove("Li Si");
// Include this key
System.out.println(map.containsKey("Zhang San"));
// Include this value or not
System.out.println(map.containsValue(18));
// Is the map set empty
System.out.println(map.isEmpty());
// Delete an element (because the key is unique, delete according to the key)
System.out.println(map.remove("Li Si"));
// Logarithm of key value pairs in map set
System.out.println(map.size());
map.clear();
System.out.println(map);
}
public static void fun3() {
// Create map set key save student value save student's household registration
Map<Student, String> map = new HashMap<>();
map.put(new Student("Wang Long",18), "Beijing");
map.put(new Student("Xu Hanhan",23), "Wuxi");
map.put(new Student("Pinus koraiensis",20), "Xuzhou");
map.put(new Student("Wang Long",18), "Shanghai");
// Why is it printed to keep Wang long in Shanghai
// The key will not be saved repeatedly, but the value will overwrite the previous value
System.out.println(map);
}
Traverse Map set
// Traverse Map
HashMap<String, Integer> map = new HashMap<>();
map.put("Pinus koraiensis", 18);
map.put("Liu Guoqiang", 19);
map.put("Xie Weifeng", 14);
map.put("Li Yanqi", 15);
// Convert map set to set
Set<String> keySet = map.keySet();
// Get the iterator of keySet
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
// Get next element
String next = iterator.next();
// Get value through key value
Integer integer = map.get(next);
System.out.println(next + " = " + integer);
}
}
public static void fun2() {
// Use enhanced for loop traversal
// Traverse Map
HashMap<String, Integer> map = new HashMap<>();
map.put("Pinus koraiensis", 18);
map.put("Liu Guoqiang", 19);
map.put("Xie Weifeng", 14);
map.put("Li Yanqi", 15);
Set<String> keySet = map.keySet();
for (String key : keySet) {
// Get value value through key
Integer integer = map.get(key);
System.out.println(key + " = " + integer);
}
}
public static void fun3() {
// entrySet();
// This method returns a key value pair object when it is stored in a collection
// key and value are saved in the Entry object
// Traverse with this method
HashMap<String, Integer> map = new HashMap<>();
map.put("Pinus koraiensis", 18);
map.put("Liu Guoqiang", 19);
map.put("Xie Weifeng", 14);
map.put("Li Yanqi", 15);
// Convert map set to set set set
Set<Entry<String, Integer>> entrySet = map.entrySet();
// Get the iterator of the transformed collection
Iterator<Entry<String, Integer>> iterator = entrySet.iterator();
// Determine if there is another element
while (iterator.hasNext()) {
// Next element (object)
Entry<String, Integer> next = iterator.next();
// Take the value value out of the object
Integer value = next.getValue();
// Take the key value out of the object
String key = next.getKey();
// Get the Value through the key Value obtained
// Integer integer = map.get(next);
// Printing
}
}
public static void fun4() {
// Enhancing for loop traversal with entrySet() method
HashMap<String, Integer> map = new HashMap<>();
map.put("Pinus koraiensis", 18);
map.put("Liu Guoqiang", 19);
map.put("Xie Weifeng", 14);
map.put("Li Yanqi", 15);
// Convert the map set to an entry set
Set<Entry<String, Integer>> entrySet = map.entrySet();
for (Entry<String, Integer> entry : entrySet) {
// Get the value value through the element getValue traversed
Integer value = entry.getValue();
// getKey get key value
String key = entry.getKey();
System.out.println(key + value);
}
}
Nesting of map s
public static void main(String[] args) {
/*
* Java subject
* java 1 Class - save students and household registration
* java 2 Class - save students and household registration
*
* 1 Class is also a map
* Save - student's household registration
* Java Discipline is a map
* Two key value pairs, one and two, are saved
*/
// Create a collection of disciplines
HashMap<HashMap<Person, String>, Integer> map = new HashMap<>();
// Create a shift collection
HashMap<Person, String> c1 = new HashMap<>();
c1.put(new Person("Pinus koraiensis",18),"Xuzhou" );
c1.put(new Person("Dongsuyan",18),"Fengxian County" );
// Create a second shift collection and add two people
HashMap<Person, String> c2 = new HashMap<>();
c2.put(new Person("Fifi",15), "ynz");
c2.put(new Person("Hey",16),"Rugao");
map.put(c1, 1);
map.put(c2, 2);
// Convert map set to set set set
Set<HashMap<Person, String>> keySet = map.keySet();
// Get iterator of KeySet
Iterator<HashMap<Person, String>> m1 = keySet.iterator();
// Judgment cycle
while (m1.hasNext()) {
// What we get are two small map sets in a large set
HashMap<Person, String> class1 = m1.next();
// Convert sub map set to set set set
Set<Person> cset = class1.keySet();
// Get iterator of cset
Iterator<Person> csetItera = cset.iterator();
// Circular judgment
while (csetItera.hasNext()) {
// Get the key value in the subset
Person p = csetItera.next();
// Get value through key value
String string = class1.get(p);
// Print results
System.out.println( p + " == " + string);
}
}
}