File tree Expand file tree Collapse file tree
src/test/java/com/appxiom/ax/tuple/test Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -96,4 +96,27 @@ public void testEqualsAndHashCode() {
9696 assertEquals (nt1 .hashCode (), nt2 .hashCode ());
9797 assertNotEquals (nt1 .hashCode (), nt3 .hashCode ());
9898 }
99+
100+ /**
101+ * Tests the usage of NamedTuple as a key in a HashMap.
102+ */
103+ @ Test
104+ public void testHashMapUsage () {
105+ java .util .HashMap <NamedTuple , String > map = new java .util .HashMap <>();
106+ NamedTuple key1 = NamedTuple .of (Map .of ("id" , 1 ));
107+ NamedTuple key2 = NamedTuple .of (Map .of ("id" , 1 ));
108+ NamedTuple key3 = NamedTuple .of (Map .of ("id" , 2 ));
109+
110+ map .put (key1 , "value1" );
111+
112+ assertTrue (map .containsKey (key1 ));
113+ assertTrue (map .containsKey (key2 )); // Different object, same content
114+ assertEquals ("value1" , map .get (key2 ));
115+
116+ map .put (key2 , "value2" );
117+ assertEquals (1 , map .size ());
118+ assertEquals ("value2" , map .get (key1 ));
119+
120+ assertFalse (map .containsKey (key3 ));
121+ }
99122}
Original file line number Diff line number Diff line change @@ -109,4 +109,27 @@ public void testEqualsAndHashCode() {
109109 assertEquals (t1 .hashCode (), t2 .hashCode ());
110110 assertNotEquals (t1 .hashCode (), t3 .hashCode ());
111111 }
112+
113+ /**
114+ * Tests the usage of Tuple as a key in a HashMap.
115+ */
116+ @ Test
117+ public void testHashMapUsage () {
118+ java .util .HashMap <Tuple , String > map = new java .util .HashMap <>();
119+ Tuple key1 = Tuple .of ("a" , 1 );
120+ Tuple key2 = Tuple .of ("a" , 1 );
121+ Tuple key3 = Tuple .of ("b" , 2 );
122+
123+ map .put (key1 , "value1" );
124+
125+ assertTrue (map .containsKey (key1 ));
126+ assertTrue (map .containsKey (key2 )); // Different object, same content
127+ assertEquals ("value1" , map .get (key2 ));
128+
129+ map .put (key2 , "value2" );
130+ assertEquals (1 , map .size ());
131+ assertEquals ("value2" , map .get (key1 ));
132+
133+ assertFalse (map .containsKey (key3 ));
134+ }
112135}
You can’t perform that action at this time.
0 commit comments