OnJava8-Examples/collectionsindepth/LinkedHashMapDemo.java

31 lines
1.0 KiB
Java
Raw Normal View History

2015-12-15 11:47:04 -08:00
// collectionsindepth/LinkedHashMapDemo.java
// (c)2016 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.
// Visit http://mindviewinc.com/Books/OnJava/ 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
*/