2016-12-30 22:22:39 -08:00
|
|
|
// collectiontopics/CanonicalMapping.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (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.*;
|
2021-11-07 17:48:46 -07:00
|
|
|
import java.util.stream.*;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
public class CanonicalMapping {
|
2021-11-07 17:48:46 -07:00
|
|
|
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) {
|
2021-11-07 17:48:46 -07:00
|
|
|
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++) {
|
2021-11-07 17:48:46 -07:00
|
|
|
String key = String.format("%03d", i);
|
|
|
|
String value = Integer.toString(i);
|
2015-06-15 17:47:35 -07:00
|
|
|
if(i % 3 == 0)
|
2021-11-07 17:48:46 -07:00
|
|
|
savedKeys[i] = key; // Save as "real" references
|
|
|
|
map.put(key, value);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2021-11-07 17:48:46 -07:00
|
|
|
showKeys(map);
|
2015-06-15 17:47:35 -07:00
|
|
|
System.gc();
|
2021-11-07 17:48:46 -07:00
|
|
|
showKeys(map);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|