英文:
Java String Split to implement a HashMap
问题
我有一个Java字符串,如下所示,并希望根据以下方式拆分它:
String toSplit = "Apple,Fruit,10|Potato,Vegetable,15:Apple,Fruit,15|Potato,Vegetable,10|Banana,Fruit,10";
基本上,我想从字符串中创建HashMap,从类型(苹果/香蕉)到它们的数量(10/15)等。我想要一个这种类型的HashMap,直到“:”之前,以及一个从“:”之后到字符串末尾的HashMap。
我该如何拆分字符串以使我能够实现这个HashMap,并且在拆分后如何将内容插入HashMap?
基本上,字符串的第一部分(直到“:”)是我需要购买的物品列表,带有数量。字符串的第二部分是我目标的物品列表。我需要输出每个物品数量的差异。
我是Java的新手,所以任何帮助将不胜感激!谢谢!
英文:
I have a string in Java as follows, and I wanted to split it according to the following
String toSplit = "Apple,Fruit,10|Potato,Vegetable,15:Apple,Fruit,15|Potato,Vegetable,10|Banana,Fruit,10"
Basically, I wanted to have hashMaps from String to Integer, from the type (Apple/Banana) to their quantity (10/15) etc. I wanted to have one hashmap of this type until the ":" and one for after the ":" till the end of the string.
How do I go about splitting the string in a way that will allow me to implement the hashmap, and would I insert the stuff into the hashmap after splitting it?
Basically, the first part of the string (until ":") is the list of stuff I need to bought, with the quantity. And the second part of the string is the list of stuff I my target was. I need to output the difference in quantities of each.
I'm new to Java, so any help would be greatly appreciated!
Thank you!
答案1
得分: 1
以下是翻译好的代码部分:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
    public static void main(String[] args) {
        String toSplit = "Apple,Fruit,10|Potato,Vegetable,15:Apple,Fruit,15|Potato,Vegetable,10|Banana,Fruit,10";
        List<Map<String, Integer>> arrayList = new ArrayList<>();
        // Split on :
        String[] lists = toSplit.split(":");
        for (String list : lists) {
            // Split on |
            String[] records = list.split("\\|");
            Map<String, Integer> map = new HashMap<>();
            for (String record : records) {
                // Split on comma
                String[] parts = record.split(",");
                map.put(parts[0], Integer.valueOf(parts[2]));
            }
            arrayList.add(map);
        }
        System.out.println(arrayList);
    }
}
输出:
[{Potato=15, Apple=10}, {Potato=10, Apple=15, Banana=10}]
英文:
First split on :, then on | and finally on , as shown below:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
	public static void main(String[] args) {
		String toSplit = "Apple,Fruit,10|Potato,Vegetable,15:Apple,Fruit,15|Potato,Vegetable,10|Banana,Fruit,10";
		List<Map<String, Integer>> arrayList = new ArrayList<>();
		// Split on :
		String[] lists = toSplit.split(":");
		for (String list : lists) {
			// Split on |
			String[] records = list.split("\\|");
			Map<String, Integer> map = new HashMap<>();
			for (String record : records) {
				// Split on comma
				String[] parts = record.split(",");
				map.put(parts[0], Integer.valueOf(parts[2]));
			}
			arrayList.add(map);
		}
		System.out.println(arrayList);
	}
}
Output:
[{Potato=15, Apple=10}, {Potato=10, Apple=15, Banana=10}]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论