Java Code :
-------------
package com.test.data;
import java.util.*;
// @author - Lakshman.
public class DependencyMap {
private Map> dataMap = null;
private Map valueMap = new HashMap();
public DependencyMap(Map> argMap) {
if (argMap != null) {
dataMap = argMap;
}
else {
dataMap = new HashMap>();
}
}
public void createMap(String keyName, String keyVal) {
if (!dataMap.containsKey(keyName)) {
return;
}
// Check if value is already set in values map.
if (!valueMap.containsKey(keyName)) {
valueMap.put(keyName, keyVal);
}
// Get dependents.
ArrayList valList = dataMap.get(keyName);
if (valList == null) {
return;
}
// Increment the value.
int val = Integer.parseInt(keyVal);
val++;
String depVal = Integer.toString(val);
// For each dependent, set - value + 1
for (String depKey : valList) {
if (!valueMap.containsKey(depKey)) {
valueMap.put(depKey, new String(depVal));
}
}
// For each dependent, recursively traverse.
for (String depKey : valList) {
createMap(depKey, depVal);
}
}
public void displayValueMap() {
for (Map.Entry valEntry : valueMap.entrySet()) {
System.out.println("Key = " + valEntry.getKey() + " :: Value = " + valEntry.getValue());
}
}
public void displayDataMap() {
if (dataMap == null) {
return;
}
int count = 0;
for (Map.Entry> dataMapEntry : dataMap.entrySet()) {
count++;
System.out.print("Element number - " + count);
System.out.print (" - Key = " + dataMapEntry.getKey() + " :: Dependenct List = ");
ArrayList valList = dataMapEntry.getValue();
if (valList == null) {
System.out.println();
continue;
}
for (String val : valList) {
System.out.print(val + " ; ");
}
System.out.println();
}
}
private static HashMap> populateMap1() {
HashMap> dataMap = new HashMap>();
ArrayList valList = new ArrayList();
valList.add("B");
valList.add("C");
valList.add("E");
dataMap.put("A", valList);
valList = new ArrayList();
valList.add("C");
valList.add("D");
dataMap.put("B", valList);
valList = new ArrayList();
valList.add("E");
dataMap.put("C", valList);
dataMap.put("D", null);
dataMap.put("E", null);
return dataMap;
}
public static void testcase1() {
Map> dataMap = populateMap1();
DependencyMap depMap = new DependencyMap(dataMap);
depMap.displayDataMap();
depMap.createMap("A", "1");
depMap.displayValueMap();
}
public static void main(String[] args) {
testcase1();
}
}
Output
--------
Element number - 1 - Key = D :: Dependenct List =
Element number - 2 - Key = E :: Dependenct List =
Element number - 3 - Key = A :: Dependenct List = B ; C ; E ;
Element number - 4 - Key = B :: Dependenct List = C ; D ;
Element number - 5 - Key = C :: Dependenct List = E ;
Key = D :: Value = 3
Key = E :: Value = 2
Key = A :: Value = 1
Key = B :: Value = 2
Key = C :: Value = 2
-------------
package com.test.data;
import java.util.*;
// @author - Lakshman.
public class DependencyMap {
private Map
private Map
public DependencyMap(Map
if (argMap != null) {
dataMap = argMap;
}
else {
dataMap = new HashMap
}
}
public void createMap(String keyName, String keyVal) {
if (!dataMap.containsKey(keyName)) {
return;
}
// Check if value is already set in values map.
if (!valueMap.containsKey(keyName)) {
valueMap.put(keyName, keyVal);
}
// Get dependents.
ArrayList
if (valList == null) {
return;
}
// Increment the value.
int val = Integer.parseInt(keyVal);
val++;
String depVal = Integer.toString(val);
// For each dependent, set - value + 1
for (String depKey : valList) {
if (!valueMap.containsKey(depKey)) {
valueMap.put(depKey, new String(depVal));
}
}
// For each dependent, recursively traverse.
for (String depKey : valList) {
createMap(depKey, depVal);
}
}
public void displayValueMap() {
for (Map.Entry
System.out.println("Key = " + valEntry.getKey() + " :: Value = " + valEntry.getValue());
}
}
public void displayDataMap() {
if (dataMap == null) {
return;
}
int count = 0;
for (Map.Entry
count++;
System.out.print("Element number - " + count);
System.out.print (" - Key = " + dataMapEntry.getKey() + " :: Dependenct List = ");
ArrayList
if (valList == null) {
System.out.println();
continue;
}
for (String val : valList) {
System.out.print(val + " ; ");
}
System.out.println();
}
}
private static HashMap
HashMap
ArrayList
valList.add("B");
valList.add("C");
valList.add("E");
dataMap.put("A", valList);
valList = new ArrayList
valList.add("C");
valList.add("D");
dataMap.put("B", valList);
valList = new ArrayList
valList.add("E");
dataMap.put("C", valList);
dataMap.put("D", null);
dataMap.put("E", null);
return dataMap;
}
public static void testcase1() {
Map
DependencyMap depMap = new DependencyMap(dataMap);
depMap.displayDataMap();
depMap.createMap("A", "1");
depMap.displayValueMap();
}
public static void main(String[] args) {
testcase1();
}
}
Output
--------
Element number - 1 - Key = D :: Dependenct List =
Element number - 2 - Key = E :: Dependenct List =
Element number - 3 - Key = A :: Dependenct List = B ; C ; E ;
Element number - 4 - Key = B :: Dependenct List = C ; D ;
Element number - 5 - Key = C :: Dependenct List = E ;
Key = D :: Value = 3
Key = E :: Value = 2
Key = A :: Value = 1
Key = B :: Value = 2
Key = C :: Value = 2
No comments:
Post a Comment