Clicky

Can you please suggest a strategy for filtering an arraylist?

--Reading rest of this may not be necessary if question makes sense --

For example, I have a list of Contacts. I would like to be able to filter the contacts when a user types in a JTextField. I would like to use keypress listener to automatically filter my JList of contacts after each key is pressed. I believe i am ok with keypress code and modifying the JList DefaultListModel. I am unclear on what to research as filter method.

I see examples on internet but they look cryptic to me. I want to make sure i am looking in the right direction before experimenting with code. :-)

Thank you!

Example Contact class
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
public class Contact
{

private String firstName;
private String lastName;
private int contactID;

public Contact()
{
  firstName = "";
  lastName = "";
  contactID = 0;
}

public Contact(String firstName, String lastName, int contactID)
{
  this.firstName = firstName;
  this.lastName = lastName; 
  this.contactID = contactID;
}

//Getters + setters ...

}


Here is a test class with ArrayList to be filtered.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
public class TestFilter {
	public static void main(String[] args) 
        {
		//Retrieve contacts from DB.
		ContactsDatabase db = ContactsDatabase.getInstance();
	
		ArrayList<Contact> contacts = new ArrayList<Contact>();
		
                //Get all contacts from DB.
		for (Contact c : db.getAllContacts())
		{
			contacts.add(c);
		}
		
                //*****************************************************************
                //Lets say i filtered the contacts by name: John
                ArrayList<Contact> filteredContacts = myFilterMethod(contacts, "John");
                //*****************************************************************
		

                for (Contact c : filteredContacts)
		{
			System.out.println("contact: " + c.getLastName() + ", " + c.getFirstName());
		}		
      }
}

asked 11/05/2011 12:00

gregg_s's gravatar image

gregg_s ♦♦


4 Answers:
Maybe to try the startighforward way:

1:
2:
3:
4:
5:
6:
7:
8:
9:
public ArrayList<String> myFilterMethod(ArrayList<String> contacts, String nmae){
ArrayList<String> filtered = new ArrayList<String>();
for(String s : contacts){
if(s.startsWith(name)) filtered.add(s);

}

return filtered;
}
link

answered

for_yan's gravatar image

for_yan

Unless you have very long lists(which would not be very practical in  dropbox probably)
this filtering should work fije, it seesm to me
link

answered 2011-11-05 at 09:13:15

for_yan's gravatar image

for_yan

huh. so that is certainly a good way of doing it. Ill never have long list. This project is just learning how to pass objects around and other basics of introductory java techniques.

Thanks!
link

answered 2011-11-05 at 09:14:21

gregg_s's gravatar image

gregg_s

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×1
×3
×141

Asked: 11/05/2011 12:00

Seen: 394 times

Last updated: 11/05/2011 05:18