LinkedHashMap允许在Map上进行插入顺序迭代,也就是说,当迭代LinkedHashMap时,将按插入顺序返回元素。
以下是LinkedHashMap类支持的构造函数的列表。
Sr.No. | Constructor & Remark |
---|---|
1 | LinkedHashMap() 此构造函数构造一个默认的LinkedHashMap。 |
2 | LinkedHashMap(Map m) 此构造函数使用给定Map类 m 中的元素初始化LinkedHashMap。 |
3 | LinkedHashMap(int capacity) 此构造函数使用给定的容量初始化LinkedHashMap。 |
4 | LinkedHashMap(int capacity,float fillRatio) 该构造函数初始化容量和填充率。 |
5 | LinkedHashMap(int capacity,float fillRatio,boolean order) 该构造函数使您可以指定是按插入顺序还是最后访问的顺序将元素存储在链表中。如果Order为true,则使用访问顺序。如果Order为false,则使用插入顺序。 |
除了从其父类继承的方法之外,LinkedHashMap还定义了以下方法-
Sr.No. | Method & Remark |
---|---|
1 | void clear() 从该Map中删除所有映射。 |
2 | boolean containsKey(Object key) 判断Map中是否包含key映射值 |
3 | Object get(Object key) 根据键(key)获取关联的值。 |
4 | protect boolean removeEldestEntry(Map.Entry eldest) 如果此Map应删除其最旧的条目,则返回true。 链接:https://www.learnfk.comhttps://www.learnfk.com/java/java-linkedhashmap-class.html 来源:LearnFk无涯教程网 |
以下程序说明了此集合支持的几种方法-
import java.util.*; public class LinkedHashMapDemo { public static void main(String args[]) { //创建哈希映射 LinkedHashMap lhm=new LinkedHashMap(); //将元素放入映射 lhm.put("Learnfk", new Double(3434.34)); lhm.put("Mahnaz", new Double(123.22)); lhm.put("Ayan", new Double(1378.00)); lhm.put("Daisy", new Double(99.22)); lhm.put("Qadir", new Double(-19.08)); //获取一组集合 Set set=lhm.entrySet(); //获取迭代器 Iterator i=set.iterator(); //显示元素 while(i.hasNext()) { Map.Entry me=(Map.Entry)i.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } System.out.println(); //将 1000 存入 Learnfk 的账户 double balance=((Double)lhm.get("Learnfk")).doubleValue(); lhm.put("Learnfk", new Double(balance + 1000)); System.out.println("Learnfk's new balance: " + lhm.get("Learnfk")); } }
这将产生以下输出-
Learnfk: 3434.34 Mahnaz: 123.22 Ayan: 1378.0 Daisy: 99.22 Qadir: -19.08 Learnfk's new balance: 4434.34
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)