Java 矢量数据分组 - 相关代码 [英] Java Vector Data Grouping - Relevant 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).
这是我的相关导入代码:
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 电子表格并将所有值提取到向量中并将它们打印出来,我想知道的是,我可以将向量中的相同数据分组并输出该数据而不是整个数据.
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.
假设我有一份手机合同电子表格,我想选择三星 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?
推荐答案
您正在遭受 对象拒绝 :-)
当我阅读您的代码时,您有一个 Vector
行,其中包含一个列向量,对吗?如果是,其中一列是否包含手机型号?
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 );
}
然后,地图键将是您的手机,而值将是带有该手机行的 Vector
.这不是我认为漂亮的代码,需要在错误处理方面起作用,但您可以从那里开始工作.
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 矢量数据分组 - 相关代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!