简体   繁体   English

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

[英]How to sort FastUtil BigList with Collection.sort()

If i have arraylist i can use Collection.sort() what's very effective and fast. 如果我有arraylist,我可以使用Collection.sort(),那是非常有效和快速的。 But now i have to use BigList to hold much elements of my object type, and i have to sort them by value, From object called JasPlayer kills as int and nickname as String. 但是现在我必须使用BigList来保存我的对象类型的许多元素,并且必须按值对它们进行排序,而从名为JasPlayer的对象中,以int的形式杀死并以String的昵称将其杀死。

I've tried to use Collection.sort() whats the best method to do that but i can't use it like normal List. 我试图使用Collection.sort()什么是最好的方法,但我不能像普通List那样使用它。

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

I'm pretty sure you are suffering from the XY Problem . 我很确定您正在遭受XY问题 Fastutil's BigList is harder to use than normal lists and there is no reason to use it if the number of elements can't exceed Integer.MAX_VALUE. Fastutil的BigList比普通列表更难使用,并且如果元素数不能超过Integer.MAX_VALUE,则没有理由使用它。

If you really need it (say you really have 3000 millions of elements and need to store them in memory as a list) the way I found to sort a BigList is using the static sorting methods at the BigArrays class, mergesort and quicksort . 如果您确实需要它(例如您确实有3000百万个元素,并且需要将它们作为列表存储在内存中),那么我发现对BigList进行排序的方法是使用BigArrays类, mergesort和 quicksort的静态排序方法。 They take as arguments: 他们认为:

  • Starting (inclusive) and ending (exclusive) indexes to sort, ie 0 and the size of the list 开始(包含)索引和结束(包含)索引进行排序,即0和列表的大小
  • a LongComparator , which is an object that given two long indexes compares the elements at these indexes 一个LongComparator,它是一个给定两个长索引的对象,用于比较这些索引处的元素
  • a BigSwapper, which is an object that given two long indexes swaps the elements at these indexes. 一个BigSwapper,这是一个给定两个长索引的对象,它们会交换这些索引处的元素。

Example: 例:

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 
{
    public static void main( String[] args )
    {
        BigList<String> bigList = new ObjectBigArrayBigList<String>();
        bigList.add("Z");
        bigList.add("X");
        bigList.add("Y");
        bigList.add("A");
        bigList.add("C");
        bigList.add("B");

        System.out.println("Biglist before: " + bigList.toString());

        LongComparator cmp = (i,j) -> bigList.get(i).compareTo(bigList.get(j));
        BigSwapper swapper = (i,j) -> {
            String tmp = bigList.get(i);
            bigList.set(i, bigList.get(j));
            bigList.set(j, tmp);
        };

        BigArrays.mergeSort(0, bigList.size64(), cmp, swapper);

        System.out.println("Biglist after : " + bigList.toString());
     }
}

Output: 输出:

Biglist before: [Z, X, Y, A, C, B]
Biglist after : [A, B, C, X, Y, Z]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM