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?

DVarga
  • 21,311
  • 6
  • 55
  • 60

4 Answers4

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:

  • 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_. – Alexander Sep 21 '16 at 10:02
  • you're right, i edited my code following your comment –  Sep 21 '16 at 10:10
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;
Nikhil
  • 3,711
  • 8
  • 32
  • 43
  • i don't think that has any performance improvement over a simple 'if', both are compiled to the same java byte code. – marmor Sep 21 '16 at 10:15
  • @marmor I read it [here](http://stackoverflow.com/questions/39613124/cannot-check-an-element-of-arraylistobject-if-it-has-null-value-in-android/39613354?noredirect=1#39613705) and [here](http://stackoverflow.com/questions/160218/to-ternary-or-not-to-ternary). I will be keen to learn if you have more references – Nikhil Sep 21 '16 at 10:22
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
DVarga
  • 21,311
  • 6
  • 55
  • 60
0
Using Java 8 Syntax
via this way you can check in list which fastest
arrList
.stream()
.filter(p-> p.getscore()==null)
Milap Pancholi
  • 134
  • 1
  • 12