有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java在ArrayList中删除重复项的问题

enter image description here我从CSV文件中读取行并将它们导入到ArrayList中,但结果如下所示。这被认为是索引0。如何将其固定到多个index中?(即[亚洲,亚洲,亚洲,非洲,非洲…])empty[10]是excel工作表中的列位置

    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


共 (1) 个答案

  1. # 1 楼答案

    如果不希望在集合中有重复项,一种方法是使用不包含重复项的集合,如Set

    因此,您可以做的是:

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

    另一种方法是使用流的distinct函数,如:

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

    但在任何情况下,都不应在每次读取新行时创建新数组

    另一种解决问题的方法是,在阅读csv的所有行之后,您可以按大陆分组,并按案例求和,如:

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