首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ArrayList中的Java去重问题

ArrayList中的Java去重问题
EN

Stack Overflow用户
提问于 2021-05-27 10:17:36
回答 1查看 91关注 0票数 0

我从CSV文件中读取了行,并将它们导入到ArrayList中,但结果如下所示。这被认为是索引0。如何将其修复为多个index?(即亚洲、非洲、非洲...)。empty[10]是excel表格中的列位置。

代码语言:javascript
复制
    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;


    }
代码语言:javascript
复制
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;
    }
}
代码语言:javascript
复制
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]

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2021-05-28 00:57:16

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

所以你能做的是:

代码语言:javascript
复制
Set<String> continents = new HashSet<String>();
continents.add(empty[10]);

另一种是使用流的distinct函数,如下所示:

代码语言:javascript
复制
continents
 .stream()
 .distinct()
 .collect(Collectors.toUnmodifiableList());

但在任何情况下,您都不应该在每次读取新行时都创建一个新的Array。

另一种解决问题的方法是,在阅读完你的csv的所有行后,你可以groupBy continent,并按以下情况求和:

代码语言:javascript
复制
List<COVIDData> covidData = new COVIDDataAnalyzer().readFile(filePath);

Map<String, Long> collect = covidData
 .stream()
 .collect(Collectors.groupingBy(COVIDData::getContinent,
  Collectors.summingLong(COVIDData::getCases)));
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67714845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档