0

次のメソッドを作成しました。

public List<String> listAll() {
    List worldCountriesByLocal = new ArrayList();

    for (Locale locale : Locale.getAvailableLocales()) {
        final String isoCountry = locale.getDisplayCountry();
        if (isoCountry.length() > 0) {
            worldCountriesByLocal.add(isoCountry);
            Collections.sort(worldCountriesByLocal);
        }
    }
    return worldCountriesByLocal;
}

非常にシンプルで、ユーザーロケールの世界の国のリストを返します。次に、アルファベット順に並べ替えます。これはすべて完全に機能します(国の重複が時々発生するようです!)。

とにかく、私が必要としているのは、米国と英国をリストの一番上に置くことです。私が抱えている問題は、米国と英国で返されるインデックスまたは文字列を分離できないことです。これは、ロケールに固有であるためです。

どんなアイデアでも本当にありがたいです。

4

4 に答える 4

8

とにかく、私が必要としているのは、米国と英国をリストの一番上に置くことです。私が抱えている問題は、米国と英国で返されるインデックスまたは文字列を分離できないことです。これは、ロケールに固有であるためです。

Comparator<Locale>次の手順で2つのロケールを比較するには、独自のロケールを実装する必要があるようです。

  • ロケールが同じ場合は、0を返します
  • 1つのロケールが米国の場合、その「勝利」を実現します
  • 1つのロケールが英国の場合、その「勝利」を実現します
  • それ以外の場合は、を使用しますo1.getDisplayCountry().compareTo(o2.getDisplayCountry())(つまり、既存の動作に委任します)

(これにより、米国が英国よりも優先されます。)

次にCollections.sort、カスタムコンパレータのインスタンスを使用して呼び出します。

国名を抽出する前にこれらすべてを実行してから、ソートされたリストから国名を抽出します。

于 2012-10-05T14:02:45.597 に答える
1

TreeSetまた、を使用して重複を排除し、独自のを使用ComparatorしてUSとGBを開始することもできます。

国ごとに複数のロケールが存在することが多いため、重複が発生します(これにより削除されます)。米国(スペイン語)と米国(英語)があり、たとえば3つのスイス(フランス語、ドイツ語、イタリア語)があります。

public class AllLocales {
  // Which Locales get priority.
  private static final Locale[] priorityLocales = {
    Locale.US,
    Locale.UK
  };

  private static class MyLocale implements Comparable<MyLocale> {
    // My Locale.
    private final Locale me;

    public MyLocale(Locale me) {
      this.me = me;
    }

    // Convenience
    public String getCountry() {
      return me.getCountry();
    }

    @Override
    public int compareTo(MyLocale it) {
      // No duplicates in the country field.
      if (getCountry().equals(it.getCountry())) {
        return 0;
      }
      // Check for priority ones.
      for (int i = 0; i < priorityLocales.length; i++) {
        Locale priority = priorityLocales[i];
        // I am a priority one.
        if (getCountry().equals(priority.getCountry())) {
          // I come first.
          return -1;
        }
        // It is a priority one.
        if (it.getCountry().equals(priority.getCountry())) {
          // It comes first.
          return 1;
        }
      }
      // Default to straight comparison.
      return getCountry().compareTo(it.getCountry());
    }
  }

  public static List<String> listAll() {
    Set<MyLocale> byLocale = new TreeSet();
    // Gather them all up.
    for (Locale locale : Locale.getAvailableLocales()) {
      final String isoCountry = locale.getDisplayCountry();
      if (isoCountry.length() > 0) {
        //System.out.println(locale.getCountry() + ":" + isoCountry + ":" + locale.getDisplayName());
        byLocale.add(new MyLocale(locale));
      }
    }
    // Roll them out of the set.
    ArrayList<String> list = new ArrayList<>();
    for (MyLocale l : byLocale) {
      list.add(l.getCountry());
    }
    return list;
  }

  public static void main(String[] args) throws InterruptedException {
    // Some demo usages.
    List<String> locales = listAll();
    System.out.println(locales);
  }
}
于 2012-10-05T14:54:18.497 に答える
1

はい、ソートするときは、独自のコンパレータを提供するだけです

Collections.sort(worldCountriesByLocal、new Comparator(){

    @Override
    public int compare(String o1, String o2) {
        if (o1.equals(TOP_VALUE))
            return -1;
        if (o2.equals(TOP_VALUE))
            return 1;
        return o1.compareTo(o2);
    }
})

ここで、最上位の値は、常に最上位にしたい値になります

于 2012-10-05T14:05:10.463 に答える
1

優先順位を割り当てる整数(たとえば、米国の場合は0、英国の場合は1、その他の場合は2)、区切り文字、国名で構成されるソートトークンを使用して、独自のPOJOを記述します。次に、その並べ替えIDと値としてPOJOをキーとするHashMapに配列を配置します。次に、マップからキーを並べ替え、並べ替えを繰り返して、並べ替えられた各キーのプレーンな国名を取得します。

例えば

2.Sweden
2.France
2.Tanzania
0.US
1.UK

並べ替え

0.US
1.UK
2.France
2.Sweden
2.Tanzania

編集:POJOは、国名以外のフィールドが多い場合にのみ必要です。国名だけの場合は、ソートIDをハッシュキー、国名をvalに設定し、POJOの部分をスキップします。

于 2012-10-05T14:05:47.960 に答える