当前位置: 首页 > 知识库问答 >
问题:

根据我的序列对hashmap进行排序

谢和颂
2023-03-14
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;

             }
         }; 

共有1个答案

厉钊
2023-03-14

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

我猜你想要这样的东西:

  • 创建映射的副本
  • 用于序列中的每个元素
    • 用于映射副本中的每个条目(此处使用迭代器,因为您必须对其调用remove())
      • 如果条目与sequenc元素匹配
        • 添加到列表
        • 从映射副本中删除当前条目(这就是您需要该副本的原因)
        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)
        

 类似资料:
  • 边走边学Java(Python背景)。简单的单词计数程序在Java7代码(不能用J8!)。 我有一个单词的哈希图:计数对。现在我需要按计数(递减顺序)排序,并打破按字母顺序使用word的联系。 我正在寻找对这个想法的反馈: 遍历HashMap中的映射项(me) 使用me.getkey=K和me.getvalue=v new map.entry reverse_me=(V,K){不确定此语法} 将r

  • 我有一个,其中的键是字符串。我需要获取这些键并根据键大小(即字符串长度)对其进行排序,然后将其存储在某个中。 注意:如果两个键的大小相同,那么我们可以把它放在任何顺序。 为(如)。

  • 问题内容: 我试图根据键中的日期对此HashMap进行排序 我的哈希图: 问题答案: 使用代替。正如已经实现的那样,它将在插入时自动排序。 或者,如果您有一个现有的并且想要基于它创建一个,则将其传递给构造函数: 也可以看看: Java教程-地图实现 Java教程-对象排序

  • 我目前有一个应用程序,可以显示1.5公里半径内附近的医院,它看起来是这样的: 我遇到的麻烦是,我不知道如何根据他们从最低到最高的计算距离来排序卡片。 我创建了一个来存储计算的距离列表,并用对其进行排序。 我如何确保小部件将遵循排序的距离值的顺序?

  • 问题内容: 我想对以下数据框进行排序: 我想对它进行排序,以便根据列表对LSE列进行重新排序: 当然,其他列也需要相应地重新排序。有没有办法在熊猫里做到这一点? 问题答案: pandas0.15版中对s的改进支持使您可以轻松做到这一点: 如果这只是临时排序,则可能不希望将LSE列保留为a ,但是如果您希望这种排序能够在不同的上下文中使用几次,则是一个很好的解决方案。 在更高版本的,中,已被替换为,

  • 我有一个,有200多列。问题在于订单生成时 我需要对这些列进行如下排序: 我有办法在Python中做到这一点吗?