简体   繁体   中英

hashmap with one key and multiple values in Jtable?

it is possible to implement a hashmap with one key and multiple values like

Map<Object,ArrayList<Object>> multiMap = new HashMap<Object,ArrayList<Object>>();

in a Jtable? I have such a hashmap but I'm just able to fill 2 rows...

You can output the same key multiple times with different data, for example

Key 1 : Data 1
Key 1 : Data 2

then group keys that are the same. See This Link for details on how to set a model for grouping.

I have such a hashmap but I'm just able to fill 2 rows...

You have a problem with your TableModel.

Here is an example that uses:

Map<Integer, Map<Integer, Object>> data = new HashMap<Integer, HashMap<Integer, Object>>();

as the data storage for the TableModel:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableSparse extends JPanel
{
    public TableSparse(int row, int column)
    {
        setLayout( new BorderLayout() );

        TableModel model = new SparseTableModel(row, column);
        JTable table = new JTable(model);
        table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
        JScrollPane scrollPane = new JScrollPane( table );
        add( scrollPane );

        model.setValueAt("one", 0, 0);
        model.setValueAt("two", 5, 5);
    }

    class SparseTableModel extends AbstractTableModel
    {
        Map<Integer, Map<Integer, Object>> data = new HashMap<Integer, Map<Integer, Object>>();
        int rows;
        int columns;

        public SparseTableModel(int rows, int columns)
        {
            this.rows = rows;
            this.columns = columns;
        }

        @Override
        public int getRowCount() { return rows; }

        @Override
        public int getColumnCount() { return columns; }

        @Override
        public boolean isCellEditable(int row, int column) { return true; }

        @Override
        public Object getValueAt(int row, int column)
        {
            //  Check for row

            Integer key = new Integer(row);
            Map<Integer, Object> rows = data.get(key);

            if (rows == null) return null;

            //  Now check for column

            key = new Integer(column);
            return rows.get(key);
        }

        @Override
        public void setValueAt(Object value, int row, int column)
        {
            //  Remove cell data

            if (value.toString().equals(""))
            {
                removeCellData(row, column);
                return;
            }

            //  Save cell data

            Integer key = new Integer(row);
            Map<Integer, Object> rows = data.get(key);

            if (rows == null)
            {
                rows = new HashMap<Integer, Object>();
                data.put(key, rows);
            }

            key = new Integer(column);
            rows.put(key, value);
        }

        private void removeCellData(int row, int column)
        {
            Integer rowKey = new Integer(row);
            Map<Integer, Object> rows = data.get(rowKey);

            if (rows == null) return;

            Integer columnKey = new Integer(column);
            rows.remove(columnKey);

            if (rows.isEmpty())
                data.remove(rowKey);
        }

    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new TableSparse(100, 256) );
        frame.setSize(600, 600);
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

You should be able to modify the code to use a HashMap of ArrayLists for the model data.

A key can be mapped with a Object only.But you can always add an Object into the ArrayList present as value

Map<Object,ArrayList<Object>> multiMap = new HashMap<Object,ArrayList<Object>>();
Object key ;//key to be searched
Object value ;
 if(multiMap.get(key)==null){    
   ArrayList<Object> arObj = new ArrayList<Object>();
   arObj.add(value);
   multiMap.put(key,arObj);
 }
 else{
   multiMap.get(key).add(value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM