skip to main
|
skip to sidebar
Code Crumbs
Wednesday, April 8, 2009
Map Utils
Merge Maps
public static List
> mergeMaps
(List
> list1, List
> list2, String primKey) {
List
> output = new ArrayList
>(list1.size());
Object key1, key2;
Map
map;
for(Map
map1:list1){
key1 = map1.get(primKey);
map = new HashMap
();
map.putAll(map1);
for(Map
map2:list2){
key2 = map2.get(primKey);
if(key1.equals(key2)){
map.putAll(map2);
break;
}
}
output.add(map);
}
return output;
}
Normalize Map
/**
* If the input list is like this:
* [
* {ID=3, TAG=Tag1, NAME=Name1},
* {ID=3, TAG=Tag2, NAME=Name1},
* {ID=3, TAG=Tag3, NAME=Name1},
* {ID=4, TAG=Tag4, NAME=Name2},
* {ID=4, TAG=Tag5, NAME=Name2},
* {ID=5, TAG=Tag6, NAME=Name3}
* ]
*
* The output list would look like this:
* [
* {ID=3, TAG=[Tag1, Tag2, Tag3], NAME=Name1},
* {ID=4, TAG=[Tag4, Tag5], NAME=Name2},
* {ID=5, TAG=[Tag6], NAME=Name3}
* ]
*
* @param list
* @param primKey (E.g.: ID)
* @param joinKey (E.g.: TAG)
* @param stripOtherCols - dictates if other fields in the map (like: NAME) needs to be stripped or preserved in output.
* @return
*/
@SuppressWarnings("unchecked")
public static List
> normalize
(List
> list, String primKey, String joinKey, boolean stripOtherCols) {
Map
> outputMap = new HashMap
>();
Map
innerOutputMap;
for(Map
innerInputMap:list){
if( (innerOutputMap = outputMap.get( innerInputMap.get(primKey).toString() ) ) == null ){
innerOutputMap = populateInnerOutputmap(innerInputMap, primKey, joinKey, stripOtherCols);
outputMap.put(innerInputMap.get(primKey).toString(),innerOutputMap);
}
if( isNotNull(innerInputMap.get(joinKey)) ){
List itemList = (List)innerOutputMap.get(joinKey);
itemList.add( innerInputMap.get(joinKey) );
}
}
List
> outputList =
(List)Arrays.asList( outputMap.values().toArray() );
return outputList;
}
De-normalize Key Value Pair
/**
* If the input list is like this:
* [
* {VAL=Val1, ID=3, KEY=Key1, NAME=Name1},
* {VAL=Val2, ID=3, KEY=Key2, NAME=Name1},
* {VAL=Val3, ID=3, KEY=Key3, NAME=Name1},
* {VAL=Val4, ID=4, KEY=Key4, NAME=Name1},
* {VAL=Val5, ID=4, KEY=Key5, NAME=Name1},
* {VAL=Val6, ID=5, KEY=Key6, NAME=Name1}
* ]
*
* The output list would look like this:
* [
* {ID=3, Key1=Val1, Key3=Val3, Key2=Val2, NAME=Name1},
* {ID=5, Key6=Val6, NAME=Name1},
* {ID=4, Key4=Val4, Key5=Val5, NAME=Name1}
* ]
*
* @param list
* @param primKey
* @param key
* @param val
* @param stripOtherCols - dictates if other fields in the map (like: NAME) needs to be stripped or preserved in output.
* @return
*/
@SuppressWarnings("unchecked")
public static List
> denormalizeKeyValuePair
(List
> list, String primKey, String key, String val, boolean stripOtherCols) {
Map
> outputMap = new HashMap
>();
Map
innerOutputMap;
for(Map
innerInputMap:list){
if( (innerOutputMap = outputMap.get( innerInputMap.get(primKey).toString() )) == null){
innerOutputMap = populateInnerOutputmap(innerInputMap, primKey, null, stripOtherCols);
outputMap.put(innerInputMap.get(primKey).toString(),innerOutputMap);
}
if( isNotNull(innerInputMap.get(key)) && isNotNull(innerInputMap.get(val))){
innerOutputMap.put((String)innerInputMap.get(key), innerInputMap.get(val));
}
}
List
> outputList =
(List)Arrays.asList( outputMap.values().toArray() );
return outputList;
}
Populate InnerOutputmap
private static Map
populateInnerOutputmap
(Map
innerInputMap, String primKey, String joinKey, boolean stripOtherCols){
Map
innerOutputMap = new HashMap
();
if( stripOtherCols ){
innerOutputMap.put(primKey, innerInputMap.get(primKey));
}else{
innerOutputMap.putAll(innerInputMap);
}
if(joinKey != null ){
innerOutputMap.put(joinKey, new ArrayList
());
}
return innerOutputMap;
}
isNotNull
private static boolean isNotNull(Object obj){
if(obj != null ){
//This special check for String is because of Oracle (probably all DBs have similar behavior)
//Null values are sent as String 'null'. Probably we can use something like NVL to avoind that.
//Once NVL stuff is tested, we can remove this 'null' check
if( obj instanceof String) {
if( !"null".equalsIgnoreCase( (String)obj ) & ((String)obj).length() > 0 ){
return true;
}else{
return false;
}
}else{
return true;
}
}else{
return false;
}
}
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
Followers
Blog Archive
▼
2009
(7)
▼
April
(7)
Java SXL Transformation
Spring Custom Extensions
Http Client Usage
Map Utils
Is Host Reachable
Get Local Host LAN Address
Spring Config
About Me
Sree
View my complete profile
No comments:
Post a Comment