Java 8分组无法通过列表参数分组

时间:2019-05-30 03:10:32

标签: java

我遇到一个标题描述的问题。下面是代码。我希望有人可以帮助我。真的很感激。

// this is a column class
public class Column {

    private String name;

    private Object value;

    public String getName() {
        return name;
    }

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

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

     @Override
    public boolean equals(Object obj) {
        if(this == obj)
            return true;

        if(obj == null || obj.getClass()!= this.getClass())
            return false;

        Column column = (Column) obj;

        // comparing the state of argument with
        // the state of 'this' Object.
        return (column.name == this.name && column.value == this.value);
    }

    @Override
    public int hashCode() {
        return this.value.hashCode();
    } 

}

// this is Table class
public class PhysicalTable {

    private String name;

    private List<Column> indexIdsColumns;

    private List<Column> columns;

    public String getName() {
        return name;
    }

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

    public List<Column> getColumns() {
        return columns;
    }

    public void setColumns(List<Column> columns) {
        this.columns = columns;
    }

    public List<Column> getIndexIdsColumns() {
        return indexIdsColumns;
    }

    public void setIndexIdsColumns(List<Column> indexIdsColumns) {
        this.indexIdsColumns = indexIdsColumns;
    }

}

// group by logic, this part is core logic.
Map<String, List<PhysicalTable>> tableMap = allTables.stream().collect(Collectors.groupingBy(PhysicalTable::getName));
        Iterator<Map.Entry<String, List<PhysicalTable>>> entries = tableMap.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, List<PhysicalTable>> entry = entries.next();
            String tableName = entry.getKey();
            List<PhysicalTable> tablesWithSameName = entry.getValue();

            //group by index ids
            Map<List<Column>, List<PhysicalTable>> map = tablesWithSameName.stream().collect(Collectors.groupingBy(PhysicalTable::getIndexIdsColumns, Collectors.toList()));
            Iterator<Map.Entry<List<Column>,List<PhysicalTable>>> iterator  = map.entrySet().iterator();
        }

我希望java groupingBy表具有indexIdsColumns,但是它不起作用!还是Java不支持使用列表参数进行分组?

如果按解决方案进行分组很好,是否还有其他方法可以对具有索引id列的表进行分组。

谢谢!

0 个答案:

没有答案