【问题标题】:comparator for LinkedHashMapLinkedHashMap 的比较器
【发布时间】:2013-04-30 07:05:39
【问题描述】:

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

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

解析数据的代码如下:

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;

}

【问题讨论】:

  • Map 中的 (key,value) 对是什么?

标签: java comparator linkedhashmap


【解决方案1】:

您可以使用TreeMap 代替LinkedHashMap,因为您不想保留插入顺序,而对于排序TreeMap 是更好的选择。 也读这个Java TreeMap Comparator

【讨论】:

    【解决方案2】:

    你可以做一个这样的模型:

    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;
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      相关资源
      最近更新 更多