java - 根据我的顺序对 HashMap 进行排序

标签 java sorting hashmap

我有一个字符串序列和一个HashMap。我需要根据序列对 HashMap 进行排序。如果 HashMap 包含序列中存在的字符串,则这些字符串应根据序列进行排序并打印。

String sequence="People,Object,Environment,Message,Service";
HashMap<String, String> lhm = new HashMap<String, String>();
List<String> list=new ArrayList<String>();
lhm.put("Objectabc", "biu");
lhm.put("Message someText", "nuios");
lhm.put("Servicexyxyx", "sdfe");
lhm.put("People bcda", "dfdfh");
lhm.put("Environment qwer", "qwe");
lhm.put("Other", "names");
lhm.put("Elements", "ioup");            
lhm.put("Rand", "uiy");

// Get a set of the entries
Set<Entry<String, String>> set = lhm.entrySet();
String[] resultSequence=sequence.split(",");

for(int j=0;j<resultSequence.length;j++)
{
    Iterator<Entry<String, String>> iter = set.iterator();
    while(iter.hasNext()) {

       Map.Entry me = (Map.Entry)iter.next();
       String res=(String) me.getKey();

       if(res.contains(resultSequence[j]))
       {
           System.out.println("values according with the sequence is "+res);
       }
       if(!res.contains(resultSequence[j]))
       {
           list.add(res);
           // System.out.println("values not according with the sequence is "+res);
       }

    }  

 }

 List<String> list2=new ArrayList<String>(new LinkedHashSet<String>(list));

 Iterator<String> iterlist2=list2.iterator();
 while(iterlist2.hasNext())
 {
     System.out.println("non equal elements are "+iterlist2.next());
 }

我在这里得到的输出是

values according with the sequence is People bcda
values according with the sequence is Objectabc
values according with the sequence is Environment qwer
values according with the sequence is Message someText
values according with the sequence is Servicexyxyx
non equal elements are Elements
non equal elements are Other
non equal elements are Servicexyxyx
non equal elements are Objectabc
non equal elements are Message someText
non equal elements are Rand
non equal elements are Environment qwer
non equal elements are People bcda

我的预期输出:

values according with the sequence is People bcda
values according with the sequence is Objectabc
values according with the sequence is Environment qwer
values according with the sequence is Message someText
values according with the sequence is Servicexyxyx
non equal elements are Elements
non equal elements are Other
non equal elements are Rand

在我的代码中,我将不等于序列的元素存储到数组列表中并打印它。但是我无法正确设计循环,它只会添加不包含序列中字符串的剩余元素。帮我解决这个问题。谢谢

编辑:对于同样的问题,我尝试编写一个比较器。但它不起作用

Comparator<String> comparator = new Comparator<String>() {
             @Override
             public int compare(String key1, String key2) {
                 int returned = sequence.indexOf(key1) - sequence.indexOf(key2);

                 if (returned == 0 && !key1.contains(key2))
                     returned = -1;

                 return returned;

             }
         }; 

最佳答案

您的问题似乎是,您正在迭代序列,并且对于该序列中的每个元素,您正在迭代 map 并添加每个不匹配的元素。

我猜你想要这样的东西:

  • 创建 map 副本
  • 对于序列中的每个元素
    • 对于映射副本中的每个条目(此处使用迭代器,因为您必须对其调用remove())
      • 如果条目与序列元素匹配
        • 添加到列表
        • 从 map 副本中删除当前条目(这就是您需要该副本的原因)
  • 执行此操作后,列表将按顺序包含所有匹配元素,而 map 副本将包含所有不匹配元素

您的情况的问题是映射键和序列元素不完全匹配,否则您可以对其进行优化以实现更好的查找。

编辑:

另一种选择可能是使用 TreeMap 和查找包装器,如下所示:

String sequence = "People,Object,Environment,Message,Service";

Map<String, String> lhm = new TreeMap<String, String>();
lhm.put( "Objectabc", "biu" );
lhm.put( "Message someText", "nuios" );
lhm.put( "Servicexyxyx", "sdfe" );
lhm.put( "People bcda", "dfdfh" );
lhm.put( "Environment qwer", "qwe" );
lhm.put( "Other", "names" );
lhm.put( "Elements", "ioup" );
lhm.put( "Rand", "uiy" );

for( String element : sequence.split( "," ) )
{
  final String elem = element;

  //try to get the value and remove it in one step
  String value = lhm.remove( new Comparable<String>()
  {
    public int compareTo( String other )
    {
      if( other.contains( elem ) )
      {
        return 0;
      }

      return elem.compareTo( other );
    }
  } );

  if( value != null )
  {
    System.out.println("values according with the sequence (key:" + element + ") is " + value); 
  }
}

for( Map.Entry<String, String> e : lhm.entrySet())
{
  System.out.println("non equal elements are " + e.getKey() + " (value: " + e.getValue() + ")");
}

输出为:

values according with the sequence (key:People) is dfdfh
values according with the sequence (key:Object) is biu
values according with the sequence (key:Environment) is qwe
values according with the sequence (key:Message) is nuios
values according with the sequence (key:Service) is sdfe
non equal elements are Elements (value: ioup)
non equal elements are Other (value: names)
non equal elements are Rand (value: uiy)

请注意,contains(...) 调用嵌入在匿名比较器中。这样,您只需迭代一次,并且在每次迭代中进行二分搜索,而不是循环遍历所有剩余的映射条目。

关于java - 根据我的顺序对 HashMap 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18719609/

相关文章:

java - 编写整数数组的递归排序算法

java - 解析顶部元素包含对象列表的 XML 文档

ruby - 创建一个几乎排序的数组

java - JDO 无主多对多关系

Python numpy 参数排序

java - 即使该元素存在于 HashMap 中,HashMap.get() 方法也会返回 null

java - 使用值列表搜索 HashMap 的优化

java - 创建方法来汇总 Java 的 HashMap 中的所有值

java - 操作栏中的选项卡 Sherlock

java - 部署 Spring Boot + Angular 应用程序时出现的问题