猿问

如何使用 Collection.sort() 对 FastUtil BigList 进行排序

如果我有 arraylist 我可以使用 Collection.sort() 这是非常有效和快速的。但是现在我必须使用 BigList 来保存我的对象类型的许多元素,并且我必须按值对它们进行排序,从名为 JasPlayer 的对象杀死为 int,昵称为 String。


我尝试使用 Collection.sort() 最好的方法来做到这一点,但我不能像普通列表那样使用它。


private BigList<JasPlayer> playerRankingPlaces;


public BigList<JasPlayer> getRanking() {

    return this.playerRankingPlaces;

}


public void addRankingElement(JasPlayer element) {

    this.playerRankingPlaces.add(element);

}


public void setRanking(BigList<JasPlayer> playerRanking) {

    this.playerRankingPlaces = playerRanking;

}


public void sortRankingList() {

    for(JasPlayer player : JasPlayerUtils.getPlayers()) {

        addRankingElement(player);

        long startTime = System.nanoTime();


           //THERE's Problem, i can't use biglist like normal list :\

        Collections.sort(playerRankingPlaces, Comparator.comparing(Ranking :: getKills).thenComparing(Ranking :: getName));



        long endTime = System.nanoTime() - startTime;

        MessageUtils.sendDebug("Sorting " + playerRankingPlaces.size() + "took " +  endTime / 1e9 + " seconds");

    }

}


private static int getKills(UUID uuid) {

    if(JasPlayerUtils.findByUUID(uuid).isPresent()) {

        return JasPlayerUtils.findByUUID(uuid).get().getKills();

    }

    return 0;

}


private static String getName(UUID uuid) {

    if(JasPlayerUtils.findByUUID(uuid).isPresent()) {

        return JasPlayerUtils.findByUUID(uuid).get().getPlayer().getName();

    }

    return "Brak";

}


哆啦的时光机
浏览 253回答 1
1回答

胡说叔叔

我很确定您正遭受XY 问题的困扰。Fastutil 的 BigList 比普通列表更难使用,如果元素数量不能超过 Integer.MAX_VALUE,则没有理由使用它。如果你真的需要它(假设你真的有 30 亿个元素并且需要将它们作为列表存储在内存中),我发现对 BigList 进行排序的方式是使用 BigArrays 类中的静态排序方法mergesort 和 quicksort。他们将其作为论据:要排序的开始(包括)和结束(不包括)索引,即 0 和列表的大小一个 LongComparator ,它是一个给定两个长索引的对象,比较这些索引处的元素BigSwapper,它是一个给定两个长索引的对象,交换这些索引处的元素。例子:import it.unimi.dsi.fastutil.BigArrays;import it.unimi.dsi.fastutil.BigList;import it.unimi.dsi.fastutil.BigSwapper;import it.unimi.dsi.fastutil.longs.LongComparator;import it.unimi.dsi.fastutil.objects.ObjectBigArrayBigList;public class App&nbsp;{&nbsp; &nbsp; public static void main( String[] args )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; BigList<String> bigList = new ObjectBigArrayBigList<String>();&nbsp; &nbsp; &nbsp; &nbsp; bigList.add("Z");&nbsp; &nbsp; &nbsp; &nbsp; bigList.add("X");&nbsp; &nbsp; &nbsp; &nbsp; bigList.add("Y");&nbsp; &nbsp; &nbsp; &nbsp; bigList.add("A");&nbsp; &nbsp; &nbsp; &nbsp; bigList.add("C");&nbsp; &nbsp; &nbsp; &nbsp; bigList.add("B");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Biglist before: " + bigList.toString());&nbsp; &nbsp; &nbsp; &nbsp; LongComparator cmp = (i,j) -> bigList.get(i).compareTo(bigList.get(j));&nbsp; &nbsp; &nbsp; &nbsp; BigSwapper swapper = (i,j) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String tmp = bigList.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bigList.set(i, bigList.get(j));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bigList.set(j, tmp);&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; BigArrays.mergeSort(0, bigList.size64(), cmp, swapper);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Biglist after : " + bigList.toString());&nbsp; &nbsp; &nbsp;}}输出:Biglist before: [Z, X, Y, A, C, B]Biglist after : [A, B, C, X, Y, Z]
随时随地看视频慕课网APP

相关分类

Java
我要回答