从ArrayList中获取数据并输入到LinkedHashMap的put方法中。

huangapple go评论65阅读模式
英文:

Getting Data from Arraylist and input into put method of LinkedHashMap

问题

public Map<String, String> convertMap(ArrayList<String> Data){
    Map<String, String> myLinkedHashMap = new LinkedHashMap<String, String>();

    myLinkedHashMap.put("1", "first");
    myLinkedHashMap.put("2", "second");
    myLinkedHashMap.put("3", "third");

    return myLinkedHashMap;
}

I am stucked on getting out the respective information from the ArrayList Data in order to use the put method to insert it into a linkedhashmap.

Lets say Arraylist Data contains:
Name: John
Age: 20
Gender: Male

I wan to replace 'Name' at the "1" Column , and 'John' at the "First" Column.

Can anyone kindly guide me on this issue?


<details>
<summary>英文:</summary>

i have this function below which takes in a arraylist of Strings and return a LinkedHashMap. I plan to use this linkedhashmap to write into a textfile subsequently. 


        public Map&lt;String,String&gt; convertMap(ArrayList&lt;String&gt; Data){
        Map&lt;String,String&gt; myLinkedHashMap = new LinkedHashMap&lt;String, String&gt;();

        myLinkedHashMap.put(&quot;1&quot;, &quot;first&quot;);
        myLinkedHashMap.put(&quot;2&quot;, &quot;second&quot;);
        myLinkedHashMap.put(&quot;3&quot;, &quot;third&quot;);

        return myLinkedHashMap;
        }

I am stucked on getting out the respective information from the ArrayList&lt;String&gt; Data in order to use the put method to insert it into a linkedhashmap. 

Lets say Arraylist&lt;String&gt; Data contains:
Name: John
Age: 20
Gender: Male

I wan to replace &#39;Name&#39; at the &quot;1&quot; Column , and &#39;John&#39; at the &quot;First&quot; Column.

Can anyone kindly guide me on this issue ?

</details>


# 答案1
**得分**: 0

让我们假设你的 ArrayList `list` 包含:

{"Name: John", "Age: 20", "Gender: Male"}

然后你可以循环遍历 ArrayList 中的每个元素,以获取每个单独的字符串。然后,你可以将字符串拆分成两部分,然后将这两部分都放入你的 HashMap 中:

for (String string : list) {
String[] result = string.split(":", 2); // "Name: John"
map.put(result[0], result[1].trim()); // 变为:"Name", "John"
}

我在第二个字符串上使用了 `trim()` 来移除任何多余的空格。

**注意:**
这并不是很优雅的解决方案。这只允许特定格式的字符串,但如果它们确保是符合格式的,那么这个方法会起作用。

<details>
<summary>英文:</summary>

Let&#39;s say your ArrayList `list` contains:

{"Name: John", "Age: 20", "Gender: Male"}

You then could loop trough every element in your ArrayList to get every single string. You could then split the string into 2, then put both parts into your HashMap:

for(String string : list) {
String[] result = string.split(":", 2); // "Name: John"
map.put(result[0], result[1].trim()); // becomes: "Name", "John"
}

I use `trim()` on the second String to remove any excess whitespaces.

**Note:**
This is not pretty. This only allows Strings in a certain format, but will do the job if they 100% are.

</details>



huangapple
  • 本文由 发表于 2020年4月9日 00:29:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/61105467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定