Skip to content

Commit 65dd2e6

Browse files
author
Alan
committed
moddified maps
1 parent 58a14a6 commit 65dd2e6

8 files changed

+119
-81
lines changed

Maps/Caching.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

Maps/HashMap.java

-19
This file was deleted.

Maps/HashMapTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}

Maps/LinkedHashMap.java

-22
This file was deleted.

Maps/LinkedHashMapTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

Maps/Sorting.java

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
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.*;
132

14-
public class Main
3+
public class Sorting
154
{
165
public static void main (String[] args)
176
{

Maps/TreeMap.java

-27
This file was deleted.

Maps/TreeMapTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+

0 commit comments

Comments
 (0)