LinkedHashMap的比较器 [英] comparator for LinkedHashMap

查看:174
本文介绍了LinkedHashMap的比较器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个程序,以从excel文件中读取数据并将其存储在表中。但是由于我是Java比较器的新手,所以我很难做到其中之一。这是我的问题,我设法从Excel文件中将所有数据作为字符串读取,并将它们存储在表中。但是我的项目是通过升序ID将它们存储在表中。有人可以帮助我创建一个比较器来比较LinkedHashmap并通过其ID存储它们吗?
我必须存储的数据如下:

i am making a program to read data from excel files and store them in tables. But since I am new to Comparators in Java i have difficulties in making one of them. Here is my issue, I have managed to read all the data from excel files as a string and store them in a table. But my project is to store them in table by ascending IDs. Can someone help me to create a Comparator to compare LinkedHashmaps and store them by their IDs? The data that I have to store is like the below:

ID  Name    Salary         
50  christine   2349000        
43  paulina 1245874        
54  laura   4587894 

解析数据的代码是以下内容:

The code for parsing the data is the below:

private static LinkedHashMap parseExcelColumnTitles(List sheetData) {

    List list = (List) sheetData.get(0);
    LinkedHashMap < String, Integer > tableFields = new LinkedHashMap(list.size());
    for (int j = 0; j < list.size(); j++) {
        Cell cell = (Cell) list.get(j);
        tableFields.put(cell.getStringCellValue(), cell.getCellType());
    }

    return tableFields;

}

private static LinkedHashMap[] parseExcelColumnData(List sheetData) {

    LinkedHashMap[] tousRows = new LinkedHashMap[sheetData.size() - 1];
    for (int rowCounter = 1; rowCounter < sheetData.size(); rowCounter++) {

        List list = (List) sheetData.get(rowCounter);

        LinkedHashMap < String, Integer > tableFields = new LinkedHashMap(list.size());
        String str;
        String[] tousFields = new String[list.size()];

        int i = 0;

        for (int j = 0; j < list.size(); j++) {
            Cell cell = (Cell) list.get(j);
            if (cell != null) {
                if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    tableFields.put(String.valueOf(cell
                        .getNumericCellValue()), cell.getCellType());
                } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    tableFields.put(cell.getStringCellValue(), cell
                        .getCellType());
                } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                    tableFields.put(String.valueOf(cell
                        .getBooleanCellValue()), cell.getCellType());
                }
            }

        }
        tousRows[rowCounter - 1] = tableFields;
    }

    return tousRows;

}


推荐答案

您可以做这样的模型:

public class Salary implements Comparable<Salary> {
    private Long id;
    private String name;
    private Double salary;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    @Override
    public int compareTo(Salary o) {
        if (o.getId() < this.id) {
            return -1;
        } else if (o.getId() > this.id) {
            return 1;
        }
        return 0;
    }
}

然后您可以使用集合.sort(list)订购您的 LinkedHashMap

Then you can use Collections.sort(list) to order your LinkedHashMap

这篇关于LinkedHashMap的比较器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆