OnJava8-Examples/collectiontopics/LinkedHashMapDemo.java

31 lines
1.0 KiB
Java
Raw Normal View History

2016-12-30 22:22:39 -08:00
// collectiontopics/LinkedHashMapDemo.java
2016-12-30 17:23:13 -08:00
// (c)2017 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// What you can do with a LinkedHashMap
2015-06-15 17:47:35 -07:00
import java.util.*;
import onjava.*;
2015-06-15 17:47:35 -07:00
public class LinkedHashMapDemo {
public static void main(String[] args) {
LinkedHashMap<Integer,String> linkedMap =
2016-07-05 14:46:09 -06:00
new LinkedHashMap<>(new CountMap(9));
2015-11-03 12:00:44 -08:00
System.out.println(linkedMap);
2015-06-15 17:47:35 -07:00
// Least-recently-used order:
linkedMap = new LinkedHashMap<>(16, 0.75f, true);
2016-07-05 14:46:09 -06:00
linkedMap.putAll(new CountMap(9));
2015-11-03 12:00:44 -08:00
System.out.println(linkedMap);
2015-06-15 17:47:35 -07:00
for(int i = 0; i < 6; i++) // Cause accesses:
linkedMap.get(i);
2015-11-03 12:00:44 -08:00
System.out.println(linkedMap);
2015-06-15 17:47:35 -07:00
linkedMap.get(0);
2015-11-03 12:00:44 -08:00
System.out.println(linkedMap);
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
{0=A0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0, 6=G0, 7=H0, 8=I0}
{0=A0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0, 6=G0, 7=H0, 8=I0}
{6=G0, 7=H0, 8=I0, 0=A0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0}
{6=G0, 7=H0, 8=I0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0, 0=A0}
2015-09-07 11:44:36 -06:00
*/