Filter
ArrayUtil.filtermethod is used to edit an existing array element, only for a generic array operation, and the original type array is not provided. The editor interface in the method is used to return the edited value of each element, and returns NULL when this element is discarded.
Example: Filtering an array, preserving only an even number
integer[] a = {1, 2,3,4,5, 6};integer[] filter = Arrayutil.filter (A, new Editor< Integer> () { @Override public Integer edit (Integer t) {return (t% 2 = 0)? T: null}}); Assert.assertarrayequals (filter, new integer[]{2,4,6});
where Arrayutil.filter () passes an anonymous inner class that implements the method in curly braces.
which
Filter method
/*** Filter * *@param<T> array element type *@paramArray Arrays *@paramEditor interface *@returnFiltered Array*/ Public Static<T> t[] Filter (t[] array, editor<t>editor) {ArrayList<T> list =NewArraylist<t>(); T modified; for(T t:array) {modified=Editor.edit (t); if(NULL!=modified) {List.add (t); } } returnList.toarray (arrays.copyof (array, list.size ())); }
Editor Interface:
/*** Editor interface, commonly used for unified editing of elements in a collection <br> * This editor two functions: * * <pre> * 1, if the return value is <code>null</code>, indicating that this value is discarded * 2, Make changes to Objects * </pre> * *@param<T> Edited object type *@authorLooly*/ Public InterfaceEditor<t> { /*** Modify the filtered results * *@paramT-Filtered object *@returnmodified object, if filtered back <code>null</code>*/ PublicT -edit (t-t);}
Arrayutil of Java Common tool class