Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6533883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:11:02+00:00 2026-05-25T10:11:02+00:00

I have a class containing two properties: public class player{ public player(String playerName,int points){

  • 0

I have a class containing two properties:

public class player{
    public player(String playerName,int points){
        this.playerName=playerName;
        this.points=points;
    }
    public String getPlayerName() {
        return playerName;
    }
    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }
    public int getPoints() {
        return points;
    }
    public void setPoints(int points) {
        this.points = points;
    }
    private String playerName;
    private int points;
}

I have arrayList class contains collection of palyer objects.

List palyers=new ArrayList();
players.add(new player("mike",2));
players.add(new player("steve",3));
players.add(new player("jhon",7));
players.add(new player("harry",5);

Here my question is how to display player names with smallest points difference.

Output:

Based on the example code i written:

Mike and steve is the output

THis way comparison should happen:

mike to steve --> 1

mike to jhon--->5

mike to harry-->3

steve to mike -->1
steve to jhon--->5
steve to harry--->3

jhon to mike-->5
jhon to steve-->4
jhon to harry--->2

harry to mike -->3

harry to steve-->2

harry to jhon -->2

Based on above comparison mike and steve should display

Any java API to compare the properties?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:11:02+00:00Added an answer on May 25, 2026 at 10:11 am

    So you want to know the pair of players whose score has the smallest difference?
    I don’t think there’s an API function for that, although there might be something in the Apache Commons Collections.

    Otherwise you’ll have to use a nested loop.

    int res1 = -1, res2 = -1;
    
    int maxDiff = Integer.MAX_VALUE;
    for ( int i = 0; i < players.size( ); i++ )
    {
        for ( int j = i + 1; j < players.size() ; j++ )
        {
            int diff = Math.abs( players.get(i).getPoints( ) - players.get(j).getPoints( ) );
            if ( diff < maxDiff )
            {
                maxDiff = diff;
                res1 = i;
                res2 = j;
            }           
        }
    }
    System.out.println(players.get(res1).getPlayerName( ) + " and " + players.get(res2).getPlayerName( ));
    

    Obviously, this code needs some work; for example, if two pairs of players have the same difference between them, only the latest pair processed will be reported. You may also want to re-work this piece of code to remove the default values (Note how the System.out.println will crash if your List contains 0 players, for example). I leave these for you to solve. HTH.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an example class containing two data points: public enum Sort { First,
I have class: static class AnotherClass { int number; String isPrime; public int getNumber()
In Java I have two classes: Class A { public String ID; public Object
I have a class A containing two pointers to objects of another class B.
Ok I have two modules, each containing a class, the problem is their classes
So, I have a list containing a custom class, MyClass MyClass has properties, which
I have been trying to create an array of a class containing two values,
I have a class containing a number of double values. This is stored in
I have class containing an std::set of boost::weak_ptr<T> . I have two functions begin()
I have a class containing Linq To SQL objects that are used to populate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.