on 02-16-2010 7:10 AM
hi,
I am using a hash map which is showing the data with out sorting.
I am able to sort it using the tree map, but the code is sorting according to the id or key and i want to display the data by sorting according to the Text.
Regards,
Sanjyoti.
Hi Sanjyoti,
My team have some code regarding this that they have used for some functionality, hope this help.
========Sort based on the keys ========
Map yourMap= new HashMap();
// put some tuples in yourMap ...
Map sortedMap = new TreeMap(yourMap);
=====To sort only the keys : ========
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
Map<String,java.io.File> Val = new HashMap<String,java.io.File>();
Val.put("Roshan", new java.io.File("Roshan.gif"));
Val.put("Sandy", new java.io.File("Sandy.gif"));
Val.put("Dhiraj", new java.io.File("Dhiraj.gif"));
Val.put("Laxmi", new java.io.File("Laxmi.gif"));
Val.put("Vipul", new java.io.File("Vipul.gif"));
Val.put("Shashank", new java.io.File("SHASHANK.gif"));
SortedSet<String> sortedset= new TreeSet<String>(Val.keySet());
Iterator<String> it = sortedset.iterator();
while (it.hasNext()) {
System.out.println (it.next());
}
...
--------------------------------------------------------------------------------
==========Sort based on the values ======
HashMap yourMap = new HashMap();
// put some tuples in yourMap ...
// to hold the result
HashMap map = new LinkedHashMap();
List yourMapKeys = new ArrayList(yourMap.keySet());
List yourMapValues = new ArrayList(yourMap.values());
TreeSet sortedSet = new TreeSet(yourMapValues);
Object[] sortedArray = sortedSet.toArray();
int size = sortedArray.length;
for (int i=0; i<size; i++) {
map.put
(yourMapKeys.get(yourMapValues.indexOf(sortedArray<i>)),
sortedArray<i>);
}
=======To iterate your new Sorted Map ===
Set ref = map.keySet();
Iterator it = ref.iterator();
while (it.hasNext()) {
String file = (String)it.next();
Regards,
Roshan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Sanjyoti,
Following example demonstrate getting a List of values out of a Map where you'd like the contents to be ordered by Value (not Key).
import java.text.MessageFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.log4j.Logger;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* Sorting a Map<String,Long> by Values
*
* @author ableasdale
*
*/
public class TestOrderingMapItems {
private static final Logger LOG = Logger
.getLogger(TestOrderingMapItems.class);
private Map<String, Long> map;
@BeforeClass
public void setUpOnce() throws Exception {
LOG.info("Set up");
map = new HashMap<String, Long>();
map.put("A", 5L);
map.put("B", 4L);
map.put("C", 10L);
map.put("D", 6L);
}
@Test
public void getItemsByKeyInOrder() throws Exception {
List<Map.Entry<String, Long>> resultList = new LinkedList<Map.Entry<String, Long>>(
map.entrySet());
Collections.sort(resultList, new Comparator<Object>() {
public int compare(Object o1, Object o2) {
// Switch o2 and o1 to order from lowest to highest
return ((Comparable<Object>) ((Map.Entry<?, ?>) (o2))
.getValue()).compareTo(((Map.Entry<?, ?>) (o1))
.getValue());
}
});
LOG.info("Ordered list as follows:");
for (Object e : resultList) {
LOG.info(MessageFormat.format("{0} | {1}", ((Entry<?, ?>) e).getKey(), ((Entry<?, ?>) e).getValue()));
}
}
}
Warm Regards
Upendra Agarwal
Edited by: Upendra Agrawal on Feb 16, 2010 10:23 AM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Dear Sanjyoti,
As suggessted by roshan...may, this one will help..
HashMap yourMap = new HashMap();
// put some tuples in yourMap .....
yourMap.put("TextA",5);
yourMap.put("TextC",4);
yourMap.put("TextD",3);
yourMap.put("TextB",2);
yourMap.put("TextE",1);
List yourMapKeys = new ArrayList(yourMap.keySet());
List yourMapValues = new ArrayList(yourMap.values());
TreeSet sortedSet = new TreeSet(yourMapValues);
Object[] sortedArray = sortedSet.toArray();
int size = sortedArray.length;
// to hold the result....
HashMap map = new LinkedHashMap();
//ascending sort......
for (int i=0; i<size; i++) {
map.put
(yourMapKeys.get(yourMapValues.indexOf(sortedArray<i>)),
sortedArray<i>);
}
// iterate your new sorted map
Set ref = map.keySet();
Iterator it = ref.iterator();
while (it.hasNext()) {
String file = (String)it.next();
}
Warm Regards,
Upendra Agrawal
User | Count |
---|---|
86 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.