0

If i have arraylist i can use Collection.sort() what's very effective and fast. 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.

I've tried to use Collection.sort() whats the best method to do that but i can't use it like normal 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";
}
  • `Collections.sort` accepts `List` as a parameter. If your `BigList` class does not implement the `List` interface, then you cannot use `Collections.sort`. – Dimitar Spasovski Feb 17 '19 at 20:44
  • @Dvorog So what can i do? I biglist its abstract, what can i use instead of collection.sort? I want so sort thousands of elements.. –  Feb 17 '19 at 21:31
  • 1
    1.Consider using normal list (if you are only having few thousand elements). 2.Look for sorting implementations of the list that you are using. 3.Consider using a data structure that will keep the elements sorted (some kind of a BigSet if you really have a lot of elements). – Dimitar Spasovski Feb 17 '19 at 21:37
  • I will often check that list, like 30 sec... for 10-20k elements.. I was looking for implementantions but i didn't find any. –  Feb 17 '19 at 21:39
  • I can always use GapList if i will don't find any solution. –  Feb 17 '19 at 21:39
  • The purpose of a BigList is to hold over Integer.MAX_VALUE (2147 millions) elements. If you only have 10-20k there is no reason to use BigList. – ggf31416 Feb 17 '19 at 21:54

1 Answers1

3

I'm pretty sure you are suffering from the XY Problem. 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.

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. They take as arguments:

  • Starting (inclusive) and ending (exclusive) indexes to sort, i.e 0 and the size of the list
  • a LongComparator , which is an object that given two long indexes compares the elements at these indexes
  • a BigSwapper, which is an object that given two long indexes swaps the elements at these indexes.

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]
ggf31416
  • 3,582
  • 1
  • 25
  • 26
  • Oh thanks for nice example, but if you say that its only for hold more than max value integer so i don't need that, i will use gaplist with that: ranking.sort(Comparator.comparing(Ranking::getPoints).thenComparing(Ranking::getName)); Collections.reverse(ranking); –  Feb 18 '19 at 14:31
  • Thanks. The answer is actually aimed to people that need to sort a FastUtil's BigList in the future, as FastUtil's documentation is lacking in many aspects. There are very few examples and many methods in concrete classes lack javadoc documentation, so they inherit whatever javadoc was in place for their abstract superclasses which often is not applicable. – ggf31416 Feb 18 '19 at 18:21