File tree 8 files changed +119
-81
lines changed
8 files changed +119
-81
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ public class Caching
3
+ {
4
+ static void logLn (Object o ){System .out .println (o );}
5
+ static void log (Object o ){System .out .print (o );}
6
+ /**
7
+ * naive implementation, takes a long time
8
+ * @param n
9
+ * @return fibonaci of the number
10
+ */
11
+ static long fib (long n )
12
+ {
13
+ if (n ==0 )
14
+ return 0 ;
15
+
16
+ if (n == 1 || n == 2 )
17
+ return 1 ;
18
+
19
+ return fib (n - 1 ) + fib (n -2 );
20
+ }
21
+ /**
22
+ * optimized implementation using HashMap as cache
23
+ * @param n
24
+ * @return fibonaci of the number
25
+ */
26
+ static Map <Long , Long > cache = new HashMap <>();
27
+ static long optFib (long x )
28
+ {
29
+ long zero = 0 ;
30
+ long one = 1 ;
31
+ long two = 2 ;
32
+ cache .put (zero ,zero );
33
+ cache .put (one , one );
34
+ cache .put (two , one );
35
+
36
+ if (cache .containsKey (x ))
37
+ return cache .get (x );
38
+
39
+ cache .put (x , optFib (x -1 )+optFib (x -2 ));
40
+ return cache .get (x );
41
+ }
42
+
43
+ public static void main (String [] args )
44
+ {
45
+ for (long i =0 ; i <100 ; i ++)
46
+ {
47
+ logLn (i + ": " + optFib (i ));
48
+ }
49
+ }
50
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ public class HashMapTest
3
+ {
4
+ /**
5
+ * HashMap are not ordered
6
+ *
7
+ * */
8
+ public static void main (String [] args )
9
+ {
10
+ Map <String , Integer > m = new HashMap <>();
11
+ m .put ("Timothy" ,5 );
12
+ m .put ("Joey" ,10 );
13
+ m .put ("Dennis" ,66 );
14
+ m .put ("Varshika" ,-54 );
15
+ m .put ("Sanjna" ,-524 );
16
+ System .out .println (m );
17
+ }
18
+
19
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ /*
2
+ A linked Hash map maintains the order at which we declare the dictionary values NOT in alphabetical order
3
+ */
4
+ import java .util .*;
5
+ public class LinkedHashMapTest
6
+ {
7
+ /**
8
+ * LinkedHashMap maintain insertion order
9
+ *
10
+ * */
11
+ public static void main (String [] args )
12
+ {
13
+ Map <String , Integer > m = new LinkedHashMap <>();
14
+ m .put ("Timothy" ,5 );
15
+ m .put ("Joey" ,10 );
16
+ m .put ("Dennis" ,66 );
17
+ m .put ("Varshika" ,-54 );
18
+ m .put ("Sanjna" ,-524 );
19
+ System .out .println (m );
20
+
21
+ }
22
+ }
Original file line number Diff line number Diff line change 1
- import java .util .ArrayList ;
2
- import java .util .Arrays ;
3
- import java .util .HashMap ;
4
- import java .util .HashSet ;
5
- import java .util .LinkedHashMap ;
6
- import java .util .LinkedHashSet ;
7
- import java .util .LinkedList ;
8
- import java .util .Map ;
9
- import java .util .Scanner ;
10
- import java .util .Set ;
11
- import java .util .TreeMap ;
12
- import java .util .TreeSet ;
1
+ import java .util .*;
13
2
14
- public class Main
3
+ public class Sorting
15
4
{
16
5
public static void main (String [] args )
17
6
{
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+
2
+ import java .util .*;
3
+ public class TreeMapTest
4
+ {
5
+ /**
6
+ * TreeMaps maintain sorted order by keys
7
+ *
8
+ * */
9
+ public static void main (String [] args )
10
+ {
11
+
12
+ Map <String , Integer > m = new TreeMap <>();
13
+ m .put ("Timothy" ,5 );
14
+ m .put ("Joey" ,10 );
15
+ m .put ("Dennis" ,66 );
16
+ m .put ("Varshika" ,-54 );
17
+ m .put ("Sanjna" ,-524 );
18
+ System .out .println (m );
19
+ }
20
+ }
21
+ /*
22
+ In the console we will have the sorted dictionary in
23
+ alphabetical order: Dennis(the key)=value of dennis,
24
+ Joey(the key)=value of Joey, Timothy(the key)=value of Timothy
25
+ */
26
+
You can’t perform that action at this time.
0 commit comments