1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | package com.blogspot.howdoidothatinjava.tutorials; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; import com.blogspot.howdoidothatinjava.sandbox.TestObject; public class ObjectTest { private final static String under = "*" ; public static void main(String[] args) { List<TestObject> objList = TestObject.generateTestObjectList(); println( "Objects in the list:" , under); println(); for (TestObject t: objList){ t.displayObject(); } println(); //Ex. 1 - Hardcoded Result println( "Ex. 1: Hardcoded Result" , under); println(); println( "TestObject with a > 20 (hardcoded)" ); println(); printObjectsWithAGreaterThanTwenty(objList); println(); //Ex. 2 - Parameterized Range println( "Ex. 2 - Parameterized Range" , under); println(); println( "TestObject with b between 10 and 70 inclusive" ); println(); printObjectsWithBInRange(objList, 10 , 70 , Inclusivity.BOTH); println(); //Ex. 3 - Use of Local Class println( "Ex. 3 - Use of Local Class" , under); println(); println( "TestObject with starting with 'S' and a*3 <= b" ); println(); class TestObjectForNameAndRange implements TestObjectValidator { public boolean test(TestObject t) { return t.getName().startsWith( "S" ) && t.getA() * 3 <= t.getB(); } } printTestObjects(objList, new TestObjectForNameAndRange()); println(); //Ex. 4 - Use of Anonymous Class println( "Ex. 4 - Use of Anonymous Class" , under); println(); println( "TestObject with starting with 'S' and a*3 <= b" ); println(); printTestObjects(objList, new TestObjectValidator() { public boolean test(TestObject t) { return t.getName().startsWith( "S" ) && t.getA() * 3 <= t.getB(); } }); println(); //Ex. 5 - Use of Lambda println( "Ex. 5 - Use of Lambda" , under); println(); println( "TestObject with starting with 'S' and a*3 <= b" ); println(); printTestObjects(objList, (TestObject t)->t.getName().startsWith( "S" ) && t.getA() * 3 <= t.getB()); println(); //Ex. 6 - Use of Predicate in place of Lambda println( "Ex. 6 - Use of Predicate in place of Lambda" , under); println(); println( "TestObject with name containing 's', case insensitive" ); println(); printTestObjectsWithPredicate(objList, t -> t.getName().toLowerCase().contains( "s" )); println(); //Ex. 7 - Use of Predicate and Consumer Lambda println( "Ex. 7 - Use of Predicate and Consumer Lambda" , under); println(); println( "TestObject with name ending in 'y', case insensitive" ); println(); printTestObjectWithPredicateAndConsumer(objList, t -> t.getName().toLowerCase().endsWith( "y" ), t -> t.displayObject()); println(); //Ex. 8 - Use of Predicate, Function and Consumer println( "Ex. 8 - Use of Predicate, Function and Consumer" , under); println(); println( "TestObject name where a >= b" ); println(); printTestObjectWithPredicateFunctionAndConsumer(objList, t->t.getA() >= t.getB(), t -> t.getName(), name -> println(name)); println(); //Ex. 9 - Use of Generics To Add Further Flexibility println( "Ex. 9 - Use of Generics To Add Further Flexibility" , under); println(); println( "TestObject name where a < b" ); println(); processObjects(objList, t -> t.getA() < t.getB(), t -> t.getName(), name -> println( "TestOblject (" + name + ")" )); println(); //Ex. 10 - Use of Generics to Process Objects and Return Result Collection println( "Ex. 10 - Use of Generics to Process Objects and Return Result Collection" , under); println(); println( "TestObject count of names matched where a < b" ); println(); List<String> results = processObjects(objList, t -> t.getA() < t.getB(), t -> t.getName()); println( "The number of results in List returned was " + results.size()); println(); //Ex. 11 - Use of Bulk Data Methods to Repeat Results in Ex. 10 println( "Ex. 11 - Use of Bulk Data Methods to Repeat Results in Ex. 10" , under); println(); println( "TestObject count of names matched where a < b" ); println(); results.clear(); results = objList.stream().filter(t -> t.getA() < t.getB()).map(t -> t.getName()).collect(Collectors.toList()); println( "The number of results in List returned was " + results.size()); println(); //Ex. 12 - Use of Bulk Data Method Without Temporary Collection println( "Ex. 12 - Use of Bulk Data Method Without Temporary Collection" , under); println(); println( "TestObject count of predicate results to achieve same result as in Ex. 10 & Ex. 11" ); println(); println( "The number of results found in filter was: " + objList.stream().filter(t-> t.getA() < t.getB()).count()); println(); //Rx. 13 - Use of Named Predicates as Variables println( "Rx. 13 - Use of Named Predicates as Variables" , under); println(); println( "Use of named Predicate to get range where b > 10 and b < 35" ); println(); Predicate<TestObject> p1 = t -> t.getB() > 10 && t.getB() < 35 ; processGenericObjects(objList, p1, t -> t.displayObject()); println(); println( "Use of second named predicate to find names containing 'er'" ); println(); Predicate<TestObject> p2 = t -> t.getName().toLowerCase().contains( "er" ); processGenericObjects(objList, p2, t -> println( "TestObject (" + t.getName() + ")" )); println(); //Ex. 14 - Use of Named Consumer to Customize Print Formatting println( "Ex. 14 - Use of Named Consumer to Customize Print Formatting" , under); println(); println( "Print TestObjects where name contains 'a' using named Consumer using TestObject's displayObject()" ); println(); Consumer<TestObject> printStandard = t -> t.displayObject(); processGenericObjects(objList, t -> t.getName().toLowerCase().contains( "a" ), printStandard); println(); println( "Print TestObjects where name contains 'a' using named Consumer using different print function" ); println(); Consumer<TestObject> otherPrintFormat = t -> displayOtherFormat(t); processGenericObjects(objList, t -> t.getName().toLowerCase().contains( "a" ), otherPrintFormat); } private static void printObjectsWithAGreaterThanTwenty(List<TestObject> list) { // get each element in the colection and display it based on criteria for (TestObject t : list){ if (t.getA() > 20 ) t.displayObject(); } } private static void printObjectsWithBInRange(List<TestObject> list, int low, int high, Inclusivity rule) { for (TestObject t : list){ switch (rule){ case BOTH: if (t.getB() >= low && t.getB() <= high) t.displayObject(); break ; case END: if (t.getB() > low && t.getB() <= high) t.displayObject(); break ; case START: if (t.getB() >= low && t.getB() < high) t.displayObject(); break ; case NONE: if (t.getB() > low && t.getB() < high) t.displayObject(); } } } private static void printTestObjects(List<TestObject> list, TestObjectValidator validator){ for (TestObject t : list){ if (validator.test(t)) t.displayObject(); } } private static void printTestObjectsWithPredicate(List<TestObject> list, Predicate<TestObject> filter){ for (TestObject t : list){ if (filter.test(t)) t.displayObject(); } } private static void printTestObjectWithPredicateAndConsumer(List<TestObject> list, Predicate<TestObject> filter, Consumer<TestObject> voidFunction) { for (TestObject t : list){ if (filter.test(t)) voidFunction.accept(t); } } private static void printTestObjectWithPredicateFunctionAndConsumer(List<TestObject> list, Predicate<TestObject> filter, Function<TestObject,String> mapper, Consumer<String> voidFunction){ for (TestObject t : list){ if (filter.test(t)) { String out = mapper.apply(t); voidFunction.accept(out); } } } private static <T, U> void processObjects(Iterable<T> collection, Predicate<T> filter, Function<T, U> mapper, Consumer<U> voidFunction){ for (T t : collection){ if (filter.test(t)){ U u = mapper.apply(t); voidFunction.accept(u); } } } private static <T, U> List<U> processObjects(Iterable<T> collection, Predicate<T> filter, Function<T,U> mapper){ List<U> uList = new ArrayList<U>(); for (T t : collection){ if (filter.test(t)){ uList.add(mapper.apply(t)); } } return uList; } private static <T> void processGenericObjects(Collection<T> collection, Predicate<T> criteria, Consumer<T> voidFunction){ collection.stream().filter(criteria).forEach(voidFunction); } private static void displayOtherFormat(TestObject t){ println( "\tTestObject (" + t.getName() + ")" ); println( "\t\ta: " + t.getA()); println( "\t\tb: " + t.getB()); println(); } // Functional Interfaces interface TestObjectValidator { boolean test(TestObject t); } // Methods out of pure laziness private static void println(){ println( null ); } private static void println(String out){ println(out, null ); } private static void println(String out, String under){ if ( null == out) { System.out.println(); } else { System.out.println(out); if ( null != under && !under.isEmpty()){ StringBuffer sb = new StringBuffer(); while (out.length() > 0 ){ sb.append(under); out = out.substring( 1 ); } System.out.println(sb.toString()); } } } private enum Inclusivity { START, END, BOTH, NONE } } |
Tips and tricks to implement various Java technologies and frameworks in one easy to find location.
ObjectTest.java
Subscribe to:
Posts
(
Atom
)
No comments :
Post a Comment
All comments are moderated. NO ADS. NO SPAM.