英文:
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<ShipmentAddressGroupingDtDto> submitedShipmentAddressGroupingDtDto = shipmentAddressGroupingDto.getShipmentAddressGroupingDtDto();
//in here my array start changing, it's not the same order again as my object above
Set<ShipmentAddressGroupingDtDto> setShipmentAddressGroupingDtDto = new HashSet(submitedShipmentAddressGroupingDtDto);
List<ShipmentAddressGroupingDtDto> shipmentAddressGroupingDtDto = new ArrayList<ShipmentAddressGroupingDtDto>(setShipmentAddressGroupingDtDto);
here is my value for 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
]
and what i saw in debug mode, in my setShipmentAddressGroupingDtDto is like this :
[
"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
]
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论