简体   繁体   English

与TreeMap和TreeSet排序不同步

[英]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: 我创建一个SortedMap像这样:

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: 在生成的JSON中看起来也不错:

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

afterwards I use this sortedMap to create a second part of the JSON like this: 之后,我使用此sortedMap创建JSON的第二部分,如下所示:

  /**
   * 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: JSON的这一部分如下所示:

"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: 如您所见,它们不匹配,因为我希望第二部分中的值与JSON的第一部分的顺序相匹配,如下所示:

  "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? 是因为我未正确将SortedSet与SortedMap结合使用?

You've used HashMap in your code which does not preserve the order of the items as mentioned in HashMap documentation 您已经在代码中使用了HashMap ,但未保留HashMap 文档中提到的项目顺序

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 使用LinkedHashMap保留项目的顺序,它应该可以解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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