Skip to content

Commit 10b3ae0

Browse files
committed
Map Utils
1 parent 17d829e commit 10b3ae0

File tree

3 files changed

+286
-1
lines changed

3 files changed

+286
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Below topics/problems are covered as of now.
156156
- [ ] ListUtils
157157
- [X] [BooleanUtils](../master/src/com/deepak/data/structures/Utils/BooleanUtils.java)
158158
- [ ] CollectionUtils
159-
- [ ] MapUtils
159+
- [X] [MapUtils](../master/src/com/deepak/data/structures/Utils/MapUtils.java)
160160
- [ ] DateUtils
161161

162162
**18. Iterators**
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* MapUtils.java
4+
*/
5+
package com.deepak.data.structures.Utils;
6+
7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.Comparator;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.Map.Entry;
14+
15+
/**
16+
* Utilities for Maps
17+
*
18+
* @author Deepak
19+
*/
20+
public class MapUtils {
21+
22+
/**
23+
* Method to check if a map is empty
24+
*
25+
* @param map
26+
* @return {@link boolean}
27+
*/
28+
public static <K, V> boolean isEmpty(Map<K, V> map) {
29+
if (map == null || map.size() == 0) {
30+
return true;
31+
}
32+
return false;
33+
}
34+
35+
/**
36+
* Method to invert a map i.e keys becomes values and vice versa
37+
*
38+
* @param map
39+
* @return {@link Map<V, K>}
40+
*/
41+
public static <K, V> Map<V, K> invert(Map<K, V> map) {
42+
if (map != null) {
43+
Map<V, K> returnValue = new HashMap<>();
44+
for (Entry<K, V> entry : map.entrySet()) {
45+
if (entry != null) {
46+
returnValue.put(entry.getValue(), entry.getKey());
47+
}
48+
}
49+
return returnValue;
50+
}
51+
return null;
52+
}
53+
54+
/**
55+
* Method to sort a map based on the comparator
56+
*
57+
* @param map
58+
* @param comparator
59+
* @return {@link Map<K, V>}
60+
*/
61+
public static <K, V> Map<K, V> sortedMap(Map<K, V> map, Comparator<K> comparator) {
62+
if (map != null && comparator != null) {
63+
Map<K, V> returnValue = new HashMap<>();
64+
List<K> list = new ArrayList<>();
65+
for (K key : map.keySet()) {
66+
list.add(key);
67+
}
68+
Collections.sort(list, comparator);
69+
for (int i = 0; i < list.size(); i++) {
70+
K key = list.get(i);
71+
returnValue.put(key, map.get(key));
72+
}
73+
return returnValue;
74+
}
75+
return null;
76+
}
77+
78+
/**
79+
* Method to convert a Map to Properties i.e Key=Value
80+
*
81+
* @param map
82+
* @return {@link List<String>}
83+
*/
84+
public static <K, V> List<String> toProperties(Map<K, V> map) {
85+
if (map != null) {
86+
List<String> propertiesList = new ArrayList<>();
87+
for (Entry<K, V> entry : map.entrySet()) {
88+
propertiesList.add(String.valueOf(entry.getKey())
89+
+ "=" + String.valueOf(entry.getValue()));
90+
}
91+
return propertiesList;
92+
}
93+
return null;
94+
}
95+
96+
/**
97+
* Method to convert a map to unmodifiable map
98+
*
99+
* @param map
100+
* @return {@link Map<K, V>}
101+
*/
102+
public static <K, V> Map<K, V> unmodifiableMap(Map<K, V> map) {
103+
if (map != null) {
104+
final Map<K, V> returnValue = new HashMap<>();
105+
for (Entry<K, V> entry : map.entrySet()) {
106+
final K key = entry.getKey();
107+
final V value = entry.getValue();
108+
returnValue.put(key, value);
109+
}
110+
}
111+
return null;
112+
}
113+
114+
/**
115+
* Method to check if a Map contains a key
116+
*
117+
* @param map
118+
* @param key
119+
* @return {@link boolean}
120+
*/
121+
public static <K, V> boolean containsKey(Map<K, V> map, K key) {
122+
if (map != null) {
123+
for (K k : map.keySet()) {
124+
if (k.equals(key)) {
125+
return true;
126+
}
127+
}
128+
}
129+
return false;
130+
}
131+
132+
/**
133+
* Method to check if map contains a value
134+
*
135+
* @param map
136+
* @param value
137+
* @return {@link boolean}
138+
*/
139+
public static <K, V> boolean containsValue(Map<K, V> map, V value) {
140+
if (map != null) {
141+
for (V v : map.values()) {
142+
if (v.equals(value)) {
143+
return true;
144+
}
145+
}
146+
}
147+
return false;
148+
}
149+
150+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* MapUtilsTest.java
4+
*/
5+
package com.deepak.data.structures.Utils;
6+
7+
import java.util.Comparator;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
import org.junit.Assert;
13+
import org.junit.Test;
14+
15+
/**
16+
* Test cases for Map utility functions
17+
*
18+
* @author Deepak
19+
*/
20+
public class MapUtilsTest {
21+
22+
/**
23+
* Test case to check if map is empty
24+
*/
25+
@Test
26+
public void testIsEmpty() {
27+
Map<String, String> map = new HashMap<>();
28+
Assert.assertTrue(MapUtils.isEmpty(map));
29+
map.put("A", "B");
30+
Assert.assertFalse(MapUtils.isEmpty(map));
31+
}
32+
33+
/**
34+
* Test case to check inversion of map
35+
*/
36+
@Test
37+
public void testInvertMap() {
38+
Map<String, String> map = new HashMap<>();
39+
Assert.assertTrue(MapUtils.isEmpty(map));
40+
map.put("A", "B");
41+
map.put("M", "N");
42+
map.put("X", "Y");
43+
Map<String, String> updatedMap = MapUtils.invert(map);
44+
/* Keys and values are inverted now. Searching by values */
45+
Assert.assertEquals(updatedMap.get("B"), "A");
46+
Assert.assertEquals(updatedMap.get("N"), "M");
47+
Assert.assertEquals(updatedMap.get("Y"), "X");
48+
}
49+
50+
/**
51+
* Test case to check sorting of map
52+
*/
53+
@Test
54+
public void testSortedMap() {
55+
Map<String, String> map = new HashMap<>();
56+
Assert.assertTrue(MapUtils.isEmpty(map));
57+
map.put("C", "Cat");
58+
map.put("B", "Ball");
59+
map.put("A", "Apple");
60+
MyComparator<String> comparator = new MyComparator<>();
61+
Map<String, String> updatedMap = MapUtils.sortedMap(map, comparator);
62+
for (String key : updatedMap.keySet()) {
63+
/* Since map is sorted now, A should reach on top */
64+
Assert.assertEquals(key, "A");
65+
break;
66+
}
67+
}
68+
69+
/**
70+
* Method to test conversion of map to properties
71+
*/
72+
@Test
73+
public void testToProperties() {
74+
Map<String, String> map = new HashMap<>();
75+
Assert.assertTrue(MapUtils.isEmpty(map));
76+
map.put("A", "Apple");
77+
map.put("B", "Ball");
78+
map.put("C", "Cat");
79+
List<String> properties = MapUtils.toProperties(map);
80+
Assert.assertEquals(properties.get(0), "A=Apple");
81+
Assert.assertEquals(properties.get(1), "B=Ball");
82+
Assert.assertEquals(properties.get(2), "C=Cat");
83+
}
84+
85+
/**
86+
* Method to test contains key
87+
*/
88+
@Test
89+
public void testContainsKey() {
90+
Map<String, String> map = new HashMap<>();
91+
Assert.assertTrue(MapUtils.isEmpty(map));
92+
map.put("A", "Apple");
93+
map.put("B", "Ball");
94+
map.put("C", "Cat");
95+
Assert.assertTrue(MapUtils.containsKey(map, "B"));
96+
Assert.assertTrue(MapUtils.containsKey(map, "C"));
97+
Assert.assertFalse(MapUtils.containsKey(map, "D"));
98+
}
99+
100+
/**
101+
* Method to test contains value
102+
*/
103+
@Test
104+
public void testContainsValue() {
105+
Map<String, String> map = new HashMap<>();
106+
Assert.assertTrue(MapUtils.isEmpty(map));
107+
map.put("A", "Apple");
108+
map.put("B", "Ball");
109+
map.put("C", "Cat");
110+
Assert.assertTrue(MapUtils.containsValue(map, "Ball"));
111+
Assert.assertTrue(MapUtils.containsValue(map, "Cat"));
112+
Assert.assertFalse(MapUtils.containsValue(map, "Dog"));
113+
}
114+
115+
/**
116+
* Comparator class which just handles string as of now.
117+
* This will sort strings in alphabetical order
118+
*
119+
* @author Deepak
120+
*
121+
* @param <T>
122+
*/
123+
public class MyComparator<T> implements Comparator<T> {
124+
125+
@Override
126+
public int compare(final T o1, final T o2) {
127+
if (o1 instanceof String && o2 instanceof String) {
128+
return o1.toString().compareTo(o2.toString());
129+
}
130+
return 0;
131+
}
132+
133+
}
134+
135+
}

0 commit comments

Comments
 (0)