7

I have a array list in which I bind the data This is a example

MyStrings =new ArrayList<String>();
MyStrings.add("Dog");
MyStrings.add("Cat");
MyStrings.add("Can");
MyStrings.add("Ant");
MyStrings.add("Str");

Now I have a string String sweet="c"; Now what OI want is to filter that Arraylist based on my string(sweet) so the items of the MyStrings will be only Cat and Can

EDIT I am really sorry for the trouble I got you but my main problem is that sweet is a editable Ive tried using this code

      public void onTextChanged(CharSequence s, int start, int before,int count) {  
        //adapter2.getFilter().filter(s);
        //int length = filterEditText.getText().length();
        filterME  = filterEditText.getText();
        List<String> MySortStrings =new ArrayList<String>();
        for(int i=0;i<MyStrings.size();i++)
        {
            String newString = MyStrings.get(i);
            if (newString.startsWith(filterME)){

            }
        }
        //adapter2 = new LazyAdapterGetFriends(MyFriends.this,x);
         //list.setAdapter(adapter2);
    }

using this declaration

    LazyAdapterGetFriends adapter2;
ArrayList<String> MyStrings;
//List<String> MyStrings;
EditText filterEditText;

Sorry for my wrong question.. Foolish me

Androyd
  • 129
  • 1
  • 1
  • 6

6 Answers6

8
List<String> MyStrings =new ArrayList<String>();
List<String> MySortStrings =new ArrayList<String>();
MyStrings.add("Dog");
MyStrings.add("Cat");
MyStrings.add("Can");
MyStrings.add("Ant");
MyStrings.add("Str");
String sweet="c";
for(int i=0;i<MyStrings.size();i++)
{
    if(MyStrings.get(i).startsWith(sweet.toUpperCase()))
    {
        MySortStrings.add(MyStrings.get(i));
    }
}

System.out.println(MySortStrings.size());

The list MySortStrings contains the Cat & Can

Conchylicultor
  • 4,631
  • 2
  • 37
  • 40
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
3

These days you can also use streams to do it easily:

stringList.stream().filter(s -> s.contains("c")).collect(Collectors.toList())

When you would only need to know if there is a string in the list containing your letter (not part of the question but very useful) you can do this:

stringList.stream().anyMatch(s -> s.contains("c"))
Chris
  • 432
  • 3
  • 14
1

The naive algorithm will be that you just filter everything out like this:

ArrayList<String> filtered = new ArrayList<String>();
for(String s : MyStrings){
if(s.substring(0,1).toLowerCase().equals("c")){
filtered.add(s);
}
}

but then you have access time in O(n).

if you need a more faster way you probably need to use a Key,Value Structure with Key set to the String you need to filter. Or even a http://en.wikipedia.org/wiki/Trie, where you can easily filter on every character in the string. But then you will need extra time in building up this thing.

Okay, this should be it when using your TextWatcher Stuff (untested...)

  private List<String> MySortStrings = new ArrayList<String>(); // assume that your data is in here!
  private List<String> MySortedStrings = new ArrayList<String>(); // this will be the list where your sorted strings are in. maybe you could also remove all strings which does not match, but that really depends on your situation!

  public void onTextChanged(CharSequence s, int start, int before,int count) {  
    for(String str : MySortStrings){
        if(str.startsWith(s.toString()){
            MySortedStrings.add(str);
        }
    }
}
reox
  • 5,036
  • 11
  • 53
  • 98
1

Use str.startsWith(String, int index)

Index will tell you from which index in the str it should start comparing

abhi
  • 337
  • 1
  • 7
  • 12
0

If you want to remove items that don't match from MyStrings rather than create a new ArrayList you will need to use an Iterator as this is the only safe way to modify a ArrayList while iterating over it.

myStrings = new ArrayList<String>();
myStrings.add("Dog");
myStrings.add("Cat");
myStrings.add("Can");
myStrings.add("Ant");
myStrings.add("Str");
String sweet="c";

sweet = sweet.toLowerCase();
Iterator<String> i = myStrings.iterator();

while (i.hasNext()) {
  if (! i.next().toLowerCase().startsWith(sweet)) {
    i.remove();
  }
}
David Webb
  • 190,537
  • 57
  • 313
  • 299
  • @Androyd what is an editable string and why you cant use startsWith with it? – reox Sep 10 '12 at 09:58
  • @reox and editable string is like a changable string. Its more like when you type on textbox the editable string will capture every char you type in – Androyd Sep 10 '12 at 10:01
  • @Androyd okay, but at the end its still from Type String? – reox Sep 10 '12 at 10:07
0

You can use the apache commons-collections library as well:

CollectionUtils.filter(myStrings,
  new Predicate() {
    public boolean evaluate(Object o) {
       return !  ((String)o).startsWith("c");
    }
  }
};

Any object for which the "evaluate" method of the Predicate class returns false is removed from the collection. Keep in mind, that like the solution above using the Iterator, this is destructive to the list it is given. If that is an issue, you can always copy the list first:

List<String> filtered = new ArrayList<String>(myStrings);
CollectionUtils.filter(filtered, ...);
Matt
  • 11,523
  • 2
  • 23
  • 33