1

What i trying to achieve is my Set having the same order as my List which is chaging when i assign value from List to Set. 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?

Ke Vin
  • 3,478
  • 11
  • 60
  • 91

1 Answers1

1

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

Viktor Seifert
  • 636
  • 1
  • 7
  • 17
  • oh sorry i going to accept your answer even it's a duplicate, the question bit different, hope someone find a same mistake can come here, thank you – Ke Vin Aug 24 '20 at 09:41