0

文字列シーケンスがあり、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;

             }
         }; 
4

4 に答える 4

1

次のように 2 つのループで実行する方がはるかに簡単です。

final String sequence = "People,Object,Environment,Message,Service";
final HashMap<String, String> lhm = new HashMap<String, String>();
final 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
final Set<Entry<String, String>> set = lhm.entrySet();
final String[] resultSequence = sequence.split(",");

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

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

final Iterator<Entry<String, String>> iter = set.iterator();
while (iter.hasNext())
{
    final Map.Entry me = iter.next();
    final String res = (String) me.getKey();
    boolean found = false;
    for (int j = 0; j < resultSequence.length; j++)
    {
        if (res.contains(resultSequence[j]))
        {
            found = true;
            break;
        }
    }
    if (!found)
    {
        list.add(res);
    }
}

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

final Iterator<String> iterlist2 = list.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 Rand
于 2013-09-10T13:20:15.180 に答える
0

java.util.HashMapは順不同です。このクラスは、マップの順序を保証しません。特に、順序が時間の経過とともに一定に保たれることを保証するものではありません。

あなたの場合、java.util.LinkedHashMapが最良の選択肢になります。ソートすると、順序が維持され、ソートされた方法ですべての要素が返されます。

于 2013-09-10T13:22:30.173 に答える