Solved Get highest value on a file?

Discussion in 'Plugin Development' started by Plx0wn, Oct 8, 2016.

Thread Status:
Not open for further replies.
  1. Hi! In a file (not config.yml) i have:
    Code:
    points:
       player1: 1
       player2: 5
       player3: 0
       ...
    How can i get the highest value? Thank's for you help !
     
    Last edited: Oct 8, 2016
  2. Offline

    I Al Istannen

    @Plx0wn
    Loop through the points section. There are a few ways.
    1. Make a variable highest value. Set it to 0 or -1 or something.
    2. Loop through the values (deep = false) (getValues(false).entrySet())
    3. Check if the value is bigger than your variable.
    4. If yes, set your variable equal to the found value
    5. After the loop you will have the highest value
    This in pseudo code:
    Code:java
    1. int highestValue := 0
    2.  
    3. configurationSection := getConfigurationSection("points")
    4. Map<String,Object> values := configurationSection#getValues(false)
    5.  
    6. for Entry<String,Object> in values {
    7. if(entry#getValue instanceof Number) {
    8. Number number := entry#getValue
    9. int intValue := number#intValue
    10. if (intValue > highestValue) {
    11. highestValue := intValue
    12. }
    13. }
    14. }



    Or you could use streams. I don't really know how to explain that:
    Code:java
    1. OptionalInt max = configuration.getConfigurationSection("players")
    2. .getValues(false) // get the values of the configuration section. will be things like
    3. // player0=0, player1=1, player2=2, player3=3, player4=4, player5=5
    4. .values() // get the values of that. This will return a collection with only the integers (as they are the values)
    5. .stream() // make a Stream out of it
    6. .mapToInt(value -> ((Number) value).intValue()) // convert the Integer objects to int primitives
    7. .max(); // get the maximum int value.



    If you want to get the Player with the most points it is the same base, but you need an additonal variable.
     
  3. Thank you so much guy !! And if i want make top 10 for an exemple ?
     
    Last edited: Oct 8, 2016
  4. Offline

    I Al Istannen

    @Plx0wn
    If you use the stream approach, it is quite easy. Though you will need to work with a Stream<Integer>, as the IntStream doesn't allow reverse sorting (only smallest -> biggest).

    Code:java
    1. Collection<Integer> players = configuration.getConfigurationSection("players")
    2. .getValues(false) // get the values of the configuration section. will be things like
    3. // player0=0, player1=1, player2=2, player3=3, player4=4, player5=5
    4. .values() // get the values of that. This will return a collection with only the integers (as they are the values)
    5. .stream() // make a Stream out of it
    6. .map(value -> ((Integer) value)) // cast the Objects to Integer objects
    7. .sorted(Collections.reverseOrder()) // sort it in reverse order (biggest to smallest)
    8. .limit(5) // limit it to five entries
    9. .collect(Collectors.toList()); // get all int values remaining


    So, let's look at what has changed.
    • Code:
      .mapToInt(value -> ((Number) value).intValue())    // convert the Integer objects to int primitives
      is now
      Code:
      .map(value -> ((Integer) value))    // cast the Objects to Integer objects
      This is because we now need a Stream<Integer>, not an IntStream. The IntStream uses primitives (int), the Stream<Integer> uses the Wrapper object. The values are saved as "Object", so we need to cast them to Integers.
    • Code:
      .sorted(Collections.reverseOrder())    // sort it in reverse order (biggest to smallest)
      This line is new. It sorts the Stream, in reversed order. This means from biggest to smallest, as you want.
    • Code:
      .limit(5)    // limit it to five entries
      This is new too. It limits the length of the stream to five elements. This means you will now have 0 - 5 elements in the stream, but no more.
    • Code:
      .max();    // get the maximum int value.
      Was changed to
      Code:
      .collect(Collectors.toList());    // get all int values remaining
      The first just returned the maximum, while the second returns the WHOLE stream as a List.

    I understand I am just giving code samples which isn't a great way of doing this (spoonfeeding), as it discourages the other party from learning themselves. But I have no idea how you could rephrase the Stream operations besides what I did :/
     
  5. I undestand and thank you very much guy ! Now i can continue my plugin <3
     
  6. Offline

    I Al Istannen

    @Plx0wn
    I wish you good luck :)

    In the future please Ta'h'g users, so they get an alert. Write '@' and then the name or click the "Tahg User" button in the lower right of every post.
     
  7. Last edited: Oct 9, 2016
    I Al Istannen likes this.
Thread Status:
Not open for further replies.

Share This Page