无法从jtable中删除重复项。

时间:2015-01-24 21:37:09

标签: java mysql hashset duplicate-removal

我在mysql中有两个不同的表,我想将它们组合在一个表中。我合并但有副本,我不能删除它们。我首先使用了Hashset,它适用于第一个表,但是当我合并第二个表时,它就消失了。现在我不想写一些长时间的计算,因为项目太大而且它使用数据库所以它会很慢。

enter image description here

public final class IsdenCixanlar extends javax.swing.JFrame {

public final String[][] array;
public final List<Pojo> list;
public int index = 0;
public String ad = null;

public JTable autoResizeColWidth(JTable table, TableModel model) {
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setModel(model);
    int margin = 5;
    for (int i = 0; i < table.getColumnCount(); i++) {
        int vColIndex = i;
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
        TableColumn col = colModel.getColumn(vColIndex);
        int width;
        TableCellRenderer renderer = col.getHeaderRenderer();
        if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }
        Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
        width = comp.getPreferredSize().width;
        for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, vColIndex);
            comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                    r, vColIndex);
            width = Math.max(width, comp.getPreferredSize().width);
        }
        width += 2 * margin;
        col.setPreferredWidth(width);
    }
    ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(
            SwingConstants.LEFT);
    table.getTableHeader().setReorderingAllowed(false);
    return table;
}

public IsdenCixanlar() {
    initComponents();
    //setTitle("İşdən çıxanlar");
    setLocationRelativeTo(null);
    list = new ArrayList<>(DBO.salary());
    for (Iterator<Pojo> itr = list.iterator(); itr.hasNext();) {
        Pojo element = itr.next();
        if (element.getIsleyir() == 0) {
            itr.remove();
        }
        if ("null".equals(element.getAd())) {
            itr.remove();
        }
        if ("".equals(element.getAd())) {
            itr.remove();
        }
    }
    array = new String[list.size()][2];
    list.stream().forEach((list1) -> {
        if (ad == null ? list1.getAd() != null : !ad.equals(list1.getAd())) {
            array[index][0] = list1.getAd();
            array[index][1] = DBO.cixarilma_qayitma_Find_Tarix(array[index][0], 0).getTarix();
            index++;
        }
        ad = list1.getAd();
    });
    DefaultTableModel model = new DefaultTableModel(
            array,
            new String[]{
                "Ad / Soyad / Ata adı", "İşdən çıxarılma tarixi"
            }
    ) {
        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };
    table = autoResizeColWidth(table, model);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setModel(model);
    table = new JTable(model);
    JScrollPane pane = new JScrollPane(table);
    add(pane);
}

1 个答案:

答案 0 :(得分:0)

找到答案。

list = new ArrayList<>(DBO.salary());
    for (Iterator<Pojo> itr = list.iterator(); itr.hasNext();) {
        Pojo element = itr.next();
        if (element.getIsleyir() == 0) {
            itr.remove();
        }
        if ("null".equals(element.getAd())) {
            itr.remove();
        }
        if ("".equals(element.getAd())) {
            itr.remove();
        }
    }
    HashSet hs = new HashSet();
    ArrayList al = new ArrayList();
    list.stream().forEach((list1) -> {
        hs.add(list1.getAd());
    });
    al.addAll(hs);
    array = new String[list.size()][2];
    al.stream().forEach((list1) -> {
        if (ad == null ? list1 != null : !ad.equals(list1)) {
            array[index][0] = (String) list1;
            array[index][1] = DBO.cixarilma_qayitma_Find_Tarix(array[index][0], 0).getTarix();
            index++;
        }
        ad = (String) list1;
    });