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

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

Getting Data from Arraylist and input into put method of LinkedHashMap

问题

  1. public Map<String, String> convertMap(ArrayList<String> Data){
  2. Map<String, String> myLinkedHashMap = new LinkedHashMap<String, String>();
  3. myLinkedHashMap.put("1", "first");
  4. myLinkedHashMap.put("2", "second");
  5. myLinkedHashMap.put("3", "third");
  6. return myLinkedHashMap;
  7. }

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?

  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. public Map&lt;String,String&gt; convertMap(ArrayList&lt;String&gt; Data){
  5. Map&lt;String,String&gt; myLinkedHashMap = new LinkedHashMap&lt;String, String&gt;();
  6. myLinkedHashMap.put(&quot;1&quot;, &quot;first&quot;);
  7. myLinkedHashMap.put(&quot;2&quot;, &quot;second&quot;);
  8. myLinkedHashMap.put(&quot;3&quot;, &quot;third&quot;);
  9. return myLinkedHashMap;
  10. }
  11. 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.
  12. Lets say Arraylist&lt;String&gt; Data contains:
  13. Name: John
  14. Age: 20
  15. Gender: Male
  16. I wan to replace &#39;Name&#39; at the &quot;1&quot; Column , and &#39;John&#39; at the &quot;First&quot; Column.
  17. Can anyone kindly guide me on this issue ?
  18. </details>
  19. # 答案1
  20. **得分**: 0
  21. 让我们假设你的 ArrayList `list` 包含:

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

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

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

  1. 我在第二个字符串上使用了 `trim()` 来移除任何多余的空格。
  2. **注意:**
  3. 这并不是很优雅的解决方案。这只允许特定格式的字符串,但如果它们确保是符合格式的,那么这个方法会起作用。
  4. <details>
  5. <summary>英文:</summary>
  6. Let&#39;s say your ArrayList `list` contains:

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

  1. 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"
}

  1. I use `trim()` on the second String to remove any excess whitespaces.
  2. **Note:**
  3. This is not pretty. This only allows Strings in a certain format, but will do the job if they 100% are.
  4. </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:

确定