在Java中对映射进行排序不起作用
我试图使用Comparator接口对树映射进行排序(Double作为一个值,Integer作为一个键),但它不起作用在Java中对映射进行排序不起作用,java,sorting,map,Java,Sorting,Map,我试图使用Comparator接口对树映射进行排序(Double作为一个值,Integer作为一个键),但它不起作用 // Create a tree map TreeMap tm = new TreeMap(); // Put elements to the map tm.put(1, new Double(3434.34)); tm.put(0, new Double(123.22)); tm.put(4, ne
// Create a tree map
TreeMap tm = new TreeMap();
// Put elements to the map
tm.put(1, new Double(3434.34));
tm.put(0, new Double(123.22));
tm.put(4, new Double(1378.00));
tm.put(2, new Double(99.22));
tm.put(3, new Double(-19.08));
List<Map.Entry> valueList = new ArrayList(tm.entrySet());
// Collections.sort(valueList, new Sort());
Collections.sort(valueList, new Sort());
HashMap sortedMap = new HashMap();
// Get an iterator
Iterator<Map.Entry> i = valueList.iterator();
// Display elements
while (i.hasNext()) {
Map.Entry object = i.next();
sortedMap.put(object.getKey(), object.getValue());
}
List sortedList = new ArrayList(sortedMap.entrySet());
Iterator<Map.Entry> iterator = sortedList.iterator();
while (iterator.hasNext()) {
Map.Entry entry = iterator.next();
System.out.println("Value " + entry.getValue() + "\n");
}
HashMap
本质上是无序的
相反,您可以首先使用比较器创建一个
TreeMap
。当您将它们放入HashMap
时,它们将不会保留其顺序,因为HashMap
不是有序映射。使用HashMap进行排序将不起作用,因为它不是列表。您需要查看ArrayList和排序方法
ArrayList<double> arr = new ArrayList<double>();
sort(arr) //sort ascending
ArrayList arr=new ArrayList();
排序(arr)//升序排序
如果要添加其他人已经建议的内容,请尝试以下操作:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class SortDemo
{
public class Sort implements Comparator<Map.Entry>
{
public int compare(Entry o1, Entry o2)
{
Double valueOne = (Double) o1.getValue();
Double valueTwo = (Double) o2.getValue();
return (int) Math.signum(valueOne.compareTo(valueTwo));
}
}
public static void main(String[] args)
{
new SortDemo().foo();
}
void foo()
{
TreeMap tm = new TreeMap();
tm.put(1, new Double(3434.34));
tm.put(0, new Double(123.22));
tm.put(4, new Double(1378.00));
tm.put(2, new Double(99.22));
tm.put(3, new Double(-19.08));
List<Map.Entry> valueList = new ArrayList(tm.entrySet());
Collections.sort(valueList, new Sort());
Iterator<Map.Entry> iterator = valueList.iterator();
while (iterator.hasNext())
{
Map.Entry entry = iterator.next();
System.out.println("Value: " + entry.getValue());
}
}
}
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.Comparator;
导入java.util.Iterator;
导入java.util.List;
导入java.util.Map;
导入java.util.Map.Entry;
导入java.util.TreeMap;
公共类SortDemo
{
公共类排序实现了Comparator
{
公共整数比较(条目o1、条目o2)
{
Double valueOne=(Double)o1.getValue();
Double valueTwo=(Double)o2.getValue();
return(int)Math.signum(valueOne.compareTo(valueTwo));
}
}
公共静态void main(字符串[]args)
{
新的SortDemo().foo();
}
void foo()
{
TreeMap tm=新的TreeMap();
tm.put(1,新双(3434.34);;
tm.put(0,新双)(123.22);;
标准普尔(4,新双)(1378.00);;
tm.put(2,新双)(99.22);;
标准普尔500指数(3,新双跌19.08);;
List valueList=newarraylist(tm.entrySet());
Collections.sort(valueList,new sort());
迭代器迭代器=valueList.Iterator();
while(iterator.hasNext())
{
Map.Entry=iterator.next();
System.out.println(“Value:+entry.getValue());
}
}
}
比较器仅适用于您的按键,而不适用于输入项。您需要一个比较器
,并将该比较器的实例传递给树映射
构造函数
TreeMap<Integer,Double> tm = new TreeMap<Integer,Double>(myIntegerComparator);
TreeMap tm=newtreemap(myIntegerComparator);
在您的示例中,您看到的行为是由于使用Integer
的标准比较而产生的TreeMap
(这是因为Integer
是可比较的
)
(顺便说一句,您还应该阅读泛型,并在collections类以及任何其他参数化类中使用它们。)您不应该将双值与
==
符号进行比较,而是尝试比较
和谢谢您的快速回答。我使用double类中的doubleValue()方法将值更改为double,但它仍然没有得到排序。请参阅我编辑的部分。JDK中没有允许您按值排序的Map
实现。可能存在重复的
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class SortDemo
{
public class Sort implements Comparator<Map.Entry>
{
public int compare(Entry o1, Entry o2)
{
Double valueOne = (Double) o1.getValue();
Double valueTwo = (Double) o2.getValue();
return (int) Math.signum(valueOne.compareTo(valueTwo));
}
}
public static void main(String[] args)
{
new SortDemo().foo();
}
void foo()
{
TreeMap tm = new TreeMap();
tm.put(1, new Double(3434.34));
tm.put(0, new Double(123.22));
tm.put(4, new Double(1378.00));
tm.put(2, new Double(99.22));
tm.put(3, new Double(-19.08));
List<Map.Entry> valueList = new ArrayList(tm.entrySet());
Collections.sort(valueList, new Sort());
Iterator<Map.Entry> iterator = valueList.iterator();
while (iterator.hasNext())
{
Map.Entry entry = iterator.next();
System.out.println("Value: " + entry.getValue());
}
}
}
TreeMap<Integer,Double> tm = new TreeMap<Integer,Double>(myIntegerComparator);