|
| 1 | +Filtrar listas |
| 2 | +```java |
| 3 | + private List<Banner> myCollectionA = new ArrayList<>(); |
| 4 | + |
| 5 | + myCollectionA.add(new Banner("r01", "www.google.com", "en")); |
| 6 | + myCollectionA.add(new Banner("r01", "www.google.com", "es")); |
| 7 | + myCollectionA.add(new Banner("r02", "caminos de ronda", "es")); |
| 8 | + myCollectionA.add(new Banner("r03", "descubrir osona", "es")); |
| 9 | + myCollectionA.add(new Banner("r03", "Osona discover", "en")); |
| 10 | + myCollectionA.add(new Banner("r03", "Descobirr osona", "ca")); |
| 11 | + myCollectionA.add(new Banner("r02", "Camins de ronda", "ca")); |
| 12 | + myCollectionA.add(new Banner("r04", "Ruta del ferro", "ca")); |
| 13 | +``` |
| 14 | + |
| 15 | +Filtrar por idioma |
| 16 | + |
| 17 | +```java |
| 18 | + final List<Banner> myCollectionB = (List<Banner>) CustomPredicate.filter(myCollectionA, |
| 19 | + new CustomPredicate.IPredicate<Banner>() { |
| 20 | + public boolean apply(Banner objectOfA) { |
| 21 | + return objectOfA.getLang().equals("es"); |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + final List<Banner> myCollectionC = (List<Banner>) CustomPredicate.filter(myCollectionA, |
| 26 | + new CustomPredicate.IPredicate<Banner>() { |
| 27 | + public boolean apply(Banner objectOfA) { |
| 28 | + return objectOfA.getLang().equals("ca"); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | +``` |
| 33 | +Obtener diferencias entre listas |
| 34 | + |
| 35 | +```java |
| 36 | + List<Banner> missingObjects = (List<Banner>) CustomPredicate.filter(myCollectionB, |
| 37 | + new CustomPredicate.IPredicate<Banner>() { |
| 38 | + public boolean apply(Banner objectOfA) { |
| 39 | + CustomPredicate.predicateParams = objectOfA.getRef(); |
| 40 | + return CustomPredicate.select(myCollectionC, new CustomPredicate.IPredicate<Banner>() { |
| 41 | + public boolean apply(Banner objectOfB) { |
| 42 | + return objectOfB.getRef().equals(CustomPredicate.predicateParams.toString()); |
| 43 | + } |
| 44 | + }) == null; |
| 45 | + } |
| 46 | + }); |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +CustomPredicate.java |
| 51 | + |
| 52 | +```java |
| 53 | +package com.webserveis.app.testlist; |
| 54 | + |
| 55 | +import java.util.ArrayList; |
| 56 | +import java.util.Collection; |
| 57 | + |
| 58 | +public class CustomPredicate<B> { |
| 59 | + public static Object predicateParams; |
| 60 | + |
| 61 | + public interface IPredicate<T> { boolean apply(T type); } |
| 62 | + |
| 63 | + public static <T> Collection<T> filter(Collection<T> target, IPredicate<T> predicate) { |
| 64 | + Collection<T> result = new ArrayList<T>(); |
| 65 | + for (T element : target) { |
| 66 | + if (predicate.apply(element)) { |
| 67 | + result.add(element); |
| 68 | + } |
| 69 | + } |
| 70 | + return result; |
| 71 | + } |
| 72 | + |
| 73 | + public static <T> T select(Collection<T> target, IPredicate<T> predicate) { |
| 74 | + T result = null; |
| 75 | + for (T element : target) { |
| 76 | + if (!predicate.apply(element)) |
| 77 | + continue; |
| 78 | + result = element; |
| 79 | + break; |
| 80 | + } |
| 81 | + return result; |
| 82 | + } |
| 83 | + |
| 84 | + public static <T> T select(Collection<T> target, IPredicate<T> predicate, T defaultValue) { |
| 85 | + T result = defaultValue; |
| 86 | + for (T element : target) { |
| 87 | + if (!predicate.apply(element)) |
| 88 | + continue; |
| 89 | + result = element; |
| 90 | + break; |
| 91 | + } |
| 92 | + return result; |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
0 commit comments