如何在 HashMap 中打印小于或等于给定限制的数据库?
How to print a database in HashMap that is less than or equals to the given limit?
我正在尝试使用 HashMap
创建一个数据库,其中包含 Dinos 的名称和重量。我想在 printMatchingDinos()
方法上设置一个重量限制,这样只有重量小于或等于限制的恐龙才会被打印出来。
public class DinoDatabase {
private HashMap<String, Integer> listOfDinos = new HashMap<>();
private String name;
private int weight;
public DinoDatabase() {
Map<String, Integer> listOfDinos = new HashMap<String, Integer>();
this.name = name; // Dino's name
this.weight = weight; // Dino's weight
}
// Add dino to the database
public void addDino(String name, int weight) {
if (!listOfDinos.containsKey(name)) {
listOfDinos.put(name, weight);
System.out.println(name + " added. Weight: " + weight + "kg");
}
else {
System.out.println(name + " cannot be added. It is already in the database!");
}
}
// Print Dino which has a weight less than or equals to the limit
public void printMatchingDinos(int limit) {
if (weight > limit) {
listOfDinos.remove(name, weight);
for (String name : listOfDinos.keySet()) {
System.out.println(listOfDinos.get(name));
}
}
else {
System.out.println("No Dino with given standard");
}
}
public static void main(String[] args) {
DinoDatabase db = new DinoDatabase();
db.addDino("Dravidosaurus", 907);
db.addDino("Eouplocephalus", 3175);
db.addDino("Tyrex", 1000);
db.printMatchingDinos(1000);
}
}
/* Output:
Dravidosaurus added. Weight: 907kg
Eouplocephalus added. Weight: 3175kg
Tyrex added. Weight: 1000kg
No Dino with given standard */
预期输出为
Dravidosaurus added. Weight: 907kg
Tyrex added. Weight: 1000kg
你似乎对这个 class 应该代表什么很困惑。
你的 class 有一个 name
和 weight
字段,所以,显然,一个 DinoDatabase
实例代表 一只恐龙 .然而,它也有一个将名称映射到权重的映射,因此,它也代表了恐龙数据库的概念。
这没有意义。
你应该在这里有两个 class:public class Dino {String name; int weight; }
和 public class DinoDatabase { Map<String, Dino> dinos; }
。混在一起,这没有任何意义,而且让你很困惑。例如,在您的构造函数中,您编写 this.name = name;
什么都不做,它将 name 字段分配给自己。仅当存在名为 name
的参数时才有意义。你可以完全不做 Dino
class 并为你的恐龙坚持使用 Map<String, Integer>
,但这确实意味着你承诺只存储恐龙的重量并且你可以' 方便添加更多属性,比如他们生活在哪个时代,或者他们是否会飞。
鉴于您使用的是哈希图,因此您无法在此处应用 DB smarts;找到所有符合特定条件的恐龙的唯一方法是循环遍历整个地图和 return 所有匹配的条目,除非条件是:“谁的名字正好等于这个字符串”。实际的数据库更智能,因为你可以添加一个索引,这样即使你有数百万的恐龙,这样的查询也可以很快,但这涉及的编程概念比这段代码中发生的事情要复杂得多。我假设这个练习的目的只是为了简单地编写它,并循环遍历 每个 恐龙,只 return 匹配条件的恐龙。这意味着您需要制作一个 ArrayList
对象(或您选择的另一个集合),遍历,仅添加匹配项,然后 return 该集合。
目前printMatchingDinos
现在没有过滤任何东西。您无需删除任何内容即可打印出来。你可以迭代listOfDinos
的entrySet,打印的时候在循环里放一个条件过滤掉。
public void printMatchingDinos(int limit) {
for (Map.Entry<String, Integer> entry : listOfDinos.entrySet()) {
if (entry.getValue() <= limit)
System.out.println(entry.getKey() + "- Weight: " + entry.getValue() + "kg");
}
}
尽管您的要求与预期输出不符。这些行在调用 addDino
时打印。
建议:最好为Dino
创建一个class,字段为name
&weight
,DinoDatabase
可能包含[=24=的HashMap ] pair 作为一个字段,其中 Key 是唯一标识每个 Dino 的字段。
我正在尝试使用 HashMap
创建一个数据库,其中包含 Dinos 的名称和重量。我想在 printMatchingDinos()
方法上设置一个重量限制,这样只有重量小于或等于限制的恐龙才会被打印出来。
public class DinoDatabase {
private HashMap<String, Integer> listOfDinos = new HashMap<>();
private String name;
private int weight;
public DinoDatabase() {
Map<String, Integer> listOfDinos = new HashMap<String, Integer>();
this.name = name; // Dino's name
this.weight = weight; // Dino's weight
}
// Add dino to the database
public void addDino(String name, int weight) {
if (!listOfDinos.containsKey(name)) {
listOfDinos.put(name, weight);
System.out.println(name + " added. Weight: " + weight + "kg");
}
else {
System.out.println(name + " cannot be added. It is already in the database!");
}
}
// Print Dino which has a weight less than or equals to the limit
public void printMatchingDinos(int limit) {
if (weight > limit) {
listOfDinos.remove(name, weight);
for (String name : listOfDinos.keySet()) {
System.out.println(listOfDinos.get(name));
}
}
else {
System.out.println("No Dino with given standard");
}
}
public static void main(String[] args) {
DinoDatabase db = new DinoDatabase();
db.addDino("Dravidosaurus", 907);
db.addDino("Eouplocephalus", 3175);
db.addDino("Tyrex", 1000);
db.printMatchingDinos(1000);
}
}
/* Output:
Dravidosaurus added. Weight: 907kg
Eouplocephalus added. Weight: 3175kg
Tyrex added. Weight: 1000kg
No Dino with given standard */
预期输出为
Dravidosaurus added. Weight: 907kg
Tyrex added. Weight: 1000kg
你似乎对这个 class 应该代表什么很困惑。
你的 class 有一个 name
和 weight
字段,所以,显然,一个 DinoDatabase
实例代表 一只恐龙 .然而,它也有一个将名称映射到权重的映射,因此,它也代表了恐龙数据库的概念。
这没有意义。
你应该在这里有两个 class:public class Dino {String name; int weight; }
和 public class DinoDatabase { Map<String, Dino> dinos; }
。混在一起,这没有任何意义,而且让你很困惑。例如,在您的构造函数中,您编写 this.name = name;
什么都不做,它将 name 字段分配给自己。仅当存在名为 name
的参数时才有意义。你可以完全不做 Dino
class 并为你的恐龙坚持使用 Map<String, Integer>
,但这确实意味着你承诺只存储恐龙的重量并且你可以' 方便添加更多属性,比如他们生活在哪个时代,或者他们是否会飞。
鉴于您使用的是哈希图,因此您无法在此处应用 DB smarts;找到所有符合特定条件的恐龙的唯一方法是循环遍历整个地图和 return 所有匹配的条目,除非条件是:“谁的名字正好等于这个字符串”。实际的数据库更智能,因为你可以添加一个索引,这样即使你有数百万的恐龙,这样的查询也可以很快,但这涉及的编程概念比这段代码中发生的事情要复杂得多。我假设这个练习的目的只是为了简单地编写它,并循环遍历 每个 恐龙,只 return 匹配条件的恐龙。这意味着您需要制作一个 ArrayList
对象(或您选择的另一个集合),遍历,仅添加匹配项,然后 return 该集合。
目前printMatchingDinos
现在没有过滤任何东西。您无需删除任何内容即可打印出来。你可以迭代listOfDinos
的entrySet,打印的时候在循环里放一个条件过滤掉。
public void printMatchingDinos(int limit) {
for (Map.Entry<String, Integer> entry : listOfDinos.entrySet()) {
if (entry.getValue() <= limit)
System.out.println(entry.getKey() + "- Weight: " + entry.getValue() + "kg");
}
}
尽管您的要求与预期输出不符。这些行在调用 addDino
时打印。
建议:最好为Dino
创建一个class,字段为name
&weight
,DinoDatabase
可能包含[=24=的HashMap ] pair 作为一个字段,其中 Key 是唯一标识每个 Dino 的字段。