fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12.  
  13.  
  14. Set < UUID > set =
  15. Collections.synchronizedSet(
  16. Collections.newSetFromMap(
  17. new WeakHashMap <>()
  18. )
  19. );
  20.  
  21. UUID uuid1 = UUID.fromString( "a8ee1e34-cead-11e8-a8d5-f2801f1b9fd1" );
  22. UUID uuid2 = UUID.fromString( "39bda2b4-5885-4f56-a900-411a49beebac" );
  23. UUID uuid3 = UUID.fromString( "0b630385-0452-4b96-9238-20cdce37cf55" );
  24. UUID uuid4 = UUID.fromString( "98d2bacf-3f7f-4ea0-9c17-c91f6702322c" );
  25.  
  26. System.out.println( "Size before adding: " + set.size() );
  27.  
  28. set.add( uuid1 );
  29. set.add( uuid2 );
  30. set.add( uuid3 );
  31. set.add( uuid4 );
  32.  
  33. System.out.println( "Size after adding 4 items: " + set.size() ); // Expect 4.
  34.  
  35. set.remove( uuid3 );
  36.  
  37. System.out.println( "Size after removing item # 3: " + set.size() ); // Expect 3.
  38.  
  39. uuid2 = null; // Release that UUID to garbage-collection.
  40. // System.gc(); // Ask the JVM to run the garbage-collection. Only a suggestion, may be ignored.
  41. // try {
  42. // Thread.sleep( 1_000 ); // Wait a moment, just for the heck of it.
  43. // } catch ( InterruptedException e ) {
  44. // e.printStackTrace();
  45. // }
  46.  
  47. System.out.println( "Size after making garbage of item # 2: " + set.size() ); // Expect 2.
  48.  
  49. }
  50. }
Success #stdin #stdout 0.08s 2184192KB
stdin
Standard input is empty
stdout
Size before adding: 0
Size after adding 4 items: 4
Size after removing item # 3: 3
Size after making garbage of item # 2: 3