corejava第10版
This commit is contained in:
1 parent
2021c3c227
commit
6165543dd3
1088 files changed
+94411
No files matched your search
@@ -0,0 +1,42 @@
|
||||
package map;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* This program demonstrates the use of a map with key type String and value type Employee.
|
||||
* @version 1.12 2015-06-21
|
||||
* @author Cay Horstmann
|
||||
*/
|
||||
public class MapTest
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Map<String, Employee> staff = new HashMap<>();
|
||||
staff.put("144-25-5464", new Employee("Amy Lee"));
|
||||
staff.put("567-24-2546", new Employee("Harry Hacker"));
|
||||
staff.put("157-62-7935", new Employee("Gary Cooper"));
|
||||
staff.put("456-62-5527", new Employee("Francesca Cruz"));
|
||||
|
||||
// print all entries
|
||||
|
||||
System.out.println(staff);
|
||||
|
||||
// remove an entry
|
||||
|
||||
staff.remove("567-24-2546");
|
||||
|
||||
// replace an entry
|
||||
|
||||
staff.put("456-62-5527", new Employee("Francesca Miller"));
|
||||
|
||||
// look up a value
|
||||
|
||||
System.out.println(staff.get("157-62-7935"));
|
||||
|
||||
// iterate through all entries
|
||||
|
||||
staff.forEach((k, v) ->
|
||||
System.out.println("key=" + k + ", value=" + v));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user