Java HashMap:如何为一个键添加多个值?

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

Java HashMap : How to add multiple values to one key?

问题

  1. dao获取了一个列表,我想把这个列表放入一个 `HashMap<Long, List<Object[]>>` 中,我的列表可以包含一个带有多个参数的服务,比如 `serviceId=3`
  2. 在我的最终HashMap中,结果看起来像这样: `{1, [100], 2, [101], 3, [[102, B], [103, B], [104, C]]}`。我尝试了这段代码,但它没有起作用。
  3. serviceId paramId type
  4. 1 100 A
  5. 2 101 A
  6. 3 102 B
  7. 3 103 B
  8. 3 104 C
  9. 代码:
  10. List result = dao.getServiceParam();
  11. HashMap<Long, List<Object[]>> mapArray = new HashMap<Long, List<Object[]>>();
  12. List<Object[]> listObj = new ArrayList<Object[]>();
  13. if (!result.isEmpty()) {
  14. for (int i = 0; i < result.size(); i++) {
  15. Object[] line = (Object[]) result.get(i);
  16. if ((BigDecimal) line[0] != null) {
  17. listObj.add(line);
  18. mapArray.put(new Long(((BigDecimal) line[0]).longValue()), listObj);
  19. }
  20. }
  21. }
英文:

I have a list from dao, I want to put this list in a HashMap&lt;Long,List&lt;Object[]&gt;&gt;, my list can contain a service which have multiple parameters like the serviceId=3.
In my final HashMap, the result looks like : {1,[100],2[101],3=[[102,B],[103,B],[104,C]]}. I tried with this code but it didn't work.

  1. serviceId paramId type
  2. 1 100 A
  3. 2 101 A
  4. 3 102 B
  5. 3 103 B
  6. 3 104 C

Code:

  1. List result = dao.getServiceParam();
  2. HashMap&lt;Long,List&lt;Object[]&gt;&gt; mapArray = new HashMap&lt;Long, List&lt;Object[]&gt;&gt;();
  3. List&lt;Object[]&gt; listObj = new ArrayList&lt;Object[]&gt;();
  4. if(!result.isEmpty()) {
  5. for (int i=0; i&lt; result.size(); i++) {
  6. Object[] line = (Object[])result.get(i);
  7. if ((BigDecimal) line[0]!=null) {
  8. istObj.add(line);
  9. mapArray .put(new Long(((BigDecimal) line[0]).longValue()), listObj);
  10. }
  11. }
  12. }

答案1

得分: 0

代替使用对象数组,创建您自己的POJO来表示数据,然后将其存储在列表中。然后,我们可以使用Collectors#grouping按serviceId进行分组,然后Collect到一个列表中的POJO对象列表。

  1. class POJO {
  2. private final long serviceId;
  3. private final int paramId;
  4. private final String type;
  5. // 构造函数,获取器
  6. }
  1. Map<Long, List<POJO>> map = new HashMap<>();
  1. Map<Long, List<POJO>> mapArray = result.stream()
  2. .collect(Collectors.groupingBy(POJO::getServiceId, Collectors.toList()));
英文:

Instead of using an array of Object, create your own POJO to represent the data then store it in a list. We can then take the List of POJO objects and create a map using Collectors#grouping to group by the serviceId and then Collect to a List.

  1. class POJO {
  2. private final long serviceId;
  3. private final int paramId;
  4. private final String type;
  5. // constructor, getters
  6. }
  1. Map&lt;Long, List&lt;POJO&gt;&gt; map = new HashMap&lt;&gt;();
  1. Map&lt;Long, List&lt;POJO&gt;&gt; mapArray = result.stream()
  2. .collect(Collectors.groupingBy(POJO::getServiceId, Collectors.toList()));

答案2

得分: 0

不要重复使用相同的 ArrayList 引用。创建 new ArrayList&lt;Object[]&gt;();,并将 line 添加到其中。操作如下:

  1. List result = dao.getServiceParam();
  2. HashMap&lt;Long,List&lt;Object[]&gt;&gt; mapArray = new HashMap&lt;Long, List&lt;Object[]&gt;&gt;();
  3. List&lt;Object[]&gt; listObj = null;
  4. Object[] line = null, nextLine = null;
  5. Long id = Long.valueOf(0), nextId = Long.valueOf(0);
  6. if(!result.isEmpty()) {
  7. for (int i=0; i&lt; result.size(); i++) {
  8. listObj = new ArrayList&lt;Object[]&gt;();
  9. do{
  10. line = (Object[])result.get(i);
  11. if ((BigDecimal) line[0]!=null) {
  12. listObj.add(line);
  13. id = new Long(((BigDecimal) line[0]).longValue());
  14. }
  15. i++;
  16. if(i&lt;result.size()){
  17. nextLine = (Object[])result.get(i);
  18. if ((BigDecimal) nextLine[0]!=null) {
  19. nextId = new Long(((BigDecimal) nextLine[0]).longValue());
  20. }
  21. }
  22. }while(id.equals(nextId));
  23. mapArray.put(id, listObj);
  24. i--;
  25. }
  26. }

如有疑问或问题,请随时进行评论。

英文:

Do not reuse the same reference of ArrayList. Create new ArrayList&lt;Object[]&gt;(); and add line to it. Do it as follows:

  1. List result = dao.getServiceParam();
  2. HashMap&lt;Long,List&lt;Object[]&gt;&gt; mapArray = new HashMap&lt;Long, List&lt;Object[]&gt;&gt;();
  3. List&lt;Object[]&gt; listObj = null;
  4. Object[] line = null, nextLine = null;
  5. Long id = Long.valueOf(0), nextId = Long.valueOf(0);
  6. if(!result.isEmpty()) {
  7. for (int i=0; i&lt; result.size(); i++) {
  8. listObj = new ArrayList&lt;Object[]&gt;();
  9. do{
  10. line = (Object[])result.get(i);
  11. if ((BigDecimal) line[0]!=null) {
  12. listObj.add(line);
  13. id = new Long(((BigDecimal) line[0]).longValue());
  14. }
  15. i++;
  16. if(i&lt;result.size()){
  17. nextLine = (Object[])result.get(i);
  18. if ((BigDecimal) nextLine[0]!=null) {
  19. nextId = new Long(((BigDecimal) nextLine[0]).longValue());
  20. }
  21. }
  22. }while(id.equals(nextId));
  23. mapArray.put(id, listObj);
  24. i--;
  25. }
  26. }

Feel free to comment in case of any doubt/issue.

答案3

得分: 0

  1. if (!result.isEmpty()) {
  2. for (int i = 0; i < result.size(); i++) {
  3. Object[] line = (Object[]) result.get(i);
  4. if (((BigDecimal) line[0]) != null) {
  5. Long longVal = new Long(((BigDecimal) line[0]).longValue());
  6. if (mapArray.containsKey(longVal)) {
  7. List<Object[]> tempList = mapArray.get(longVal);
  8. tempList.add(line);
  9. mapArray.put(longVal, tempList);
  10. } else {
  11. List<Object[]> tempList = new ArrayList<>();
  12. tempList.add(line);
  13. mapArray.put(longVal, tempList);
  14. }
  15. }
  16. }
  17. }
英文:
  1. if(!result.isEmpty()) {
  2. for (int i=0; i&lt; result.size(); i++) {
  3. Object[] line=(Object[])result.get(i);
  4. if((BigDecimal) line[0]!=null) {
  5. Long longVal = new Long(((BigDecimal) line[0]).longValue());
  6. if(mapArray.containsKey(longVal){
  7. List&lt;Object[]&gt; tempList = mapArray.get(longVal);
  8. tempList.add(line);
  9. mapArray.put(longVal, tempList);
  10. }
  11. else{
  12. List&lt;Object[]&gt; tempList = new ArrayList&lt;&gt;();
  13. tempList.add(line);
  14. mapArray.put(longVal,tempList);
  15. }
  16. }
  17. }
  18. }

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

发表评论

匿名网友

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

确定