OnJava8-Examples/collectiontopics/CanonicalMapping.java

33 lines
967 B
Java
Raw Normal View History

2016-12-30 22:22:39 -08:00
// collectiontopics/CanonicalMapping.java
// (c)2021 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
// Demonstrates WeakHashMap
2015-06-15 17:47:35 -07:00
import java.util.*;
import java.util.stream.*;
2015-06-15 17:47:35 -07:00
public class CanonicalMapping {
static void showKeys(Map<String, String> m) {
// Display sorted keys
List<String> keys = new ArrayList<>(m.keySet());
Collections.sort(keys);
System.out.println(keys);
}
2015-06-15 17:47:35 -07:00
public static void main(String[] args) {
int size = 100;
String[] savedKeys = new String[size];
WeakHashMap<String,String> map =
2017-01-22 16:48:11 -08:00
new WeakHashMap<>();
2015-06-15 17:47:35 -07:00
for(int i = 0; i < size; i++) {
String key = String.format("%03d", i);
String value = Integer.toString(i);
2015-06-15 17:47:35 -07:00
if(i % 3 == 0)
savedKeys[i] = key; // Save as "real" references
map.put(key, value);
2015-06-15 17:47:35 -07:00
}
showKeys(map);
2015-06-15 17:47:35 -07:00
System.gc();
showKeys(map);
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}