gpt4 book ai didi

java - 将逗号分隔的值放入集合对象中

转载 作者:行者123 更新时间:2023-12-01 23:53:54 24 4
gpt4 key购买 nike

我有一个查询字符串,它接收为一月、二月、三月

我想用逗号分割字符串,并将字符串放入HashMap中,这样当我检索时,我想获取字符串并对其进行空检查,如January,二月、三月

我该怎么做?

最佳答案

You can use the following code to store the months in a hash map.   

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.lang.String;

public class strings {
public static void main(String [] args){
String text = "jan,feb,march,april";
String[] keyValue = text.split(",");
Map<Integer, String> myMap = new HashMap<Integer, String>();
for (int i=0;i<keyValue.length;i++) {
myMap.put(i, keyValue[i]);
}
Set keys = myMap.keySet();
Iterator itr = keys.iterator();

Integer key;
String value;
while(itr.hasNext())
{
key = (Integer)itr.next();
value = (String)myMap.get(key);
System.out.println(key + " - "+ value);
}
}
}

The out put will be-
0 - jan
1 - feb
2 - march
3 - april


Further you can perform the check that you want

关于java - 将逗号分隔的值放入集合对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15897214/

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