How to get all players in list with min values












1















I am trying to return the winners of my game with the lowest attempts. But I am unsure how to do this for cases where there is a tie.



my to get the winner is



try(Scanner scan = new Scanner(new File("result.txt"))){
while(scan.hasNext()){
String s = scan.nextLine().split(" ");
players.add(new Players(s[0],s[1],Integer.valueOf(s[2])));
}
Collections.sort(players, Comparator.comparing((players1) -> players1.getAttempts()));
//test// System.out.println(players);
//Collections.sort(players, (a,b)->a.getAttempts().compareTo(b.getAttempts()));


System.out.println("The winner is: "+ players.get(0).getName() +", with "+players.get(0).getAttempts()+ " attempt(s)!");
}
result.close();
}


any help is appreciated.










share|improve this question

























  • what need to do at tie case?

    – Deadpool
    Nov 17 '18 at 4:01











  • Display that there are is a tie for first place and then list the name of the player, with the attempts

    – comp
    Nov 17 '18 at 4:03











  • What are the attributes and their corresponding type in new Players(s[0],s[1],Integer.valueOf(s[2])? Also, based on values, you're doing no comparison currently.

    – nullpointer
    Nov 17 '18 at 5:44
















1















I am trying to return the winners of my game with the lowest attempts. But I am unsure how to do this for cases where there is a tie.



my to get the winner is



try(Scanner scan = new Scanner(new File("result.txt"))){
while(scan.hasNext()){
String s = scan.nextLine().split(" ");
players.add(new Players(s[0],s[1],Integer.valueOf(s[2])));
}
Collections.sort(players, Comparator.comparing((players1) -> players1.getAttempts()));
//test// System.out.println(players);
//Collections.sort(players, (a,b)->a.getAttempts().compareTo(b.getAttempts()));


System.out.println("The winner is: "+ players.get(0).getName() +", with "+players.get(0).getAttempts()+ " attempt(s)!");
}
result.close();
}


any help is appreciated.










share|improve this question

























  • what need to do at tie case?

    – Deadpool
    Nov 17 '18 at 4:01











  • Display that there are is a tie for first place and then list the name of the player, with the attempts

    – comp
    Nov 17 '18 at 4:03











  • What are the attributes and their corresponding type in new Players(s[0],s[1],Integer.valueOf(s[2])? Also, based on values, you're doing no comparison currently.

    – nullpointer
    Nov 17 '18 at 5:44














1












1








1


1






I am trying to return the winners of my game with the lowest attempts. But I am unsure how to do this for cases where there is a tie.



my to get the winner is



try(Scanner scan = new Scanner(new File("result.txt"))){
while(scan.hasNext()){
String s = scan.nextLine().split(" ");
players.add(new Players(s[0],s[1],Integer.valueOf(s[2])));
}
Collections.sort(players, Comparator.comparing((players1) -> players1.getAttempts()));
//test// System.out.println(players);
//Collections.sort(players, (a,b)->a.getAttempts().compareTo(b.getAttempts()));


System.out.println("The winner is: "+ players.get(0).getName() +", with "+players.get(0).getAttempts()+ " attempt(s)!");
}
result.close();
}


any help is appreciated.










share|improve this question
















I am trying to return the winners of my game with the lowest attempts. But I am unsure how to do this for cases where there is a tie.



my to get the winner is



try(Scanner scan = new Scanner(new File("result.txt"))){
while(scan.hasNext()){
String s = scan.nextLine().split(" ");
players.add(new Players(s[0],s[1],Integer.valueOf(s[2])));
}
Collections.sort(players, Comparator.comparing((players1) -> players1.getAttempts()));
//test// System.out.println(players);
//Collections.sort(players, (a,b)->a.getAttempts().compareTo(b.getAttempts()));


System.out.println("The winner is: "+ players.get(0).getName() +", with "+players.get(0).getAttempts()+ " attempt(s)!");
}
result.close();
}


any help is appreciated.







java java-8






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 17:02









Deadpool

5,6002528




5,6002528










asked Nov 17 '18 at 3:57









compcomp

216




216













  • what need to do at tie case?

    – Deadpool
    Nov 17 '18 at 4:01











  • Display that there are is a tie for first place and then list the name of the player, with the attempts

    – comp
    Nov 17 '18 at 4:03











  • What are the attributes and their corresponding type in new Players(s[0],s[1],Integer.valueOf(s[2])? Also, based on values, you're doing no comparison currently.

    – nullpointer
    Nov 17 '18 at 5:44



















  • what need to do at tie case?

    – Deadpool
    Nov 17 '18 at 4:01











  • Display that there are is a tie for first place and then list the name of the player, with the attempts

    – comp
    Nov 17 '18 at 4:03











  • What are the attributes and their corresponding type in new Players(s[0],s[1],Integer.valueOf(s[2])? Also, based on values, you're doing no comparison currently.

    – nullpointer
    Nov 17 '18 at 5:44

















what need to do at tie case?

– Deadpool
Nov 17 '18 at 4:01





what need to do at tie case?

– Deadpool
Nov 17 '18 at 4:01













Display that there are is a tie for first place and then list the name of the player, with the attempts

– comp
Nov 17 '18 at 4:03





Display that there are is a tie for first place and then list the name of the player, with the attempts

– comp
Nov 17 '18 at 4:03













What are the attributes and their corresponding type in new Players(s[0],s[1],Integer.valueOf(s[2])? Also, based on values, you're doing no comparison currently.

– nullpointer
Nov 17 '18 at 5:44





What are the attributes and their corresponding type in new Players(s[0],s[1],Integer.valueOf(s[2])? Also, based on values, you're doing no comparison currently.

– nullpointer
Nov 17 '18 at 5:44












2 Answers
2






active

oldest

votes


















2














First sort the list in ascending order based on the attempts



List<Players> sortedList = players.stream().sorted((a,b)->a.getAttempts().compareTo(b.getAttempts())).collect(Collectors.toList())


Then print all winners having least attempts



sortedList.stream().filter(i->i.getAttempts()==sortedList.stream().findFirst()
.get().getAttempts()).forEach(winner->System.out.println("The winners are "+winner.getvalues));


or simply stream your collection object that sorted in ascending order



players.filter(i->i.getAttempts()==players.get(0).getAttempts()).forEach(winner->System.out.println("The winners are "+winner.getvalues));





share|improve this answer


























  • for filter it says cannot find symbol could you explain this?

    – comp
    Nov 17 '18 at 4:25











  • made some change check it @comp

    – Deadpool
    Nov 17 '18 at 4:35











  • this time .Collect is saying cannot find symbol

    – comp
    Nov 17 '18 at 4:41











  • sorry collect() lowercase letters

    – Deadpool
    Nov 17 '18 at 4:45











  • then so simple just change this line a.getAttempts().compareTo(b.getAttempts()) like this

    – Deadpool
    Nov 17 '18 at 11:32





















0














Here's another approach:



//read file into stream, try-with-resources
try (Stream<String> stream = Files.lines(Paths.get("result.txt"))) {
stream.map(line -> line.split("\s"))
.map(a -> new Players(a[0], a[1], Integer.parseInt(a[2])))
.collect(groupingBy(Players::getAttemps))
.entrySet().stream().min(Comparator.comparingInt(Map.Entry::getKey))
.map(Map.Entry::getValue)
.map(l -> l.size() > 1 ?
l.stream().map(Players::getName)
.collect(joining(", ","there is a tie -->",
"with "+ l.get(0).getAttemps()+"attempt(s)!")) :
"The winner is: " + l.get(0).getName() +
"with "+ l.get(0).getAttemps() +"attempt(s)!")
.ifPresent(System.out::println);
} catch (IOException e) { e.printStackTrace(); }


This handles, both the cases where there is only a single winner or when there is a tie and displays appropriate messages for each.



imports:



import static java.util.stream.Collectors.*;
import java.util.stream.*;
import java.util.List;
import java.util.*;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.io.*;





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348059%2fhow-to-get-all-players-in-list-with-min-values%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    First sort the list in ascending order based on the attempts



    List<Players> sortedList = players.stream().sorted((a,b)->a.getAttempts().compareTo(b.getAttempts())).collect(Collectors.toList())


    Then print all winners having least attempts



    sortedList.stream().filter(i->i.getAttempts()==sortedList.stream().findFirst()
    .get().getAttempts()).forEach(winner->System.out.println("The winners are "+winner.getvalues));


    or simply stream your collection object that sorted in ascending order



    players.filter(i->i.getAttempts()==players.get(0).getAttempts()).forEach(winner->System.out.println("The winners are "+winner.getvalues));





    share|improve this answer


























    • for filter it says cannot find symbol could you explain this?

      – comp
      Nov 17 '18 at 4:25











    • made some change check it @comp

      – Deadpool
      Nov 17 '18 at 4:35











    • this time .Collect is saying cannot find symbol

      – comp
      Nov 17 '18 at 4:41











    • sorry collect() lowercase letters

      – Deadpool
      Nov 17 '18 at 4:45











    • then so simple just change this line a.getAttempts().compareTo(b.getAttempts()) like this

      – Deadpool
      Nov 17 '18 at 11:32


















    2














    First sort the list in ascending order based on the attempts



    List<Players> sortedList = players.stream().sorted((a,b)->a.getAttempts().compareTo(b.getAttempts())).collect(Collectors.toList())


    Then print all winners having least attempts



    sortedList.stream().filter(i->i.getAttempts()==sortedList.stream().findFirst()
    .get().getAttempts()).forEach(winner->System.out.println("The winners are "+winner.getvalues));


    or simply stream your collection object that sorted in ascending order



    players.filter(i->i.getAttempts()==players.get(0).getAttempts()).forEach(winner->System.out.println("The winners are "+winner.getvalues));





    share|improve this answer


























    • for filter it says cannot find symbol could you explain this?

      – comp
      Nov 17 '18 at 4:25











    • made some change check it @comp

      – Deadpool
      Nov 17 '18 at 4:35











    • this time .Collect is saying cannot find symbol

      – comp
      Nov 17 '18 at 4:41











    • sorry collect() lowercase letters

      – Deadpool
      Nov 17 '18 at 4:45











    • then so simple just change this line a.getAttempts().compareTo(b.getAttempts()) like this

      – Deadpool
      Nov 17 '18 at 11:32
















    2












    2








    2







    First sort the list in ascending order based on the attempts



    List<Players> sortedList = players.stream().sorted((a,b)->a.getAttempts().compareTo(b.getAttempts())).collect(Collectors.toList())


    Then print all winners having least attempts



    sortedList.stream().filter(i->i.getAttempts()==sortedList.stream().findFirst()
    .get().getAttempts()).forEach(winner->System.out.println("The winners are "+winner.getvalues));


    or simply stream your collection object that sorted in ascending order



    players.filter(i->i.getAttempts()==players.get(0).getAttempts()).forEach(winner->System.out.println("The winners are "+winner.getvalues));





    share|improve this answer















    First sort the list in ascending order based on the attempts



    List<Players> sortedList = players.stream().sorted((a,b)->a.getAttempts().compareTo(b.getAttempts())).collect(Collectors.toList())


    Then print all winners having least attempts



    sortedList.stream().filter(i->i.getAttempts()==sortedList.stream().findFirst()
    .get().getAttempts()).forEach(winner->System.out.println("The winners are "+winner.getvalues));


    or simply stream your collection object that sorted in ascending order



    players.filter(i->i.getAttempts()==players.get(0).getAttempts()).forEach(winner->System.out.println("The winners are "+winner.getvalues));






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 18 '18 at 17:01

























    answered Nov 17 '18 at 4:11









    DeadpoolDeadpool

    5,6002528




    5,6002528













    • for filter it says cannot find symbol could you explain this?

      – comp
      Nov 17 '18 at 4:25











    • made some change check it @comp

      – Deadpool
      Nov 17 '18 at 4:35











    • this time .Collect is saying cannot find symbol

      – comp
      Nov 17 '18 at 4:41











    • sorry collect() lowercase letters

      – Deadpool
      Nov 17 '18 at 4:45











    • then so simple just change this line a.getAttempts().compareTo(b.getAttempts()) like this

      – Deadpool
      Nov 17 '18 at 11:32





















    • for filter it says cannot find symbol could you explain this?

      – comp
      Nov 17 '18 at 4:25











    • made some change check it @comp

      – Deadpool
      Nov 17 '18 at 4:35











    • this time .Collect is saying cannot find symbol

      – comp
      Nov 17 '18 at 4:41











    • sorry collect() lowercase letters

      – Deadpool
      Nov 17 '18 at 4:45











    • then so simple just change this line a.getAttempts().compareTo(b.getAttempts()) like this

      – Deadpool
      Nov 17 '18 at 11:32



















    for filter it says cannot find symbol could you explain this?

    – comp
    Nov 17 '18 at 4:25





    for filter it says cannot find symbol could you explain this?

    – comp
    Nov 17 '18 at 4:25













    made some change check it @comp

    – Deadpool
    Nov 17 '18 at 4:35





    made some change check it @comp

    – Deadpool
    Nov 17 '18 at 4:35













    this time .Collect is saying cannot find symbol

    – comp
    Nov 17 '18 at 4:41





    this time .Collect is saying cannot find symbol

    – comp
    Nov 17 '18 at 4:41













    sorry collect() lowercase letters

    – Deadpool
    Nov 17 '18 at 4:45





    sorry collect() lowercase letters

    – Deadpool
    Nov 17 '18 at 4:45













    then so simple just change this line a.getAttempts().compareTo(b.getAttempts()) like this

    – Deadpool
    Nov 17 '18 at 11:32







    then so simple just change this line a.getAttempts().compareTo(b.getAttempts()) like this

    – Deadpool
    Nov 17 '18 at 11:32















    0














    Here's another approach:



    //read file into stream, try-with-resources
    try (Stream<String> stream = Files.lines(Paths.get("result.txt"))) {
    stream.map(line -> line.split("\s"))
    .map(a -> new Players(a[0], a[1], Integer.parseInt(a[2])))
    .collect(groupingBy(Players::getAttemps))
    .entrySet().stream().min(Comparator.comparingInt(Map.Entry::getKey))
    .map(Map.Entry::getValue)
    .map(l -> l.size() > 1 ?
    l.stream().map(Players::getName)
    .collect(joining(", ","there is a tie -->",
    "with "+ l.get(0).getAttemps()+"attempt(s)!")) :
    "The winner is: " + l.get(0).getName() +
    "with "+ l.get(0).getAttemps() +"attempt(s)!")
    .ifPresent(System.out::println);
    } catch (IOException e) { e.printStackTrace(); }


    This handles, both the cases where there is only a single winner or when there is a tie and displays appropriate messages for each.



    imports:



    import static java.util.stream.Collectors.*;
    import java.util.stream.*;
    import java.util.List;
    import java.util.*;
    import java.nio.file.Paths;
    import java.nio.file.Files;
    import java.io.*;





    share|improve this answer




























      0














      Here's another approach:



      //read file into stream, try-with-resources
      try (Stream<String> stream = Files.lines(Paths.get("result.txt"))) {
      stream.map(line -> line.split("\s"))
      .map(a -> new Players(a[0], a[1], Integer.parseInt(a[2])))
      .collect(groupingBy(Players::getAttemps))
      .entrySet().stream().min(Comparator.comparingInt(Map.Entry::getKey))
      .map(Map.Entry::getValue)
      .map(l -> l.size() > 1 ?
      l.stream().map(Players::getName)
      .collect(joining(", ","there is a tie -->",
      "with "+ l.get(0).getAttemps()+"attempt(s)!")) :
      "The winner is: " + l.get(0).getName() +
      "with "+ l.get(0).getAttemps() +"attempt(s)!")
      .ifPresent(System.out::println);
      } catch (IOException e) { e.printStackTrace(); }


      This handles, both the cases where there is only a single winner or when there is a tie and displays appropriate messages for each.



      imports:



      import static java.util.stream.Collectors.*;
      import java.util.stream.*;
      import java.util.List;
      import java.util.*;
      import java.nio.file.Paths;
      import java.nio.file.Files;
      import java.io.*;





      share|improve this answer


























        0












        0








        0







        Here's another approach:



        //read file into stream, try-with-resources
        try (Stream<String> stream = Files.lines(Paths.get("result.txt"))) {
        stream.map(line -> line.split("\s"))
        .map(a -> new Players(a[0], a[1], Integer.parseInt(a[2])))
        .collect(groupingBy(Players::getAttemps))
        .entrySet().stream().min(Comparator.comparingInt(Map.Entry::getKey))
        .map(Map.Entry::getValue)
        .map(l -> l.size() > 1 ?
        l.stream().map(Players::getName)
        .collect(joining(", ","there is a tie -->",
        "with "+ l.get(0).getAttemps()+"attempt(s)!")) :
        "The winner is: " + l.get(0).getName() +
        "with "+ l.get(0).getAttemps() +"attempt(s)!")
        .ifPresent(System.out::println);
        } catch (IOException e) { e.printStackTrace(); }


        This handles, both the cases where there is only a single winner or when there is a tie and displays appropriate messages for each.



        imports:



        import static java.util.stream.Collectors.*;
        import java.util.stream.*;
        import java.util.List;
        import java.util.*;
        import java.nio.file.Paths;
        import java.nio.file.Files;
        import java.io.*;





        share|improve this answer













        Here's another approach:



        //read file into stream, try-with-resources
        try (Stream<String> stream = Files.lines(Paths.get("result.txt"))) {
        stream.map(line -> line.split("\s"))
        .map(a -> new Players(a[0], a[1], Integer.parseInt(a[2])))
        .collect(groupingBy(Players::getAttemps))
        .entrySet().stream().min(Comparator.comparingInt(Map.Entry::getKey))
        .map(Map.Entry::getValue)
        .map(l -> l.size() > 1 ?
        l.stream().map(Players::getName)
        .collect(joining(", ","there is a tie -->",
        "with "+ l.get(0).getAttemps()+"attempt(s)!")) :
        "The winner is: " + l.get(0).getName() +
        "with "+ l.get(0).getAttemps() +"attempt(s)!")
        .ifPresent(System.out::println);
        } catch (IOException e) { e.printStackTrace(); }


        This handles, both the cases where there is only a single winner or when there is a tie and displays appropriate messages for each.



        imports:



        import static java.util.stream.Collectors.*;
        import java.util.stream.*;
        import java.util.List;
        import java.util.*;
        import java.nio.file.Paths;
        import java.nio.file.Files;
        import java.io.*;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 17 '18 at 12:14









        AomineAomine

        42k74172




        42k74172






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348059%2fhow-to-get-all-players-in-list-with-min-values%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini