Kazh
Kazh

Reputation: 35

How to replace many if's statements in java?

I am trying to make my code "cleaner". I have many functions with a lot of if-statements. How can I replace them?

I'm using 4 differents Hashtables that's why I have those ifs. The only thing changing is the hashtable name and the jList where I add the informations.

if(n.getCategorie().getNum() == Categorie.INTERNATIONAL.getNum())
                {
                    DefaultListModel dlm = (DefaultListModel) jListInternationales.getModel();
                    dlm.addElement(n.getTitre());
                    jListInternationales.setModel(dlm);
                    NewsInter.put(""+nNews, n);
                }
                else if(n.getCategorie().getNum() == Categorie.POLITIQUE.getNum())
                {
                    DefaultListModel dlm = (DefaultListModel) jListViePolitique.getModel();
                    dlm.addElement(n.getTitre());
                    jListViePolitique.setModel(dlm);
            NewsPolitique.put(""+nNews, n);
                }
                else if(n.getCategorie().getNum() == Categorie.RAGOT.getNum())
                {
                    DefaultListModel dlm = (DefaultListModel) jListRagotsEtPotins.getModel();
                    dlm.addElement(n.getTitre());
                    jListRagotsEtPotins.setModel(dlm);
            NewsRagot.put(""+nNews, n);
                }
                else if(n.getCategorie().getNum() == Categorie.SPORT.getNum())
                {
                    DefaultListModel dlm = (DefaultListModel) jListInfosSports.getModel();
                    dlm.addElement(n.getTitre());
                    jListInfosSports.setModel(dlm);
            NewsSport.put(""+nNews, n);
                }

Upvotes: 2

Views: 249

Answers (5)

Sumitha13
Sumitha13

Reputation: 31

Use a Switch Case command which takes less memory while processing

Upvotes: 0

Karol Dowbecki
Karol Dowbecki

Reputation: 44952

The only difference between the if branches comes from the JList reference. Since the code is repeated you should extract it into a separate method:

private void updateList(JList list) {
  DefaultListModel dlm = (DefaultListModel) list.getModel();
  dlm.addElement(n.getTitre());
  list.setModel(dlm);
}

After that the code becomes much cleaner:

if (n.getCategorie().getNum() == Categorie.INTERNATIONAL.getNum()) {
  updateList(jListInternationales);
} else if (n.getCategorie().getNum() == Categorie.POLITIQUE.getNum()) {
  updateList(jListViePolitique);
} // etc

NewsInter.put(""+nNews, n);

Upvotes: 3

Zharios
Zharios

Reputation: 181

in your case, you can replace it by Switch statement.

This is the structure:

switch(value) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

In your specific problem , this would be:

switch(n.getCategorie()) {
  case International:
    // your code
    break;
  case Politique:
    // code block
    break;
...

  default:
    // code block
}

Upvotes: 0

Michiel Leegwater
Michiel Leegwater

Reputation: 1180

It seems that you're using enums. Enums may be compared by instance. Checking the numbers is not required.

Those can be used in a switch case. E.g.

switch (n.getCategorie()) {
case Categorie.INTERNATIONAL:
...

That removes some clutter, but not essentially the number of 'if' statements.

Upvotes: 3

EaglesMaster
EaglesMaster

Reputation: 41

You can replace the if, else if, else if, ... structure by changing it to the switch() structure, it looks like this:

switch(n.getCategorie().getNum()){
    case Categorie.INTERNATIONAL.getNum():
        DefaultListModel dlm = (DefaultListModel) jListInternationales.getModel();
        dlm.addElement(n.getTitre());
        jListInternationales.setModel(dlm);
        NewsInter.put(""+nNews, n);

        break;
    case Categorie.POLITIQUE.getNum():
        ...etc
}

Upvotes: 0

Related Questions