Misol org.apache.commons.collections4.CollectionUtils.filterInverse() usulidan foydalanishni ko'rsatadi . Biz faqat toq sonlarni olish uchun butun sonlar roʻyxatini filtrlaymiz.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
public class CollectionUtilsTester {
public static void main(String[] args) {
List integerList = new ArrayList();
integerList.addAll(Arrays.asList(1,2,3,4,5,6,7,8));
System.out.println("Original List: " + integerList);
CollectionUtils.filterInverse(integerList, new Predicate() {
@Override
public boolean evaluate(Integer input) {
if(input.intValue() % 2 == 0) {
return true;
}
return false;
}
});
System.out.println("Filtered List (Odd numbers): " + integerList);
}
}
Natija: Original List: [1, 2, 3, 4, 5, 6, 7, 8]
Filtered List (Odd numbers): [1, 3, 5, 7]
Commons Collections-Ob’ektlarni o’zgartirish
Apache Commons Collections kutubxonasining CollectionUtils klassi keng ko'lamli foydalanish holatlarini qamrab oladigan umumiy operatsiyalar uchun turli yordamchi usullarni taqdim etadi. Bu qozon kodini yozishdan qochishga yordam beradi. Ushbu kutubxona jdk 8 dan oldin juda foydali, chunki shunga o'xshash funksiyalar hozir Java 8 ning Stream API-da taqdim etilgan.
Ro'yxatni o'zgartirish CollectionUtils ning collect() usuli bir turdagi ob'ektlar ro'yxatini turli turdagi ob'ektlar ro'yxatiga aylantirish uchun ishlatilishi mumkin.
Deklaratsiya org.apache.commons.collections4.CollectionUtils.collect() usuli -
public static Collection collect(Iterable inputCollection, Transformer super I,? extends O> transformer)
Parametrlar inputCollection - kirishni olish uchun to'plam null bo'lmasligi mumkin.
Transformator - Foydalanish uchun transformator nol bo'lishi mumkin.
Qaytish qiymati O'zgartirilgan natija (yangi ro'yxat).
Istisno NullPointerException - Agar kirish to'plami null bo'lsa.