简体   繁体   English

Java从数组列表中获取第二个元素,并将其用作哈希映射的值

[英]Java getting every second element from array list and using it as value for hash map

I have the following text file : 我有以下文本文件:

1 dog

2 bark

3 broccoli

4 vegetable

5 orange

6 fruit

7 shark

8 fish

9 cat

10 meow

11 cricket

12 chirp

I want to make a hash map with a key for every even number and a value for every odd number in the text file. 我想用文本文件中每个偶数的键和每个奇数的值制作一个哈希图。 This textfile will have more lines added over time so I do not want to hardcode it. 随着时间的推移,此文本文件将添加更多行,因此我不想对其进行硬编码。

I read the textile lines through a List and into an ArrayList of strings. 我通过列表读入纺织线,并读入字符串的ArrayList。

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Scanner;

public class brain {
    public List<String> Payload1() throws IOException {
        Scanner sc = new Scanner (new File("src/boot/Payload1.txt"));
        List<String> lines = new ArrayList<String>();
        while (sc.hasNextLine()) {
          lines.add(sc.nextLine());

        }

        return lines;
}

I then made a for loop that would get every even and odd number in a separate string using a for loop counter. 然后,我创建了一个for循环,该循环将使用for循环计数器在单独的字符串中获取每个偶数和奇数。

            String first = Hanes.Payload1().get(i);
            String second = null; 
            if(Hanes.Payload1().size() > i + 1) {
                second = Hanes.Payload1().get(i+1);
}
            System.out.println(first);
            System.out.println(second);
}

I'm not really sure how to implement it into a hash map like this: 我不太确定如何将其实现为如下所示的哈希映射:

private HashMap<String,String> predictionFeatureMapping = new LinkedHashMap<String,String>();


    public HashMap<String,String> predictionFeatureMapper() throws IOException {
        predictionFeatureMapping = new LinkedHashMap<String,String>();

        return predictionFeatureMapping;
    }
public Map<String, String> Payload1() throws IOException {
    Scanner sc = new Scanner (new File("src/boot/Payload1.txt"));
    Map<String, String> fileMap = new LinkedHashMap<String, String>();
    int counter = 1;
    String key = "";
    while (sc.hasNextLine()) {
        if(counter % 2 != 0) {
            key = sc.nextLine();
        }else {
            fileMap.put(key, sc.nextLine());
        }
        counter++;
    }
    sc.close();
    return fileMap;
}

You can just store the key value in a String and then use the put() function during the next line when you have the value to associate with it. 您可以只将键值存储在String中,然后在下一行有值与其关联时使用put()函数。 The modulus just determines whether you are on a key line or value line. 模数仅确定您是在关键线上还是在价值线上。 If you would still like to return a list of all the lines for some other use, then use the same logic to create your hashmap: 如果您仍想返回所有行的列表以供其他用途,请使用相同的逻辑来创建您的哈希图:

public Map<String, String> getFileMap(List<String> list){
    Map<String, String> fileMap = new LinkedHashMap<String, String>();
    String key = "";
    for(int x = 0; x < list.size(); x++) {
        if((x + 1 ) % 2 != 0) {
            key = list.get(x);
        }else {
            fileMap.put(key, list.get(x));
        }
    }
    return fileMap;
}

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

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