import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import org.apache.poi.hssf.record.formula.functions.Weekday;
public class Aaaaaaaa {
public static void main(String args[]){
WeakHashMap hashMap=new WeakHashMap();
hashMap.put("5","Sreedhar5" );
hashMap.put("6","Sreedhar6" );
hashMap.put("2","Sreedhar2" );
hashMap.put("4","Sreedhar4" );
hashMap.put("7","Sreedhar7" );
hashMap.put("8","Sreedhar8" );
HashMap passedMap=new HashMap();
passedMap.put("5","Sreedhar5" );
passedMap.put("6","Sreedhar6" );
passedMap.put("2","Sreedhar2" );
passedMap.put("4","Sreedhar4" );
passedMap.put("7","Sreedhar7" );
passedMap=sortHashMapByValuesD(passedMap);
System.out.println(":::passedMap::::"+passedMap);
System.out.println("\n hashMap::::::"+hashMap);
System.out.println(":::::::"+sortHashMapByValues(passedMap, false));
}
public static LinkedHashMap sortHashMapByValuesD(HashMap passedMap) {
List mapKeys = new ArrayList(passedMap.keySet());
List mapValues = new ArrayList(passedMap.values());
Collections.sort(mapValues);
Collections.sort(mapKeys);
LinkedHashMap sortedMap =
new LinkedHashMap();
Iterator valueIt = mapValues.iterator();
while (valueIt.hasNext()) {
Object val = valueIt.next();
Iterator keyIt = mapKeys.iterator();
while (keyIt.hasNext()) {
Object key = keyIt.next();
String comp1 = passedMap.get(key).toString();
String comp2 = val.toString();
if (comp1.equals(comp2)){
passedMap.remove(key);
mapKeys.remove(key);
sortedMap.put((String)key, (String)val);
break;
}
}
}
return sortedMap;
}
public static LinkedHashMap sortHashMapByValues(HashMap passedMap, boolean ascending) {
//Asending and Desending
List mapKeys = new ArrayList(passedMap.keySet());
List mapValues = new ArrayList(passedMap.values());
Collections.sort(mapValues);
Collections.sort(mapKeys);
if (!ascending)
Collections.reverse(mapValues);
LinkedHashMap someMap = new LinkedHashMap();
Iterator valueIt = mapValues.iterator();
while (valueIt.hasNext()) {
Object val = valueIt.next();
Iterator keyIt = mapKeys.iterator();
while (keyIt.hasNext()) {
Object key = keyIt.next();
if (passedMap.get(key).toString().equals(val.toString())) {
passedMap.remove(key);
mapKeys.remove(key);
someMap.put(key, val);
break;
}
}
}
return someMap;
}
}
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import org.apache.poi.hssf.record.formula.functions.Weekday;
public class Aaaaaaaa {
public static void main(String args[]){
WeakHashMap hashMap=new WeakHashMap();
hashMap.put("5","Sreedhar5" );
hashMap.put("6","Sreedhar6" );
hashMap.put("2","Sreedhar2" );
hashMap.put("4","Sreedhar4" );
hashMap.put("7","Sreedhar7" );
hashMap.put("8","Sreedhar8" );
HashMap passedMap=new HashMap();
passedMap.put("5","Sreedhar5" );
passedMap.put("6","Sreedhar6" );
passedMap.put("2","Sreedhar2" );
passedMap.put("4","Sreedhar4" );
passedMap.put("7","Sreedhar7" );
passedMap=sortHashMapByValuesD(passedMap);
System.out.println(":::passedMap::::"+passedMap);
System.out.println("\n hashMap::::::"+hashMap);
System.out.println(":::::::"+sortHashMapByValues(passedMap, false));
}
public static LinkedHashMap sortHashMapByValuesD(HashMap passedMap) {
List mapKeys = new ArrayList(passedMap.keySet());
List mapValues = new ArrayList(passedMap.values());
Collections.sort(mapValues);
Collections.sort(mapKeys);
LinkedHashMap sortedMap =
new LinkedHashMap();
Iterator valueIt = mapValues.iterator();
while (valueIt.hasNext()) {
Object val = valueIt.next();
Iterator keyIt = mapKeys.iterator();
while (keyIt.hasNext()) {
Object key = keyIt.next();
String comp1 = passedMap.get(key).toString();
String comp2 = val.toString();
if (comp1.equals(comp2)){
passedMap.remove(key);
mapKeys.remove(key);
sortedMap.put((String)key, (String)val);
break;
}
}
}
return sortedMap;
}
public static LinkedHashMap sortHashMapByValues(HashMap passedMap, boolean ascending) {
//Asending and Desending
List mapKeys = new ArrayList(passedMap.keySet());
List mapValues = new ArrayList(passedMap.values());
Collections.sort(mapValues);
Collections.sort(mapKeys);
if (!ascending)
Collections.reverse(mapValues);
LinkedHashMap someMap = new LinkedHashMap();
Iterator valueIt = mapValues.iterator();
while (valueIt.hasNext()) {
Object val = valueIt.next();
Iterator keyIt = mapKeys.iterator();
while (keyIt.hasNext()) {
Object key = keyIt.next();
if (passedMap.get(key).toString().equals(val.toString())) {
passedMap.remove(key);
mapKeys.remove(key);
someMap.put(key, val);
break;
}
}
}
return someMap;
}
}
No comments:
Post a Comment