77import java .util .ArrayList ;
88import java .util .Arrays ;
99import java .util .List ;
10+ import java .util .concurrent .TimeUnit ;
1011
1112import org .apache .commons .collections4 .BidiMap ;
1213import org .apache .commons .collections4 .MultiValuedMap ;
14+ import org .apache .commons .collections4 .bidimap .DualHashBidiMap ;
15+ import org .apache .commons .collections4 .bidimap .DualTreeBidiMap ;
1316import org .apache .commons .collections4 .bidimap .TreeBidiMap ;
1417import org .apache .commons .collections4 .map .MultiKeyMap ;
1518import org .apache .commons .collections4 .multimap .ArrayListValuedHashMap ;
@@ -20,6 +23,7 @@ public class CollectionsUnitTest {
2023 private final static MultiValuedMap <String , String > groceryCart = new ArrayListValuedHashMap <>();
2124 private final static MultiKeyMap <String , String > days = new MultiKeyMap <String , String >();
2225 private final static MultiKeyMap <String , String > cityCoordinates = new MultiKeyMap <String , String >();
26+ private long start ;
2327
2428 static {
2529 daysOfWeek .put (1 , "Monday" );
@@ -106,4 +110,73 @@ public void givenCoordinatesMultiKeyMap_whenQueried_thenOK() {
106110
107111 }
108112
113+ @ Test
114+ public void givenTreeBidiMap_whenHundredThousandKeys_thenPerformanceNoted () {
115+ System .out .println ("**TreeBidiMap**" );
116+ BidiMap <Integer , Integer > map = new TreeBidiMap <>();
117+ start = System .nanoTime ();
118+ for (int i = 0 ; i < 100000 ; i ++) {
119+ Integer key = new Integer (i );
120+ Integer value = new Integer (i + 1 );
121+ map .put (key , value );
122+ }
123+ System .out .println ("Insertion time:" + TimeUnit .MILLISECONDS .convert (System .nanoTime () - start , TimeUnit .NANOSECONDS ));
124+
125+ start = System .nanoTime ();
126+ Integer value = (Integer ) map .get (new Integer (500 ));
127+ System .out .println ("Value:" + value );
128+ System .out .println ("Fetch time key:" + TimeUnit .MICROSECONDS .convert (System .nanoTime () - start , TimeUnit .NANOSECONDS ));
129+
130+ start = System .nanoTime ();
131+ Integer key = (Integer ) map .getKey (new Integer (501 ));
132+ System .out .println ("Key:" + key );
133+ System .out .println ("Fetch time value:" + TimeUnit .MICROSECONDS .convert (System .nanoTime () - start , TimeUnit .NANOSECONDS ));
134+ }
135+
136+ @ Test
137+ public void givenDualTreeBidiMap_whenHundredThousandKeys_thenPerformanceNoted () {
138+ System .out .println ("**DualTreeBidiMap**" );
139+ BidiMap <Integer , Integer > map = new DualTreeBidiMap <>();
140+ start = System .nanoTime ();
141+ for (int i = 0 ; i < 100000 ; i ++) {
142+ Integer key = new Integer (i );
143+ Integer value = new Integer (i + 1 );
144+ map .put (key , value );
145+ }
146+ System .out .println ("Insertion time:" + TimeUnit .MILLISECONDS .convert (System .nanoTime () - start , TimeUnit .NANOSECONDS ));
147+
148+ start = System .nanoTime ();
149+ Integer value = (Integer ) map .get (new Integer (500 ));
150+ System .out .println ("Value:" + value );
151+ System .out .println ("Fetch time key:" + TimeUnit .MICROSECONDS .convert (System .nanoTime () - start , TimeUnit .NANOSECONDS ));
152+
153+ start = System .nanoTime ();
154+ Integer key = (Integer ) map .getKey (new Integer (501 ));
155+ System .out .println ("Key:" + key );
156+ System .out .println ("Fetch time value:" + TimeUnit .MICROSECONDS .convert (System .nanoTime () - start , TimeUnit .NANOSECONDS ));
157+ }
158+
159+ @ Test
160+ public void givenDualHashBidiMap_whenHundredThousandKeys_thenPerformanceNoted () {
161+ System .out .println ("**DualHashBidiMap**" );
162+ BidiMap <Integer , Integer > map = new DualHashBidiMap <>();
163+ start = System .nanoTime ();
164+ for (int i = 0 ; i < 100000 ; i ++) {
165+ Integer key = new Integer (i );
166+ Integer value = new Integer (i + 1 );
167+ map .put (key , value );
168+ }
169+ System .out .println ("Insertion time:" + TimeUnit .MILLISECONDS .convert (System .nanoTime () - start , TimeUnit .NANOSECONDS ));
170+
171+ start = System .nanoTime ();
172+ Integer value = (Integer ) map .get (new Integer (500 ));
173+ System .out .println ("Value:" + value );
174+ System .out .println ("Fetch time key:" + TimeUnit .MICROSECONDS .convert (System .nanoTime () - start , TimeUnit .NANOSECONDS ));
175+
176+ start = System .nanoTime ();
177+ Integer key = (Integer ) map .getKey (new Integer (501 ));
178+ System .out .println ("Key:" + key );
179+ System .out .println ("Fetch time value:" + TimeUnit .MICROSECONDS .convert (System .nanoTime () - start , TimeUnit .NANOSECONDS ));
180+ }
181+
109182}
0 commit comments