Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8369952

java.util.TreeMap clear causes unexpected results

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Ubuntu 24.04
      java version "21.0.5" 2024-10-15 LTS
      Java(TM) SE Runtime Environment (build 21.0.5+9-LTS-239)
      Java HotSpot(TM) 64-Bit Server VM (build 21.0.5+9-LTS-239, mixed mode, sharing)


      A DESCRIPTION OF THE PROBLEM :
      TreeMap somehow corrupts Java memory

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Issue can be reproduced easily using below unit test

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      there are two system.out.println() lines in the shared unit test, I expect to see same string to be printed on both system.out.println() lines
      ACTUAL -
      I see different strings printed on system.out.println() lines

      ---------- BEGIN SOURCE ----------
      @Test
          public void treeMapTest() {
              TreeMap<String, String> treeMap = new TreeMap<>();
              treeMap.put("2025-10-14T02:15:30", "A");
              treeMap.put("2025-10-24T02:15:30", "B");
              treeMap.put("2025-11-15T02:15:30", "C");
              treeMap.put("2025-12-15T02:15:30", "D");

              SortedMap<String, String> subMap = treeMap.subMap("2025-10-20T00:00:00", true, "2025-11-20T00:00:00", true);
              List<Map.Entry<String, String>> tempList = new ArrayList<>(subMap.entrySet());
              System.out.println("tempList = " + tempList);
              subMap.clear();
              System.out.println("tempList = " + tempList);
          }
      ---------- END SOURCE ----------

            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: