简体   繁体   中英

Sorting with TreeMap and TreeSet not syncronized

I have a problem with sorting two components and don't see why.

I create a SortedMap like this:

private static SortedMap<String, String> getMapping( List<LdapMapping> mapping )
  {
    SortedMap<String, String> fieldMapping = new TreeMap<>();
    for ( LdapMapping map : mapping )
    {
      fieldMapping.put( map.getInternalField(), map.getLdapField() );
    }
    return fieldMapping;
  }

which also looks good in the generated JSON:

  "head": [
    "emailAddress",
    "enabled",
    "firstName",
    "lastName",
    "name"
  ],

afterwards I use this sortedMap to create a second part of the JSON like this:

  /**
   * Gets the field values from only ldap.
   *
   * @param entry the entry
   * @param mapping the mapping
   * @return the field values
   */
  private static Map<String, Boolean> getFieldValues( SearchResultEntry entry, Map<String, String> mapping )
  {
    SortedSet<String> keys = new TreeSet<>( mapping.keySet() );
    Map<String, Boolean> fields = new HashMap<>();

    for ( String key : keys )
    {
      String ldapField = mapping.get( key );
      String ldapValue = entry.getAttributeValue( ldapField );
      fields.put( ldapValue, Boolean.FALSE );
    }

    return fields;
  }

  ldapUser.setFields( getFieldValues( entry, fieldMapping ) );
  ldapUser.setAction( IMPORT_STATUS_NEW );

The second part result get's put into the Object

/**
 * The Class LdapUserImpl.
 */
public class LdapUserImpl implements LdapUser
{

  /** The Constant serialVersionUID. */
  private static final long serialVersionUID = 1L;

  /** The action. */
  private String action;

  /** The fields. */
  private transient Map<String, Boolean> fields;

  public LdapUserImpl()
  {
    /* nothing special needed */
  }

  /** {@inheritDoc} */
  @Override
  public String getAction()
  {
    return action;
  }

  /** {@inheritDoc} */
  @Override
  public Map<String, Boolean> getFields()
  {
    return fields;
  }

  /** {@inheritDoc} */
  @Override
  public void setAction( String action )
  {
    this.action = action;
  }

  /** {@inheritDoc} */
  @Override
  public void setFields( Map<String, Boolean> data )
  {
    this.fields = data;
  }

}

this part of the JSON then looks like this:

"data": [
    {
      "action": "New User",
      "fields": {
        "emailAddress": false,
        "lastName": false,
        "firstName": false,
        "enabled": false,
        "name": false
      }
    },
    {
      "action": "New User",
      "fields": {
        "emailAddress": false,
        "enabled": false,
        "firstname": false,
        "name": false,
        "lastname": false
      }
    },

As you can see, they don't match, as I want the values in the second part to match with the order of the first part of the JSON like this:

  "head": [
    "emailAddress",
    "enabled",
    "firstName",
    "lastName",
    "name"
  ],
  "data": [
    {
      "action": "New User",
      "fields": {
        "emailAddress": false,
        "enabled": false,
        "firstname": false,
        "lastname": false,
        "name": false
      }
    },
    {
      "action": "New User",
      "fields": {
        "emailAddress": false,
        "enabled": false,
        "firstname": false,
        "lastname": false,
        "name": false
      }
    }]

Note: naturally I replaced the real values with the field that get's displayed.

Where is the Problem with creating the second part? Is it because I use the SortedSet not correctly in combination with the SortedMap?

You've used HashMap in your code which does not preserve the order of the items as mentioned in HashMap documentation

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

Use a LinkedHashMap to preserve the order of the items and it should resolve your issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM