I'm trying to implement my own JTable RowFilter, since I'm using Java 1.4 (RowFilter doesn't seem to exist in this version). I still believe however that the algorithm that I'm using can be replaced by a much faster one. I have tried my algorithm on a dummy table that contains 30.000 records and 8 columns, and I'm getting the result in less than a second. But sometimes, there's this little delay that occurs while typing in the Search Criteria (Which is basically a JTextField with a DocumentListener). Here's the algorithm I'm using:
public void searchList()
{
for(int i=0;i<list.size();i++)
{
Employee e=(Employee)list.get(i);
Pattern pattern=Pattern.compile(search.getText(),Pattern.CASE_INSENSITIVE);
Matcher matcher=pattern.matcher(e.getFname());
if(matcher.find())
{
result.add(e);
continue;
}
matcher=pattern.matcher(e.getLname());
if(matcher.find())
{
result.add(e);
continue;
}
matcher=pattern.matcher(e.getHeight());
if(matcher.find())
{
result.add(e);
continue;
}
matcher=pattern.matcher(e.getOccupation());
if(matcher.find())
{
result.add(e);
continue;
}
matcher=pattern.matcher(e.getSize());
if(matcher.find())
{
result.add(e);
continue;
}
matcher=pattern.matcher(e.getSkills());
if(matcher.find())
{
result.add(e);
continue;
}
matcher=pattern.matcher(e.getSsn());
if(matcher.find())
{
result.add(e);
continue;
}
matcher=pattern.matcher(e.getStrength());
if(matcher.find())
{
result.add(e);
}
}
model.fireTableDataChanged();
table.updateUI();
}
}
The main datastructure that I'm using to bind data to my TableModel is an ArrayList that holds objects of a class called "Employee". Another ArrayList called result, contains all the "Employee" objects that matched the search criteria. Bear in mind that the filtering is happening on all 8 columns. The only optimization that I think I did is adding the "Employee" object on the first column match, instead of having to go through the rest of the columns.
Any suggestions regarding this matter? Thanks a lot for the help =)
Since you seem to be searching for the exact same value in all fields, I would just concatenate them and do the matching once.
Also, I don't think there is any good reason why you would compile the pattern in every iteration.