Java Vector Data Grouping - Relevant Code

2019-07-24 00:06发布

问题:

I am currently importing excel data into a vector in Java, however now i want to group that data so that similar data with lets say the same id is grouped (in my case matter number).

here is my relevant import code:

 public class FileChooser extends javax.swing.JFrame {
 private String path ="";

 public FileChooser() {
 initComponents();
 }

private static Vector importExcelSheet(String fileName)
{
Vector cellVectorHolder = new Vector();
try
{
Workbook workBook = WorkbookFactory.create(new FileInputStream(fileName));
Sheet sheet = workBook.getSheetAt(0);
Iterator rowIter = sheet.rowIterator();

while(rowIter.hasNext())
{
    XSSFRow row = (XSSFRow) rowIter.next();
    Iterator cellIter = row.cellIterator();
    Vector cellStoreVector=new Vector();

    while(cellIter.hasNext())
    {
        XSSFCell cell = (XSSFCell) cellIter.next();
        cellStoreVector.addElement(cell);
    }
    cellVectorHolder.addElement(cellStoreVector);
}
}
catch (Exception e)
{
 System.out.println(e.getMessage());
}
return cellVectorHolder;
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

   JFileChooser chooser = new JFileChooser();
   chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
   int option = chooser.showOpenDialog(this); // parentComponent must a component like JFrame, JDialog...
   if (option == JFileChooser.APPROVE_OPTION) {
   File selectedFile = chooser.getSelectedFile();
   path = selectedFile.getAbsolutePath();
   jTextField1.setText(path);
  }
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

     Vector dataHolder = importExcelSheet(path);
     Enumeration e = dataHolder.elements();  // get all vector elements
     while (e.hasMoreElements()) {       // step through all vector elements
     Object obj = e.nextElement();
     System.out.println(obj);
   }// TODO add your handling code here:

}

This selects the excel spreadsheet and extracts all the values to a vector and prints them out, what i want to know is, can i group the same data in the vector and output that data instead of the whole thing.

So lets say i have a spreadsheet of cellphone contracts, and i want to select all the contracts of the Samsung galaxy s3 and not all the contracts, how would i do that?

回答1:

You're suffering from Object Denial :-)

As I read your code, you have a Vector of rows containing a Vector of columns, is that correct? And if so, does one of the columns contain the phone model?

Anyways, you could be doing something along the lines of:

// TODO seriously consider something else than Vector to store the rows and columns!
Map<String,Vector> map = new HashMap<String,Vector>()

while(rowIter.hasNext())
{
    boolean isFirst = false;
    String phoneModel = "";
    while( cellIter.hasNext() )
    {
        XSSFCell cell = (XSSFCell) cellIter.next();
        if ( isFirst ) { phoneModel = cell.getTheTextContentOrWhatever(); isFirst = false; }
        cellStoreVector.addElement(cell);
    }
    if ( map.get( phoneModel ) == null ) { map.put( phoneModel , new Vector() ); }
    map.get( phoneModel ).add( cellStoreVector );
}

Then, the map keys will be your phones, and the value will be the Vector with the rows for that phone. It is not what I consider pretty code and needs works in terms of error handling, but you can work from there.

Cheers,