Prisoner.627
Prisoner.627

Reputation: 35

Java removing duplicates problem in ArrayList

enter image description hereI read lines from a CSV file and imported those to the ArrayList but comes out as below. This is considered as index 0 . How do I fix this into multiple index? (I.E [Asia, Asia, Asia, Africa, Africa...]). empty[10] is a column position in excel sheet.

    public class COVIDDataAnalyzer
{
    // You will need to define attributes to manage the data here!
    ArrayList<COVIDData> covidDataList = new ArrayList<COVIDData>();
    Map<String, Integer> continentByCases = new HashMap<String, Integer>();
    
    /**
     * Read the data in the know WHO format from the specified file
     * @param filename the name of the file to read
     * @return a list of COVIDData objects read from the file
     * @throws ParseException 
     */
    public List<COVIDData> readFile(String filename) throws IOException, ParseException
    {
        try (Scanner input = new Scanner(new File(filename));){
            if (input.hasNext() == true) {
                input.nextLine();
                while (input.hasNextLine()) {
                    covidDataList.add(getRecordFromLine(input.nextLine()));
                }
            } else {
                return null;
            }
        }
        return covidDataList;
    }
    
    private COVIDData getRecordFromLine(String line) throws ParseException{
        
        line.split(",");
        String [] empty = line.split(",");
        
        long cases = Long.parseLong(empty[4]);
        long death = Long.parseLong(empty[5]);
        long population = Long.parseLong(empty[9]);
        int year = Integer.parseInt(empty[3]) - 1900;
        int month = Integer.parseInt(empty[2]) - 1;
        int dates = Integer.parseInt(empty[1]);
        
        List<String> continent = new ArrayList<String>();
        continent.add(empty[10]);
        
        
        
        System.out.println(continent);

        Date date = new Date(year, month, dates);
        
        COVIDData data = new COVIDData(date, cases, death, empty[6], population, empty[10]);
        return data;


    }
public class COVIDData
{
    private Date day;
    private long cases;
    private long deaths;
    private String country;
    private String continent;
    private long population;

    public COVIDData(Date day, long cases, long deaths, String country, long population, String continent)
    {
        super();
        this.day = day;
        this.cases = cases;
        this.deaths = deaths;
        this.country = country;
        this.population = population;
        this.continent = continent;
    }

    public Date getDay()
    {
        return day;
    }

    public long getCases()
    {
        return cases;
    }

    public long getDeaths()
    {
        return deaths;
    }

    public String getCountry()
    {
        return country;
    }

    public long getPopulation()
    {
        return population;
    }

    public String getContinent()
    {
        return continent;
    }

    @Override
    public String toString()
    {
        return "COVIDData [day=" + day + ", cases=" + cases + ", deaths=" + deaths + ", country=" + country
                + ", continent=" + continent + ", population=" + population + "]";
    }

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + (int) (cases ^ (cases >>> 32));
        result = prime * result + ((continent == null) ? 0 : continent.hashCode());
        result = prime * result + ((country == null) ? 0 : country.hashCode());
        result = prime * result + ((day == null) ? 0 : day.hashCode());
        result = prime * result + (int) (deaths ^ (deaths >>> 32));
        result = prime * result + (int) (population ^ (population >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        COVIDData other = (COVIDData) obj;
        if (cases != other.cases)
            return false;
        if (continent == null)
        {
            if (other.continent != null)
                return false;
        } else if (!continent.equals(other.continent))
            return false;
        if (country == null)
        {
            if (other.country != null)
                return false;
        } else if (!country.equals(other.country))
            return false;
        if (day == null)
        {
            if (other.day != null)
                return false;
        } else if (!day.equals(other.day))
            return false;
        if (deaths != other.deaths)
            return false;
        if (population != other.population)
            return false;
        return true;
    }
}

Console Result
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Asia]
[Africa]
[Africa]
[Africa]
[Africa]
[Africa]
[Africa]
[Africa]
[Africa]
[Africa]
[Africa]
[Africa]

Thx.

Upvotes: 1

Views: 119

Answers (1)

Matthieu Gabin
Matthieu Gabin

Reputation: 952

If you don't want to have duplicate in a collection one way is to use a collection that doesn't contains duplicate like Set

So what you can do would looks like :

Set<String> continents = new HashSet<String>();
continents.add(empty[10]);

One other would be to use the distinct function of the stream like :

continents
 .stream()
 .distinct()
 .collect(Collectors.toUnmodifiableList());

But in any case you should not create a new Array each time you read a new row.

An other way to solve your problem is after reading the all line of your csv you could groupBy continent and sum by cases like :

List<COVIDData> covidData = new COVIDDataAnalyzer().readFile(filePath);

Map<String, Long> collect = covidData
 .stream()
 .collect(Collectors.groupingBy(COVIDData::getContinent,
  Collectors.summingLong(COVIDData::getCases)));

Upvotes: 2

Related Questions