| 37 | } |
| 38 | |
| 39 | public class TypesForSets { |
| 40 | static <T> void |
| 41 | fill(Set<T> set, Function<Integer, T> type) { |
| 42 | for(int i = 10; i >= 5; i--) // Descending |
| 43 | set.add(type.apply(i)); |
| 44 | for(int i = 0; i < 5; i++) // Ascending |
| 45 | set.add(type.apply(i)); |
| 46 | } |
| 47 | static <T> void |
| 48 | test(Set<T> set, Function<Integer, T> type) { |
| 49 | fill(set, type); |
| 50 | fill(set, type); // Try to add duplicates |
| 51 | fill(set, type); |
| 52 | System.out.println(set); |
| 53 | } |
| 54 | public static void main(String[] args) { |
| 55 | test(new HashSet<>(), HashType::new); |
| 56 | test(new LinkedHashSet<>(), HashType::new); |
| 57 | test(new TreeSet<>(), TreeType::new); |
| 58 | // Things that don't work: |
| 59 | test(new HashSet<>(), SetType::new); |
| 60 | test(new HashSet<>(), TreeType::new); |
| 61 | test(new LinkedHashSet<>(), SetType::new); |
| 62 | test(new LinkedHashSet<>(), TreeType::new); |
| 63 | try { |
| 64 | test(new TreeSet<>(), SetType::new); |
| 65 | } catch(Exception e) { |
| 66 | System.out.println(e.getMessage()); |
| 67 | } |
| 68 | try { |
| 69 | test(new TreeSet<>(), HashType::new); |
| 70 | } catch(Exception e) { |
| 71 | System.out.println(e.getMessage()); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | /* Output: |
| 76 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 77 | [10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4] |
nothing calls this directly
no outgoing calls
no test coverage detected