String Arraylist will get an element starting with this string

I have a list of arrays to which I bind data. This is an 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 wants is to filter this Arraylist based on my string (sweet) so MyStrings elements will only be Cat and Can

EDIT I'm really sorry for the troubles I got from you, but my main problem is that sweet is editable Ive tried to use 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 ad

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

Sorry for my wrong question .. Stupid I

+6
source share
5 answers
 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()); 

MySortStrings list contains Cat and Can

+9
source

The naive algorithm will be that you just filter everything 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 faster way, you probably need to use Key, Value Structure with the Key set to String, which you need to filter out. Or even http://en.wikipedia.org/wiki/Trie , where you can easily filter each character in a string. But then you need extra time to create this thing.

Well, that should be 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); } } } 
+1
source

Use str.startsWith (String, int index)

The index will tell you from which index on str it should start comparing

+1
source

If you want to remove elements that do not match MyStrings , rather than create a new ArrayList , you will need to use a Iterator , as this is the only safe way to change the ArrayList when ArrayList over.

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

You can also use the apache collection collections library:

 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 destroys the list that it sets. If this is a problem, you can always copy the list:

 List<String> filtered = new ArrayList<String>(myStrings); CollectionUtils.filter(filtered, ...); 
0
source

Source: https://habr.com/ru/post/925001/


All Articles