第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

即時訂購集

即時訂購集

楊__羊羊 2021-05-07 14:11:43
我有一個Java Set,可以向以下人員提供信息:Set<myData> dataLocations = getData(location);我想對這個Set進行排序,但是我嘗試了sortedSet卻無法使其正常工作,所以我嘗試了dataLocations = dataLocations.stream().sorted(Comparator.comparing(myData -> myData.expDtTm)).collect(Collectors.toSet());唯一的問題是,在Java文檔中,它不能保證保留任何訂單。所以我嘗試了這個:TreeSet<myData> sortedDataLocations = dataLocations.stream().sorted(Comparator.comparing(myData -> myData.expDtTm)).collect(Collectors.toCollection(TreeSet<myData>));不用說它沒有用,所以任何有其他想法的人都會非常感激。
查看完整描述

3 回答

?
ITMISS

TA貢獻1871條經(jīng)驗 獲得超8個贊

您可以使用TreeSet并提供比較器


TreeSet<myData> sorted = new TreeSet<>(Comparator.comparing(MyData::expDtTm));

sorted.addAll(dataLocations);

或按照CollectorJavadocs類中的描述為TreeSet以下內(nèi)容創(chuàng)建您自己的收集器:


Collector<Widget, ?, TreeSet<Widget>> intoSet =

     Collector.of(

         TreeSet::new, 

         TreeSet::add,

         (left, right) -> { left.addAll(right); return left; }

     );


查看完整回答
反對 回復(fù) 2021-05-26
?
ibeautiful

TA貢獻1993條經(jīng)驗 獲得超6個贊

您可以嘗試以下方法:


public class Example {

  public static void main(String[] args) {

    Comparator<String> stringComparator =

      Comparator.comparing((String x) -> x);


    Supplier<TreeSet<String>> supplier =

      () -> new TreeSet<>(stringComparator);


    Set<String> set = new HashSet<>(Arrays.asList("1", "3", "7", "2", "9", "4"));

    TreeSet<String> treeSet = set.stream()

      .collect(Collectors.toCollection(supplier));

    System.out.println(treeSet);

  }

}

將String類替換為您的String類。


輸出


[1, 2, 3, 4, 7, 9]


查看完整回答
反對 回復(fù) 2021-05-26
  • 3 回答
  • 0 關(guān)注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號