This is the fourth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021.
The Map is a very important piece of the container.
HashMap is a commonly used implementation class in maps. “LeetCode must see HashMap” proves how important it is to master HashMap.
Key words to remember: the key cannot be repeated.
It is technically possible to add the same key, but the meaning overwrites the value of the previous key. There is no more explanation.
Little white practice, pattern error
1 After creating a new class, start immediately:
Map<Integer, String> m1 = new HashMap<>(); // implement class m1.put(1,"One");
Now the interface of IDEA looks like this:
So my confident self started following the put line, and as I typed something vaguely wrong:Why is that?
When I hit Enter, it becomes:
I will not believe evil, but will follow the knock. Sure enough, the error was reported:
And why I clearly have M1, followed by the USE of M1, how the above used M1 or gray?
Tips after placing:
Cannot resolve symbol ‘put’
I’m more depressed. I’m as tall as an old man. A HashMap followed by a Map?Sorry, wrong guess.
Then start baidu HashMap Java, open the rookie tutorial, found that others are similar:
Later, I honestly baidu the cause of the error, the first result tells me the solution: it is not in the main method!
Package in, as expected, all obedient:
Low order method:
get(), size(), isEmpty(), containsKey(), containsValue()
public class TestMap {
public static void main(String[] args) {
Map<Integer, String> m1 = new HashMap<>(); / / implementation class
m1.put(1."One");
m1.put(2."Two");
m1.put(3."Three");
m1.put(4."Four");
System.out.println("m1.get(1): " + m1.get(1));
System.out.println("m1.size(): " + m1.size());
System.out.println("m1.isEmpty(): " + m1.isEmpty());
System.out.println("m1.containsKey(3)" + m1.containsKey(3));
System.out.println("m1.containsValue("Five")" + m1.containsValue("Five"));
Map<Integer, String> m2 = new HashMap<>();
m2.put(5."Five");
m2.put(6."Six");
m1.putAll(m2);
System.out.println(After "m1. PutAll (m2) : \ n"+ m1); }}Copy the code
In Python it is a dictionary that goes everywhere:
- get():
dict[key]
- size():
len(dict)
- isEmpty():
len(dict)==0
- containsKey():
x in dict.keys()
- containsValue():
x in dict.values()
Where can not install where
Class value (string); class value (string);
public class TestMap2 {
public static void main(String[] args) {
Employee e1 = new Employee(Awesome!."Fang".66666);
Employee e2 = new Employee(667."Zhang".9999999); // Because he is crazy
Employee e3 = new Employee(668."' uncle".36888);
Map<Integer, Employee> map = new HashMap<>();
map.put(Awesome!, e1);
map.put(667, e2);
map.put(668, e3);
Employee emp = map.get(Awesome!); // Return the objectSystem.out.println(emp.getName()); }}Copy the code
The class information is :(id, name, salary) + JavaBean methods get and set, and the constructor. The running results are as follows:
Python is defiant:
e1 = [Awesome!."Fang".66666]
e2 = [667."Zhang".9999999]
e3 = [668."' uncle".36888]
employees = {}
employees[Awesome!] = e1
employees[667] = e2
employees[668] = e3
Copy the code
A similar usage is perfectly possible. Lists + dictionaries are temporarily invincible.
Another day to look forward to new knowledge!