繁体   English   中英

从txt文件读取到hashmap

[英]Read from txt file to hashmap

如何从文本文件读取字符串和int(Integer)并更新HashMap中的值? 文件包含:

比托克:2

波城(cesta de 2):1

blahblahblah:1

这是我到目前为止的内容:

  BufferedReader buffer = new BufferedReader(new FileReader("pedidos.txt"));

    String linha;

    int k = 1;
    String valor = null;
    try {
        while ((linha = buffer.readLine()) != null) {
            String[] Linha = linha.split(":");

            System.out.print(k + "-");
            k++;
            Linha[0] = Linha[0].trim();
            Linha[1] = Linha[1].trim();
            //prod.put(Linha[0], Integer.parseInt(Linha[1]));

            for (int i = 0; i < Linha.length; i++) {
                System.out.println(Linha[i] + " ");
                i++;
                int f = 0;
                if (f > linha.length()) {
                    System.out.println("Não existem pedidos");
                }}}

这是我最初的HashMap:

HashMap<String, Integer> prod = new HashMap<String, Integer>();
    prod.put("Queijo de Cabra", 0);
    prod.put("Queijo Fresco", 0);
    prod.put("Pão (cesta de 2)", 0);
    //PRATOS            
    prod.put("Bacalhau à Zé do Pipo", 0);
    prod.put("Bitoque", 0);
    prod.put("Salada de atum", 0);
    prod.put("Salada de Nabiças", 0);
    prod.put("Lasanha de vegetais", 0);
    //BEBIDAS            
    prod.put("Café", 0);
    prod.put("Coca-cola(lata)", 0);
    prod.put("7Up(lata)", 0);
    prod.put("Água do Luso 1.5L", 0);
    prod.put("Água do Luso ", 0);
    prod.put("Vinho da casa 1L (branco)", 0);
    prod.put("Vinho da casa 1L (tinto)", 0);
    //SOBREMESAS            
    prod.put("Bolo de bolacha", 0);
    prod.put("Banana", 0);
    prod.put("Melão", 0);
    //OUTROS            
    prod.put("Pau de canela", 0);
    prod.put("Bagaço", 0);
    prod.put("Pastilhas Gorila", 0);

如何为哈希图创建类别?

看一下这个:

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

public class main {

public static void main(String[] args) throws InterruptedException {
    main m = new main();
}

public void usingMaps() {
    String values = "something: 1";

    String[] parts = values.split(":");
    String key = parts[0].trim();
    Integer value = Integer.parseInt(parts[1].trim());
    HashMap<String, Integer> map = new HashMap<String, Integer>();

    map.put(key, value);

    for (Entry<String, Integer> entry : map.entrySet()) {
        System.out.println(entry.getKey());
        System.out.println(entry.getValue());
    }
}

public main() {
    usingMaps();
}
}

输出:

something
1

作为另一种方法,您可以使用简单的代码。 因此,简单的示例如下所示:

test.txt

test1:1
test2:2
test3:3

码:

Properties properties = new Properties();
properties.load(Main.class.getResourceAsStream("test.txt"));

Map map = properties;

Map<String, Integer> testMap = new HashMap<String, Integer>();
testMap.put("test0",11);
testMap.put("test00",111);

testMap.putAll(map);

System.out.println(testMap);

结果:

{test00=111, test1=1, test0=11, test2=2, test3=3}

编辑以模拟您拥有的代码。

哦! 我想我现在已经理解了您的问题:您在哈希图中有初始值,并且想要通过文本文件中的值来修改值(例如,如果文本文件中说您想增加2个值,则需要增加2点的东西?)

如果这就是您想要的,那么您非常亲密:

替换为:

//prod.put(Linha[0], Integer.parseInt(Linha[1]));

有了这个:

prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));

(这是一种快速而肮脏的方法,如果您不将哈希映射中的值初始化为0,它将失败。)可以通过测试这种情况来避免这种情况:

if (prod.containsKey(Linha[0])) {
    // update value
    prod.put(Linha[0], prod.get(Linha[0])+Integer.parseInt(Linha[1]));
} else {
    // first entry
    prod.put(Linha[0], Integer.parseInt(Linha[1]));
}

这样的优势之一是您可以随时添加新产品,而无需修改初始化地图的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM