8

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

6
  • i suggest you to follow the link saigeethamn.blogspot.in/2010/05/… as this may be useful Commented Sep 10, 2012 at 9:23
  • for loop? [at least 4 more chars] Commented Sep 10, 2012 at 9:23
  • use String.startsWith("c") and myString.remove(..) for string which does not start with "c" Commented Sep 10, 2012 at 9:24
  • Go for LamdaJ see this stackoverflow.com/a/10396356/739270 Commented Sep 10, 2012 at 9:28
  • i don't understand where the problem lies Commented Sep 10, 2012 at 9:38

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

2 Comments

Sir I am just following your logic but sorry I thought it was string but I found out that it must be Editable sweet;
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"))

Comments

1

Use str.startsWith(String, int index)

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

Comments

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

Comments

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

3 Comments

@Androyd what is an editable string and why you cant use startsWith with it?
@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 okay, but at the end its still from Type String?
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, ...);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.