Results 1 to 12 of 12
  1. #1
    saziz94 is offline Member
    Join Date
    Jul 2009
    Posts
    9
    Rep Power
    0

    Default I need your help guys "HashMap"

    Hi,

    I'm working in course project to Manage League Games

    I'm trying to write a code to generate Matches between Team.

    so,
    I declare
    private List<Team> _data = new ArrayList<Team>(); // this list has all Team in the League
    private Map<Matches,MatchesResult> _matches = new HashMap<Matches,MatchesResult>(); // it has all Matches among Teams also the Matches result

    Class Matches has attributes Team team1, team2;
    Class MatchesResult has attributes Matches match; int team1Result, team2Result;


    So I write the following code to generate the Matches;

    public void generateMatches(){
    Team [] team = new Team[_data.size()];
    int k=0;
    Iterator t = _data.iterator();

    // The following loop used to copy all Team into team array
    while (t.hasNext()) {
    team[k]= (Team) t.next();
    k++;
    }

    // This code is to generate Matches among Teams
    for(int i=0;i<team.length;i++){
    for(int j =i+1;j<team.length;j++){
    Matches m1 = new Matches(team[i],team[j]);
    MatchesResult m2 = new MatchesResult(m1);
    _matches.put(m1,m2);
    }
    }

    }


    I run the program with the following code;

    Team t1 = new Soccer("Brazil");
    Team t2 = new Soccer("France");
    Team t3 = new Soccer("India");
    Team t4 = new Soccer("Italy");

    LeagueGames a = new LeagueGames();

    System.out.println(a.size());
    a.addTeam(t1);
    a.addTeam(t2);
    a.addTeam(t3);
    a.addTeam(t4);
    System.out.println(a.size());
    a.generateMatches();
    System.out.println(a.toString());

    System.out.println(a.get(new Matches(t3,t4)));
    // Here suppose I get the Match between India and Italy BUT I got NULL


    Here is the Output
    0
    4
    Leagua Matches:
    Brazil vs France : 0 - 0
    France vs Italy : 0 - 0
    France vs India : 0 - 0
    Brazil vs Italy : 0 - 0
    India vs Italy : 0 - 0
    Brazil vs India : 0 - 0

    null
    I have no idea why I got NULL.

    Could you help, please?

    Thank's

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    There is a lot of stuff missing from what you posted: it's hard to say what a.get() is doing without knowing what a LeagueGames is.

    I am guessing that get() obtains what it returns from a map. In that case you might get a null because "new Matches(t3,t4)" is not equal to any of the keys within the map. See get() in the Map API docs.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    To be more pointed about it, get() involves comparing keys using their equals() method: does your Matches class override equals()? Because it should.

  4. #4
    saziz94 is offline Member
    Join Date
    Jul 2009
    Posts
    9
    Rep Power
    0

    Default

    Yes, you are right

    Here's implementation of get() method in LeagueGames

    public MatchesResult get(Matches m) {

    if ( _matches.containsKey(m))
    return _matches.get(m);
    else
    return null;
    }


    But when I print out the map, "new Matches(t3,t4)" is already there in the map

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I have no doubt that there is some match between India and Italy as a key in the map. But how is the _matches map supposed to know that "new Matches(t3,t4)" match between India and Italy is equal() to the match it has as a key?

    And, again, does Matches override equals()?

    It's not just maps, but a lot of the collection classes use equals():

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    class MyInt {
    	private int val;
    	
    	MyInt(int val) {
    		this.val = val;
    	}
    	
    	@Override
    	public String toString() {
    		return String.valueOf(val);
    	}
    }
    
    public class EqualityCounts {
    	public static void main(String[] args) {
    		List<MyInt> list = new ArrayList<MyInt>();
    		MyInt devil = new MyInt(666);
    		
    		list.add(devil);
    		list.add(new MyInt(0));
    		list.add(new MyInt(1));
    		
    		System.out.print("The list is ");
    		for(MyInt i :list) {
    			System.out.print(i);
    			System.out.print(" ");
    		}
    		System.out.println();
    		System.out.println("list contains 666: " + list.contains(new MyInt(666)));
    	}
    }
    Last edited by pbrockway2; 03-05-2011 at 06:07 AM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Try replacing MyInt with Integer inside the main() method. The difference is that Integer overrides equals(), but MyInt does not.

  7. #7
    saziz94 is offline Member
    Join Date
    Jul 2009
    Posts
    9
    Rep Power
    0

    Default

    hah,

    PHP Code:
    	
    
    public boolean equal(Matches that){
    		if ( this._team1.equals(that._team1) && this._team2.equals(that._team2)){
    			return true;
    		}
    		else
    			return false;
    	}
    So, I have to override the equal() in all class
    Team
    Matches
    MatchesResult

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Yes, if Matches is going to function as a key in a map which depends only on its two team attributes then it will need an equals() that expresses that fact. (I don't know about the other classes; it depends on how they're used.)

    Have a read of Roedy Green's equals() page. Note the comment about hashCode() that he makes near the end.

  9. #9
    saziz94 is offline Member
    Join Date
    Jul 2009
    Posts
    9
    Rep Power
    0

    Default

    what about compareTo() still I have to override it too,

    I really appreciate your efforts,

    Thank you,

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Do any of your classes have a compareTo()?

    hashCode() and equals() are something every class has. But compareTo() is only for classes that implement the Comparable interface.

  11. #11
    saziz94 is offline Member
    Join Date
    Jul 2009
    Posts
    9
    Rep Power
    0

    Default

    No, I don't have classes implement the Comparable interface.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    No, I don't have classes implement the Comparable interface.

    You should be good then. Comparable is used for collections which are sorted (SortedSet etc) and with them care has to be taken to make equals() and compareTo() consistent.

Similar Threads

  1. hi guys please help me in "for loop "
    By funkygarzon in forum New To Java
    Replies: 28
    Last Post: 01-26-2011, 08:20 PM
  2. Replies: 3
    Last Post: 10-12-2010, 05:21 PM
  3. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •