3

I have an ArrayList<Game> and my object game consists of Date,Opponent,Score and other fields counting 10 elements.

Sometimes Score maybe null.
How can I check this and change it to some default value?

I tried the following:

for(Game a : arrList)
{
  if(a.getScore() == null)
  {

  } 
} 

I need to do the if(..) 10 times or there is another faster way?

12
  • 1
    Can't you just initialize the score to your default value when you declare it in your Game class? Commented Sep 21, 2016 at 9:52
  • no way to improve your code unless you can initialize the score attribute with default value in the game class Commented Sep 21, 2016 at 9:53
  • Could you elaborate what you mean with "do the if(...) 10 times"? Maybe you could let Game.getScore return an Optional and use a.getScore().orElse(defaultValue) instead of a.getScore() everywhere. Or even introduce a Game.getScore(defaultValue) getter. Commented Sep 21, 2016 at 9:53
  • Why does iterating over 10 elements bother you? There is no reason to overengineer it. Commented Sep 21, 2016 at 9:53
  • 1
    @user1506104, no, Score is a class. Commented Sep 21, 2016 at 9:55

4 Answers 4

2

In your class Game, you can put a default value for score :

class Game{
     private Score score;
     public Score getScore(){     
         return this.score == null? this.score : new Score();      
     }
}

For your information : http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.5

Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10.2):

For type byte, the default value is zero, that is, the value of (byte)0.

For type short, the default value is zero, that is, the value of (short)0.

For type int, the default value is zero, that is, 0.

For type long, the default value is zero, that is, 0L.

For type float, the default value is positive zero, that is, 0.0f.

For type double, the default value is positive zero, that is, 0.0d.

For type char, the default value is the null character, that is, '\u0000'.

For type boolean, the default value is false.

For all reference types (§4.3), the default value is null.

In your class Game, you can put a default value for score :

EDIT:

class Game{
     private Score score;
     public Score getScore(){     
         return this.score;      
     }
     public void setScore(Score score){     
         this.score = score;      
     }
     public Score getScoreOrDefault(Score default){
         if(Objects.isNull(this.score)){
              setScore(default);
              return default;
         }
     }
}

And after you can call getScoreOrDefault in passing a new Score as a parameter:

Sign up to request clarification or add additional context in comments.

2 Comments

As an extension to this solution and because you might want to keep your pojo behaviour as clear as possible, add some getter methods like getScoreOrDefault(Score default):Score.
you're right, i edited my code following your comment
1

I think you are doing it in right way but still if you want more performance then you can use ternary operators inside loop as follows

(a.getScore()==null)? a.setScore("value"):do nothing;

2 Comments

i don't think that has any performance improvement over a simple 'if', both are compiled to the same java byte code.
@marmor I read it here and here. I will be keen to learn if you have more references
1

You could update the getScore() method in Game to return Optional<Score>:

public Optional<Score> getScore() {
    return Optional.ofNullable(score);
}

Then when you call it you can use ifPresent with a Consumer:

If a value is present, invoke the specified consumer with the value, otherwise do nothing.

game.getScore().ifPresent(score -> 
    System.out.println("This is only executed if the value is present!"));

Example

public class Game {

    private Score score;
    private String name;

    public Game(String name) { this.name = name;}

    public Game(String name, Integer scoreVal) {
        this.name = name;
        score = new Score(scoreVal);
    }

    public String getName() { return name; }

    public Optional<Score> getScore() {
        return Optional.ofNullable(score);
    }

    public static void main(String[] args) {
        List<Game> games = new ArrayList<Game>();
        games.add(new Game("Game 1"));
        games.add(new Game("Game 2", 10));

        for(Game game: games) {
            game.getScore().ifPresent(score -> 
                System.out.println("Score value in " + game.getName() + " is " + score.getValue()));
        } 
    }
}

class Score {
    private Integer value = 0;

    public Score(Integer val) { value = val; }

    public Integer getValue() { return value; }
}

Output

Score value in Game 2 is 10

Comments

0
Using Java 8 Syntax
via this way you can check in list which fastest
arrList
.stream()
.filter(p-> p.getscore()==null)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.