[英]Sorting in a hashMap
我在整理此代码(在哈希图中)时遇到了一些麻烦-我还需要一些帮助来整理类似的代码集以获取double(价格)。 谢谢。
@SuppressWarnings("unused")
public class Inventory {
Scanner in = new Scanner(System.in);
ArrayList<Sellable> groceries;
HashMap<String, Integer> stock;
public Inventory() {
groceries = new ArrayList<Sellable>();
stock = new HashMap<String, Integer>();
//HARDCODING...:
Sellable n1 = new Produce("Corn", 3, 5.00);
Sellable n2 = new Snack("Natural Popcorn Seeds", 2.50);
Sellable n3 = new Produce("Potatoes", 3, 5.00);
Sellable n4 = new Snack("Organic Potato Chips", 2.50);
Sellable n5 = new Produce("Apples", 5, 1.75);
Sellable n6 = new Snack("Apple Juice - 128 oz.", 3.50);
Sellable n7 = new Produce("Oranges", 5, 1.75);
Sellable n8 = new Snack("Orange Juice - 128 oz.", 3.50);
//ADD TO HASHMAP
groceries.add(n1);
groceries.add(n2);
groceries.add(n3);
groceries.add(n4);
groceries.add(n5);
groceries.add(n6);
groceries.add(n7);
groceries.add(n8);
//PUT UP FOR PRINTING
stock.put(n1.getName(), 50);
stock.put(n2.getName(), 100);
stock.put(n3.getName(), 50);
stock.put(n4.getName(), 100);
stock.put(n5.getName(), 50);
stock.put(n6.getName(), 100);
stock.put(n7.getName(), 50);
stock.put(n8.getName(), 100);
}
//Sorting Method 1
@SuppressWarnings("unchecked")
public void sortByName() {
groceries = new ArrayList<Sellable>();
stock = new HashMap<String, Integer>();
{
if (stock != null) {
List<Sellable> groceries = new ArrayList<Sellable>();
stock.addAll(((Entry<String, Integer>) stock).getValue(), groceries);
Collections.sort(groceries, new Comparator<Sellable>() {
public int compare(Sellable product1, Sellable product2) {
try {
Sellable choice1 = (Sellable) product1;
Sellable choice2 = (Sellable) product2;
//LESS THAN
if (choice1.getName().compareTo(choice2.getName()) < 0) {
return -1;
} //GREATER THAN
else if (choice1.getName().compareTo(choice2.getName()) > 0) {
return 1;
} //EQUALS
else {
return 0;
}
} catch (ClassCastException FAIL) {
return -2;
}
}
});
}
}
}
}
请删除代码行,
groceries = new ArrayList<Sellable>();
stock = new HashMap<String, Integer>();
来自您的方法sortByName().
此groceries
和stock
重置为空。
对不起,但是您的代码或问题对我来说都没有多大意义。
您的sortByName
破坏了groceries
和stock
。
然后,显然是试图从(现在)空的HashMap复制到ArrayList。 但这是荒谬的,因为
您正在尝试使用不存在的方法( HashMap.addAll
),
您正在将HashMap
投射到Entry
,
等等。
Comparator
看起来似乎可以按“名称”排序,但是在ClassCastException事件中返回-2的代码是错误的。 您应该只是让异常传播...如果发生。 您实际上并没有在比较器中进行类型转换。 参数的类型正确。 (将可Sellable
铸造为可Sellable
Sellable
?为什么?)
想添加一个新答案而不是编辑我以前的答案。
请注意,除非确实需要,否则不要(重新)初始化方法中的成员变量。 我也使用String.compareTo()比较名称。
请在下面找到修改后的排序方法:
@SuppressWarnings("unchecked")
public void sortByName() {
// groceries = new ArrayList<Sellable>();
// stock = new HashMap<String, Integer>();
if (stock != null) {
//List<Sellable> groceries = new ArrayList<Sellable>();
//stock.addAll(((Entry<String, Integer>) stock).getValue(), groceries);
Collections.sort(groceries, new Comparator<Sellable>() {
public int compare(Sellable product1, Sellable product2) {
try {
return product1.getName().compareTo(product2.getName());
} catch (Exception FAIL) {
return -2;
}
}
});
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.