Java教程-Java LinkedHashMap类

Java LinkedHashMap类
Java LinkedHashMap类是Map接口的Hashtable和链表实现,具有可预测的迭代顺序。它继承自HashMap类并实现了Map接口。
需要记住的要点
- Java LinkedHashMap根据键存储值。
- Java LinkedHashMap包含唯一元素。
- Java LinkedHashMap可以有一个null键和多个null值。
- Java LinkedHashMap是非同步的。
- Java LinkedHashMap保持插入顺序。
- Java HashMap类的初始默认容量为16,负载因子为0.75。
LinkedHashMap类声明
让我们看一下java.util.LinkedHashMap类的声明。
- public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>
LinkedHashMap类参数
让我们看一下java.util.LinkedHashMap类的参数。
- K:这是该映射维护的键的类型。
- V:这是映射的值的类型。
Java LinkedHashMap示例
1. **import** java.util.*;
2. **class** LinkedHashMap1{
3. **public** **static** **void** main(String args[]){
4.
5. LinkedHashMap<Integer,String> hm=**new** LinkedHashMap<Integer,String>();
6.
7. hm.put(100,"Amit");
8. hm.put(101,"Vijay");
9. hm.put(102,"Rahul");
10.
11. **for**(Map.Entry m:hm.entrySet()){
12. System.out.println(m.getKey()+" "+m.getValue());
13. }
14. }
15. }
Copy code
输出:
100 Amit
101 Vijay
102 Rahul
Java LinkedHashMap示例:键-值对
1. **import** java.util.*;
2. **class** LinkedHashMap2{
3. **public** **static** **void** main(String args[]){
4. LinkedHashMap<Integer, String> map = **new** LinkedHashMap<Integer, String>();
5. map.put(100,"Amit");
6. map.put(101,"Vijay");
7. map.put(102,"Rahul");
8. //获取键
9. System.out.println("Keys: "+map.keySet());
10. //获取值
11. System.out.println("Values: "+map.values());
12. //获取键值对
13. System.out.println("Key-Value pairs: "+map.entrySet());
14. }
15. }
mathematicaCopy code
输出:
Keys: [100, 101, 102]
Values: [Amit, Vijay, Rahul]
Key-Value pairs: [100=Amit, 101=Vijay, 102=Rahul]
Java LinkedHashMap示例:remove()
1. **import** java.util.*;
2. **public** **class** LinkedHashMap3 {
3. **public** **static** **void** main(String args[]) {
4. Map<Integer,String> map=**new** LinkedHashMap<Integer,String>();
5. map.put(101,"Amit");
6. map.put(102,"Vijay");
7. map.put(103,"Rahul");
8. System.out.println("Before invoking remove() method: "+map);
9. map.remove(102);
10. System.out.println("After invoking remove() method: "+map);
11. }
12. }
输出:
Before invoking remove() method: {101=Amit, 102=Vijay, 103=Rahul}
After invoking remove() method: {101=Amit, 103=Rahul}
Java LinkedHashMap示例:Book
1. **import** java.util.*;
2. **class** Book {
3. **int** id;
4. String name,author,publisher;
5. **int** quantity;
6. **public** Book(**int** id, String name, String author, String publisher, **int** quantity) {
7. **this**.id = id;
8. **this**.name = name;
9. **this**.author = author;
10. **this**.publisher = publisher;
11. **this**.quantity = quantity;
12. }
13. }
14. **public** **class** MapExample {
15. **public** **static** **void** main(String[] args) {
16. //Creating map of Books
17. Map<Integer,Book> map=**new** LinkedHashMap<Integer,Book>();
18. //Creating Books
19. Book b1=**new** Book(101,"Let us C","Yashwant Kanetkar","BPB",8);
20. Book b2=**new** Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);
21. Book b3=**new** Book(103,"Operating System","Galvin","Wiley",6);
22. //Adding Books to map
23. map.put(2,b2);
24. map.put(1,b1);
25. map.put(3,b3);
26.
27. //Traversing map
28. **for**(Map.Entry<Integer, Book> entry:map.entrySet()){
29. **int** key=entry.getKey();
30. Book b=entry.getValue();
31. System.out.println(key+" Details:");
32. System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);
33. }
34. }
35. }
输出: