如何防止 HashSet 改变我从 List 分配的数组顺序?

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

How to prevent HashSet to change my array order that i assign from List?

问题

我试图实现的是,我的 Set<ObjectClass> 拥有与我的 List<ObjectClass> 相同的顺序,当我从 List<ObjectClass> 分配值到 Set<ObjectClass> 时,顺序会发生变化。以下是我所做的:

// 在这里,我的数组仍然显示与我提交的前端相同
List<ShipmentAddressGroupingDtDto> submitedShipmentAddressGroupingDtDto = shipmentAddressGroupingDto.getShipmentAddressGroupingDtDto();

// 在这里,我的数组开始改变,它的顺序再次不同于上面的对象
Set<ShipmentAddressGroupingDtDto> setShipmentAddressGroupingDtDto = new HashSet(submitedShipmentAddressGroupingDtDto);
List<ShipmentAddressGroupingDtDto> shipmentAddressGroupingDtDto = new ArrayList<ShipmentAddressGroupingDtDto>(setShipmentAddressGroupingDtDto);

这是我的 submitedShipmentAddressGroupingDtDto 的值:

[
"id": 0,
"address": "Jl. Imam Bonjol",
"isActive": true,
"partnerShipmentId": 1
],
[
"id": 0,
"address": "Jl. Imam Bonjol",
"isActive": true,
"partnerShipmentId": 2
],
[
"id": 0,
"address": "Jl. Imam Bonjol",
"isActive": true,
"partnerShipmentId": 4
],
[
"id": 0,
"address": "Jl. Imam Bonjolx",
"isActive": true,
"partnerShipmentId": 7
]

以及在调试模式中我看到的,我的 setShipmentAddressGroupingDtDto 如下所示:

[
"id": 0,
"address": "Jl. Imam Bonjol",
"isActive": true,
"partnerShipmentId": 2
],
[
"id": 0,
"address": "Jl. Imam Bonjolx",
"isActive": true,
"partnerShipmentId": 7
],
[
"id": 0,
"address": "Jl. Imam Bonjol",
"isActive": true,
"partnerShipmentId": 1
],
[
"id": 0,
"address": "Jl. Imam Bonjol",
"isActive": true,
"partnerShipmentId": 4
]

我如何防止 Set 改变我的 List 的顺序?或者如果我无法防止,如何将其排序为我的提交值?

英文:

What i trying to achieve is my Set<ObjectClass> having the same order as my List<ObjectClass> which is chaging when i assign value from List<ObjectClass> to Set<ObjectClass>. Here is what i do :

// in here my array still showing same as my submited front end
List&lt;ShipmentAddressGroupingDtDto&gt; submitedShipmentAddressGroupingDtDto = shipmentAddressGroupingDto.getShipmentAddressGroupingDtDto();

//in here my array start changing, it&#39;s not the same order again as my object above
        Set&lt;ShipmentAddressGroupingDtDto&gt; setShipmentAddressGroupingDtDto = new HashSet(submitedShipmentAddressGroupingDtDto);
        List&lt;ShipmentAddressGroupingDtDto&gt; shipmentAddressGroupingDtDto = new ArrayList&lt;ShipmentAddressGroupingDtDto&gt;(setShipmentAddressGroupingDtDto);

here is my value for submitedShipmentAddressGroupingDtDto :

      [
        &quot;id&quot;: 0,
        &quot;address&quot;: &quot;Jl. Imam Bonjol&quot;,
        &quot;isActive&quot;: true,
        &quot;partnerShipmentId&quot;: 1
      ],
      [
        &quot;id&quot;: 0,
        &quot;address&quot;: &quot;Jl. Imam Bonjol&quot;,
        &quot;isActive&quot;: true,
        &quot;partnerShipmentId&quot;: 2
      ],
      [
        &quot;id&quot;: 0,
        &quot;address&quot;: &quot;Jl. Imam Bonjol&quot;,
        &quot;isActive&quot;: true,
        &quot;partnerShipmentId&quot;: 4
      ],
      [
        &quot;id&quot;: 0,
        &quot;address&quot;: &quot;Jl. Imam Bonjolx&quot;,
        &quot;isActive&quot;: true,
        &quot;partnerShipmentId&quot;: 7
      ]

and what i saw in debug mode, in my setShipmentAddressGroupingDtDto is like this :

      [
        &quot;id&quot;: 0,
        &quot;address&quot;: &quot;Jl. Imam Bonjol&quot;,
        &quot;isActive&quot;: true,
        &quot;partnerShipmentId&quot;: 2
      ],
      [
        &quot;id&quot;: 0,
        &quot;address&quot;: &quot;Jl. Imam Bonjolx&quot;,
        &quot;isActive&quot;: true,
        &quot;partnerShipmentId&quot;: 7
      ],
      [
        &quot;id&quot;: 0,
        &quot;address&quot;: &quot;Jl. Imam Bonjol&quot;,
        &quot;isActive&quot;: true,
        &quot;partnerShipmentId&quot;: 1
      ],
      [
        &quot;id&quot;: 0,
        &quot;address&quot;: &quot;Jl. Imam Bonjol&quot;,
        &quot;isActive&quot;: true,
        &quot;partnerShipmentId&quot;: 4
      ]

How do i prevent Set ordering my List? or if i can't prevent that, how can i sort it as my submited value?

答案1

得分: 1

你可以使用 LinkedHashSet,它会保持你放入其中元素的顺序。

英文:

You can use a LinkedHashSet which maintains the ordering of the elements you put into it.

huangapple
  • 本文由 发表于 2020年8月24日 16:59:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63557800.html
匿名

发表评论

匿名网友

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

确定