gpt4 book ai didi

java - 将 hashmap 的字符串值添加到 JComboBox

转载 作者:行者123 更新时间:2023-12-01 10:34:28 28 4
gpt4 key购买 nike

我是一个 Java 新手,我想从使用 hashmap 填充我的 JComboBox 的不同类中获取所有值

 public class StockData {

private static class Item {

Item(String n, double p, int q) {
name = n;
price = p;
quantity = q;
}

// get methods
public String getName() {
return name;
}

public double getPrice() {
return price;
}

public int getQuantity() {
return quantity;
}

// instance variables
private final String name;
private final double price;
private int quantity;
}

// with a Map you use put to insert a key, value pair
// and get(key) to retrieve the value associated with a key
// You don't need to understand how this works!
private final static Map<String, Item> stock = new HashMap();

static {
// if you want to have extra stock items, put them in here
// use the same style - keys should be Strings
stock.put("00", new Item("Bath towel", 5.50, 10));
stock.put("11", new Item("Plebney light", 20.00, 5));
stock.put("22", new Item("Gorilla suit", 30.00, 7));
stock.put("33", new Item("Whizz games console", 50.00, 8));
stock.put("44", new Item("Oven", 200.00, 4));
}
public static Map<String, Item> getStock() {
return stock;
}
public static String getName(String key) {
Item item = stock.get(key);
if (item == null) {
return null; // null means no such item
} else {
return item.getName();
}
}
}

我想将所有这些值(例如浴巾、Plebney 灯等)放入我的 JCombobox 中。

  package stock;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class PurchaseItem extends JFrame implements ActionListener {
// StockData ss = new StockData();
JComboBox<String> box = new JComboBox<>(stock.values().stream().map(Item::getName).toArray(String[]::new));
JComboBox b = new JComboBox();
}

最佳答案

假设您的 Item 对象有一个 getName 函数,您可以这样写:

    JComboBox<String> box = new JComboBox<>(
stock.values().stream()
.map(Item::getName)
.toArray(String[]::new)
);

我认为理解语法的最好方法是阅读所调用方法的 javadoc。还有一个小解释 .values() 返回 Stock map 中的一组所有值,然后按照文档所述进行流式传输,返回一个以集合作为源的顺序流,然后 map 调用 getName() 函数(我假设您有: public String getName()) 为流中的每个对象,然后将所有字符串转换为字符串数组。结果与以下相同:

    JComboBox<String> box = new JComboBox<>();
for(Item item:stock.values()){
box.addItem(item.getName());
}

编辑:

如果stock 变量位于不同的类中,则PurchaseItem 与PurchaseItem 无法看到它。您可以通过更改隐私或为其创建 get 函数来使其暴露:

   public static Map<String, Item> getStock() {
return stock;
}

由于我最初为您编写的函数需要 vlaues,因此您可以为 vlaue 集创建一个 getter,如下所示:

public static Collection<Item> getStock() {
return stock.values();
}

然后修改代码如下:

JComboBox<String> box = new JComboBox<>(
NameOfTheClassWhichContainsStock.getStock().stream()
.map(Item::getName)
.toArray(String[]::new)
);

编辑2:您的 Item 类是私有(private)的,因此它在 StockData 类之外不可见,这就是找不到它的原因。如果将 Item 类更改为 public 并在 PurchaseItem 类中导入 StockData,则可以编写:

JComboBox<String> box = new JComboBox<>(StockData.getStock()
.values()
.stream()
.map(StockData.Item::getName)
.toArray(String[]::new)
);

EDIT3:在我的电脑上注明:

package stockdata;

import java.util.HashMap;
import java.util.Map;

public class StockData {

public static class Item {

Item(String n, double p, int q) {
name = n;
price = p;
quantity = q;
}

// get methods
public String getName() {
return name;
}

public double getPrice() {
return price;
}

public int getQuantity() {
return quantity;
}

// instance variables
private final String name;
private final double price;
private int quantity;
}

// with a Map you use put to insert a key, value pair
// and get(key) to retrieve the value associated with a key
// You don't need to understand how this works!
private final static Map<String, Item> stock = new HashMap<>();

static {
// if you want to have extra stock items, put them in here
// use the same style - keys should be Strings
stock.put("00", new Item("Bath towel", 5.50, 10));
stock.put("11", new Item("Plebney light", 20.00, 5));
stock.put("22", new Item("Gorilla suit", 30.00, 7));
stock.put("33", new Item("Whizz games console", 50.00, 8));
stock.put("44", new Item("Oven", 200.00, 4));
}

public static Map<String, Item> getStock() {
return stock;
}

public static String getName(String key) {
Item item = stock.get(key);
if (item == null) {
return null; // null means no such item
} else {
return item.getName();
}
}
}

//================================================ ===================

package stockdata;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import stockdata.StockData;

public class PurchaseItem extends JFrame implements ActionListener {
// StockData ss = new StockData();
JComboBox<String> box = new JComboBox<>(StockData.getStock().values().stream().map(StockData.Item::getName).toArray(String[]::new));
JComboBox b = new JComboBox();

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub

}
}

编辑4:

public double getPriceForName(String itemName){

return StockData.getStock()
.values()
.stream()
.filter(item->item.getName().equals(itemName))//filter only those whit the given name
.mapToDouble(StockData.Item::getPrice)//get item price
.findFirst()//Item name should be unique then it is ok to do this
.getAsDouble();//if there is no item with the given name this will throw a NoSuchElementException
}

public int getQuantityForName(String itemName){

return StockData.getStock()
.values()
.stream()
.filter(item->item.getName().equals(itemName))//filter only those whit the given name
.mapToInt(StockData.Item::getQuantity)//get item quantity
.findFirst()//Item name should be unique then it is ok to do this
.getAsInt();//if there is no item with the given name this will throw a NoSuchElementException
}

关于java - 将 hashmap 的字符串值添加到 JComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34862364/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com