String Arraylist get the item starting with this string

后端 未结 5 1710
长情又很酷
长情又很酷 2021-02-09 15:35

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

MyStrings =new ArrayList();
MyStrings.add(\"Dog\");
MyStrings.add(\"Cat\");
MyString         


        
相关标签:
5条回答
  • 2021-02-09 16:07

    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, ...);
    
    0 讨论(0)
  • 2021-02-09 16:13

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-09 16:18
    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

    0 讨论(0)
  • 2021-02-09 16:19

    Use str.startsWith(String, int index)

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

    0 讨论(0)
  • 2021-02-09 16:34

    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();
      }
    }
    
    0 讨论(0)
提交回复
热议问题