如何对List< String>进行重新排序

编程入门 行业动态 更新时间:2024-10-22 08:16:45
本文介绍了如何对List< String>进行重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了以下方法:

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

它非常简单,它返回用户区域设置中的世界国家/地区列表.然后,我对其进行排序以使其按字母顺序排列.这一切都很好(除了我似乎偶尔会得到一些国家的重复!).

Its pretty simple and it returns a list of world countries in the users locale. I then sort it to get it alphabetic. This all works perfectly (except I seem to occasionally get duplicates of countries!).

无论如何,无论如何,我需要将美国和英国放在列表的顶部.我的问题是我无法隔离将返回给美国和英国的索引或字符串,因为这是特定于语言环境的!

Anyway, what I need is to place the US, and UK at the top of the list regardless. The problem I have is that I can't isolate the index or the string that will be returned for the US and UK because that is specific to the locale!

任何想法都将不胜感激.

Any ideas would be really appreciated.

推荐答案

您还可以使用TreeSet消除重复项,并使用自己的Comparator消除US和GB的开头.

You could also use a TreeSet to eliminate duplicates and your own Comparator to bring US and GB up to the start.

您会得到重复(这将消除重复),因为每个国家/地区通常有一个以上的语言环境.有一个美国(西班牙)和一个美国(英语),例如三个瑞士(法国,德国和意大利).

You are getting duplicates (which this will eliminate) because there are often more than one locale per country. There is a US(Spanish) as well as a US(English) and there are three Switzerlands (French, German and Italian) for example.

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

更多推荐

如何对List&lt; String&gt;进行重新排序

本文发布于:2023-11-07 06:17:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565760.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:amp   List   lt   gt   String

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!