Sarah Smith
Sarah Smith

Reputation: 11

Why is my set not adding all the elements?

There are no duplicates in my textfile but when i print out the size of the set, it is significantly less than the actual number of elements in the text file....any ideas on how i can fix this

This is the main method

import java.util.*;
import java.util.Collection;
import java.io.*;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class Schedule
{
  public static void main (String args[])throws IOException
  {
    Scanner sc=new Scanner(new File("Footballteams.txt"));
    ArrayList<Football>y=new ArrayList<Football>();

    for(int i=0;i<114;i++)
    {
        String team=sc.nextLine();
        Integer points=0;
        String[]a=team.split("\\|",9);
        String name=a[0];
        int wins=Integer.parseInt(a[1]);
        points+=wins;
        int finalRecord14=Integer.parseInt(a[2]);
        int finalRecord13=Integer.parseInt(a[3]);
        int finalRecord12=Integer.parseInt(a[4]);
        int finalRecord11=Integer.parseInt(a[5]);
        int bowlVictories=Integer.parseInt(a[6]);
        points=points+(bowlVictories*10);
        int bowlLosses=Integer.parseInt(a[7]);
        points=points-(bowlLosses*5);
        int ConferenceChamp=Integer.parseInt(a[8]);
        points=points+(ConferenceChamp*10);
        Football x=new Football(name,wins,finalRecord14,finalRecord13,finalRecord12,finalRecord11,bowlVictories,bowlLosses,ConferenceChamp,points);
        teams.add(x);
        y.add(x);

    }
    System.out.println(teams.size());
    System.out.println(y.size());

This is the compareTo method in the football class

 public int compareTo(Football o)
 {

    return getPoints().compareTo(o.getPoints());
 }'

This is the file im reading from(i couldnt format it but its actually 1 team per line)

Georgia Southern(Sun Belt)|9|-1|-1|-1|-1|0|0|1
Louisiana-Lafayette(Sun Belt)|36|0|0|0|0|4|0|1
Appalachian State(Sun Belt)|7|0|0|0|0|0|0|0
Texas State(Sun Belt)|17|0|0|0|0|0|0|0
Arkansas State(Sun Belt)|35|0|0|0|0|2|2|3
South Alabama(Sun Belt)|14|0|0|0|0|0|1|0
Louisiana-Monroe(Sun Belt)|22|0|0|0|0|0|1|0
Troy(Sun Belt)|17|0|0|0|0|0|0|0
New Mexico State(Sun Belt)|9|0|0|0|0|0|0|0
Idaho(Sun Belt)|5|0|0|0|0|0|0|0
Georgia State(Sun Belt)|5|0|0|0|0|0|0|0
Memphis(AAC)|19|0|0|0|25|1|0|1
Cincinnati(AAC)|38|25|0|0|0|2|2|3
Central Florida(AAC)|36|0|0|10|0|2|1|2
East Carolina(AAC)|31|0|0|0|0|1|2|0
Houston(AAC)|34|18|0|0|0|2|1|0
Temple(AAC)|21|0|0|0|0|1|0|0
South Florida(AAC)|14|0|0|0|0|0|0|0
Tulane(AAC)|14|0|0|0|0|0|1|0
Tulsa(AAC)|24|0|0|0|0|1|1|1
SMU(AAC)|21|0|0|0|0|2|0|0
Connecticut(AAC)|15|0|0|0|0|0|0|0
Florida State(ACC)|48|23|10|1|5|3|1|3
Clemson(ACC)|42|22|11|8|15|3|1|1
Louisville(ACC)|39|0|13|15|24|2|2|1
Boston College(ACC)|20|0|0|0|0|0|2|0
NC State(ACC)|26|0|0|0|0|2|1|0
Syracuse(ACC)|23|0|0|0|0|2|0|1
Wake Forest(ACC)|18|0|0|0|0|0|1|0
Georgia Tech(ACC)|33|0|0|0|8|2|2|0
Duke(ACC)|28|0|0|23|0|0|3|0
North Carolina(ACC)|28|0|0|0|0|1|2|0
Pittsburgh(ACC)|25|0|0|0|0|1|3|0
Miami(ACC)|28|0|0|0|0|0|2|0
Virginia Tech(ACC)|33|21|0|0|0|2|2|0
Virginia(ACC)|19|0|0|0|0|0|1|0
Baylor(XII)|40|13|0|13|7|2|2|2
TCU(XII)|44|14|0|0|3|2|1|2
Kansas State(XII)|38|15|12|0|18|0|3|1
Oklahoma(XII)|39|16|15|6|0|2|2|1
Texas(XII)|31|0|19|0|0|2|2|0
West Virginia(XII)|28|17|0|0|0|1|2|1
Oklahoma State(XII)|37|3|0|17|0|3|1|1
Texas Tech(XII)|25|0|0|0|0|2|0|0
Kansas(XII)|9|0|0|0|0|0|0|0
Iowa State(XII)|17|0|0|0|0|0|2|0
Ohio State(B1G)|44|0|3|12|1|2|2|1
Michigan State(B1G)|42|11|0|3|5|4|0|1
Maryland(B1G)|20|0|0|0|0|0|2|0
Rutgers(B1G)|32|0|0|0|0|2|2|0
Michigan(B1G)|31|12|24|0|0|1|2|0
Penn State(B1G)|31|0|0|0|0|1|1|0
Indiana(B1G)|14|0|0|0|0|0|0|0
Wisconsin(B1G)|39|10|0|22|13|1|3|2
Minnesota(B1G)|25|0|0|0|0|0|3|0
Nebraska(B1G)|37|24|25|0|0|1|3|0
Iowa(B1G)|26|0|0|0|0|0|3|0
Illinois(B1G)|19|0|0|0|0|1|1|0
Northwestern(B1G)|26|0|17|0|0|1|1|0
Purdue(B1G)|17|0|0|0|0|1|1|0
Army Black Knights(IND)|12|0|0|0|0|0|0|0
BYU Cougars(IND|34|0|0|0|0|2|2|1
Notre Dame Fighting Irish(IND)|39|0|4|20|0|1|2|3
Navy Midshipmen(IND)|30|0|0|0|0|2|1|0
Marshall(C-USA)|35|0|0|0|3|3|1|1
Middle Tennessee(C-USA)|26|0|0|0|0|0|2|0
Western Kentucky(C-USA)|30|0|0|0|0|1|1|0
Old Dominion(C-USA)|6|0|0|0|0|0|0|0
FIU(C-USA)|16|0|0|0|0|0|1|0
Florida Atlantic(C-USA|13|0|0|0|0|0|0|0
Louisiana Tech(C-USA)|30|0|0|0|0|0|0|0
Rice(C-USA)|28|0|0|0|0|2|1|1
UTEP(C-USA)|17|0|0|0|0|0|1|0
UTSA(C-USA)|19|0|0|0|0|0|0|0
UNT(C-USA)|22|0|0|0|0|1|0|0
Southern Miss(C-USA)|16|6|0|0|0|1|0|1
Fresno State(MWC)|30|0|0|0|0|0|3|1
Hawaii(MWC)|14|0|0|0|0|0|0|0
Nevada(MWC)|25|0|0|0|0|0|3|0
San Diego(MWC)|32|0|0|0|0|1|3|0
San Jose State(MWC)|25|0|21|0|0|1|0|0
UNLV(MWC)|13|0|0|0|0|0|1|0
Air Force(MWC)|25|0|0|0|0|1|2|1
Boise State(MWC)|43|6|14|0|16|3|1|1
Colorado State(MWC)|25|0|0|0|0|1|1|0
New Mexico(MWC)|12|0|0|0|0|0|0|0
Utah State(MWC)|37|0|17|0|21|3|1|1
Wyoming(MWC)|21|0|0|0|0|0|1|0
Alabama(SEC)|48|1|1|8|4|2|2|2
Arkansa(SEC)|10|5|0|0|0|2|0|0
Auburn(SEC)|31|0|0|2|23|1|2|1
LSU(SEC)|41|1|9|14|22|2|2|1
Ole Miss(SEC)|26|0|0|0|17|2|1|0
Mississippi State(SEC)|32|0|0|0|11|2|2|0
Texas A&M(SEC)|35|0|5|18|0|4|0|0
Arizona(Pac South)|30|0|0|19|2|2|1|0
UCLA(Pac South)|35|0|0|16|10|2|2|0
Arizona State(Pac South)|34|0|0|21|12|2|2|0
USC(Pac South)|36|6|0|19|20|2|1|0
Utah(Pac South)|27|0|16|0|21|2|0|0
Colorado(Pac South)|10|0|0|0|0|0|0|0
Oregon(PAC North)|48|2|9|2|4|1|1|2
Stanford(PAC North)|42|8|0|10|6|7|1|0
Washington(PAC North)|31|8|0|0|0|0|0|1
California(PAC North)|16|0|0|0|0|0|0|0
Washington State(PAC North)|16|0|0|0|0|0|0|0
Oregon State(PAC North)|24|0|0|19|0|0|0|0
Florida(SEC)|29|0|9|0|0|3|1|0
Georgia(SEC)|40|18|4|24|9|2|2|0
Kentucky(SEC)|14|0|0|0|0|0|0|0
Missouri Tigers(SEC)|35|0|0|5|14|2|0|0
South Carolina(SEC)|40|9|8|4|0|4|0|0
Tennessee(SEC)|22|0|0|0|0|1|0|0
Vanderbilt(SEC)|27|0|23|24|0|2|1|0

This is the football class

public class Football implements Comparable<Football>
{
    public String name;
    public int wins,finalRecord14,finalRecord13,finalRecord12,finalRecord11,bowlVictories,bowlLosses,ConferenceChamps;
    public Integer points;

    public Football(String name,int wins,int finalRecord14,int finalRecord13,int finalRecord12,int finalRecord11,int bowlVictories,int bowlLosses,int ConferenceChamp,Integer points)
    {
           this.name=name;
           this.wins=wins;
           this.finalRecord14=finalRecord14;
           this.finalRecord13=finalRecord13;
           this.finalRecord12=finalRecord12;
           this.finalRecord11=finalRecord11;
           this.bowlVictories=bowlVictories;
           this.bowlLosses=bowlLosses;
           this.ConferenceChamps=ConferenceChamp;
           this.points=points;
    }
    public String getName()
    {
        return name;
    }
    public int getWins()
    {
        return wins;
    }
    public int getfinalRecord14()
    {
        return finalRecord14;
    }
    public int getfinalRecord13()
    {
        return finalRecord13;
    }
    public int getfinalRecord12()
    {
        return finalRecord12;
    }
    public int getfinalRecord11()
    {
        return finalRecord11;
    }
    public int getBowlVictories()
    {
        return bowlVictories;
    }
    public int getBowlLosses()
    {
        return bowlLosses;
    }
    public int getConferenceChamps()
    {
        return ConferenceChamps;
    }
    public Integer getPoints()
    {
        return points;
    }
    public int compareTo(Football o)
    {


        return getPoints().compareTo(o.getPoints());
    }
}

Upvotes: 0

Views: 62

Answers (1)

qwwqwwq
qwwqwwq

Reputation: 7309

As far as a HashSet is concerned in java, if you call object1.equals(object2), and that method returns true, those two objects are identical. It does not matter if those two objects have a million fields different between them, all that matters is how the equals method is implemented. And we already know Sets cannot have duplicate elements.

If you are using a TreeSet then you must also implement a compareTo method consistent with your equals method, in that compareTo returns 0 when two objects are effectively identical.

Java does not do any sort of magic for you, such as hashing all the bytes in both objects, or serializing them and comparing them (not all objects are serializable!), or anything like that. It's up to you to provide the criteria for when two objects are effectively identical.

Since you want unique football teams in your set, your equals method should include a comparison of the team names etc, not just the number of points, as multiple teams might have the same number of points.

Upvotes: 2

Related Questions