Java的矢量数据分组 - 相关code [英] Java Vector Data Grouping - Relevant Code

查看:202
本文介绍了Java的矢量数据分组 - 相关code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在导入Excel数据到Java中的一个载体,但现在我想组数据,以便与类似的数据可以说相同的ID进行分组(在我的情况下,无论数)。

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).

这是我的有关进口code:

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:

}

这个选择Excel的小号preadsheet并提取所有的值向量,并打印出来,我想知道的是,我可以组矢量相同的数据和输出数据,而不是整个事情

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.

因此​​,可以说,我有手机的合同A S preadsheet,我要选择的三星galaxy S3的所有合同,而不是所有的合同,我如何做到这一点?

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?

推荐答案

你患<一个href=\"http://stackoverflow.com/questions/3725703/how-to-store-more-than-one-string-in-a-map/3725728#3725728\">Object拒绝的: - )

当我看了你的code,你有一个包含列的向量行的矢量,是正确的?如果是这样,其中一列包含手机型号?

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

然后,映射键将你的手机,并且该值将是矢量与行该手机。这不是我所考虑的pretty code和在错误处理方面需要的作品,但你可以从那里工作。

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.

干杯,

这篇关于Java的矢量数据分组 - 相关code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆