|
| 1 | +package com.baeldung.array; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +public class SearchArrayTest { |
| 8 | + |
| 9 | + |
| 10 | + @Test |
| 11 | + public void searchArrayAllocNewCollections() { |
| 12 | + |
| 13 | + int count = 1000; |
| 14 | + |
| 15 | + String[] array = seedArray(count); |
| 16 | + |
| 17 | + long startTime = System.nanoTime(); |
| 18 | + for (int i = 0; i < count; i++) { |
| 19 | + searchList(array, "A"); |
| 20 | + } |
| 21 | + long duration = System.nanoTime() - startTime; |
| 22 | + System.out.println("SearchList: " + duration / 10000); |
| 23 | + |
| 24 | + startTime = System.nanoTime(); |
| 25 | + for (int i = 0; i < count; i++) { |
| 26 | + searchSet(array,"A"); |
| 27 | + } |
| 28 | + duration = System.nanoTime() - startTime; |
| 29 | + System.out.println("SearchSet: " + duration / 10000); |
| 30 | + |
| 31 | + startTime = System.nanoTime(); |
| 32 | + for (int i = 0; i < count; i++) { |
| 33 | + searchLoop(array, "A"); |
| 34 | + } |
| 35 | + duration = System.nanoTime() - startTime; |
| 36 | + System.out.println("SearchLoop: " + duration / 10000); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void searchArrayReuseCollections() { |
| 41 | + |
| 42 | + int count = 10000; |
| 43 | + String[] array = seedArray(count); |
| 44 | + |
| 45 | + List<String> asList = Arrays.asList(array); |
| 46 | + Set<String> asSet = new HashSet<>(Arrays.asList(array)); |
| 47 | + |
| 48 | + long startTime = System.nanoTime(); |
| 49 | + for (int i = 0; i < count; i++) { |
| 50 | + asList.contains("A"); |
| 51 | + } |
| 52 | + long duration = System.nanoTime() - startTime; |
| 53 | + System.out.println("List: " + duration / 10000); |
| 54 | + |
| 55 | + startTime = System.nanoTime(); |
| 56 | + for (int i = 0; i < count; i++) { |
| 57 | + asSet.contains("A"); |
| 58 | + } |
| 59 | + duration = System.nanoTime() - startTime; |
| 60 | + System.out.println("Set: " + duration / 10000); |
| 61 | + |
| 62 | + startTime = System.nanoTime(); |
| 63 | + for (int i = 0; i < count; i++) { |
| 64 | + searchLoop(array, "A"); |
| 65 | + } |
| 66 | + duration = System.nanoTime() - startTime; |
| 67 | + System.out.println("Loop: " + duration / 10000); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + @Test |
| 73 | + public void searchArrayBinarySearch() { |
| 74 | + |
| 75 | + int count = 10000; |
| 76 | + String[] array = seedArray(count); |
| 77 | + Arrays.sort(array); |
| 78 | + |
| 79 | + long startTime = System.nanoTime(); |
| 80 | + for (int i = 0; i < count; i++) { |
| 81 | + Arrays.binarySearch(array, "A"); |
| 82 | + } |
| 83 | + long duration = System.nanoTime() - startTime; |
| 84 | + System.out.println("Binary search: " + duration / 10000); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + private boolean searchList(String[] arr, String targetValue) { |
| 89 | + return Arrays.asList(arr).contains(targetValue); |
| 90 | + } |
| 91 | + |
| 92 | + private boolean searchSet(String[] arr, String targetValue) { |
| 93 | + Set<String> set = new HashSet<>(Arrays.asList(arr)); |
| 94 | + return set.contains(targetValue); |
| 95 | + } |
| 96 | + |
| 97 | + private boolean searchLoop(String[] arr, String targetValue) { |
| 98 | + for (String s : arr) { |
| 99 | + if (s.equals(targetValue)) |
| 100 | + return true; |
| 101 | + } |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
| 105 | + private String[] seedArray(int length) { |
| 106 | + |
| 107 | + String[] strings = new String[length]; |
| 108 | + Random random = new Random(); |
| 109 | + for (int i = 0; i < length; i++) |
| 110 | + { |
| 111 | + strings[i] = String.valueOf(random.nextInt()); |
| 112 | + } |
| 113 | + return strings; |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments