栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何对List 重新排序

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何对List 重新排序

您还可以使用

TreeSet
来消除重复项,并使用自己来消除
Comparator
US和GB。

由于每个国家/地区通常有多个区域设置,因此您会得到重复(这将消除这种情况)。有一个美国(西班牙)和一个美国(英语),例如三个瑞士(法国,德国和意大利)。

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);  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/498062.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号