3 回答

TA貢獻1786條經驗 獲得超13個贊
聽起來您想根據兩個標準進行排序:
每個列表項中的前三個字符,以及
每個列表項的其余內容,例如 Letter.pdf 首先出現。
一個老派的合理解決方案是實現 custom java.util.Comparator
,并將其用作排序的基礎:
public static List<String> sortOldSchool(List<String> list){
Comparator<String> comparator = new Comparator<String>(){
private static final String LETTER = "Letter.pdf";
public int compare(String orange, String apple){
String ostart = orange.substring(0,3);
String astart = apple.substring(0,3);
if (ostart.equals(astart)){
if (orange.endsWith(LETTER)){
return (apple.endsWith(LETTER)) ? 0 : -1;
}
else return (apple.endsWith(LETTER)) ? 1 : orange.compareTo(apple);
}
else return ostart.compareTo(astart);
}
};
Collections.sort(list, comparator);
return list;
}
一種更現代的方法是利用從 Java 8 開始可用的新功能范式:
public static List<String> sortFunctional(List<String> list){
Comparator<String> firstThree= Comparator.comparing(item -> item.substring(0,3));
Comparator<String> letter = Comparator.comparing(item -> (!item.endsWith("Letter.pdf")));
return list.stream()
.sorted(firstThree.thenComparing(letter))
.collect(Collectors.toList());
}

TA貢獻1911條經驗 獲得超7個贊
根據您的描述 - 我建議將問題分解為兩個子問題:
步驟 1:將值組織到它們的前綴組中
第 2 步:對每個前綴組中的值進行排序,確保值“_Letter.pdf”排在第一位。
我提供了一個代碼示例來演示以下每個步驟:
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
public class StackoverflowTest {
List<String> values = Arrays.asList(
"123_Letter.pdf",
"123_Others.pdf",
"123_More.pdf",
"222_Second.pdf",
"222_Letter.pdf",
"222_Third.pdf",
"222_Fourth.pdf",
"123_File.pdf");
List<String> expected = Arrays.asList(
"123_Letter.pdf",
"123_Others.pdf",
"123_More.pdf",
"123_File.pdf",
"222_Letter.pdf",
"222_Second.pdf",
"222_Third.pdf",
"222_Fourth.pdf"
);
@Test
public void sort() {
// Basic function to group the values by the prefix
Collector<String, ?, Map<String, List<String>>> groupByPrefix = Collectors.groupingBy(c -> c.substring(0, 3));
// Basic function to sort the groups with _Letter.pdf first
Function<List<String>, Stream<? extends String>> sortPrefixGroups = v ->
v.stream().sorted(Comparator.comparing(i -> !i.endsWith("_Letter.pdf")));
// Step 1: Organise the values into their prefix groups
Map<String, List<String>> groupedByPrefix = new TreeMap<>(values.stream().collect(groupByPrefix));
// Step 2: Sort each of the prefix groups and recombine them back into a list to maintain their internal order
List<String> collect = groupedByPrefix.entrySet().stream().map(Map.Entry::getValue).flatMap(sortPrefixGroups).collect(toList());
Assert.assertEquals(expected, collect);
//Print just for fun
collect.forEach(System.out::println);
}
}

TA貢獻1803條經驗 獲得超3個贊
如何使用HashMap:
HashMap<String, List<String>> hashMap = new HashMap<String, List<String>>();
現在,遍歷哈希圖并添加元素:
if (!hashMap.containsKey(listItem.substring(0,2)) {
List<String> items = new ArrayList<String>();
items.add(listItem);
hashMap.put(listItem.substring(0,2), items);
} else {
hashMap.get(listItem.substring(0,2)).add(items);
}
添加回答
舉報