Advertisement
spkenny

StringUniquenessChecker

Apr 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.HashSet;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Set;
  6. import java.util.function.Function;
  7.  
  8. /**
  9.  *
  10.  * The class purpose is to optimize memory usage checking for duplicates in a
  11.  * list or stream of strings
  12.  *
  13.  */
  14. public class StringUniquenessChecker {
  15.  
  16.     private Map<Integer, Map<String, Integer>> stringUsage = new HashMap<>();
  17.     private Set<Integer> hashCodes = new HashSet<>();
  18.    
  19.     public void doAction1(List<String> list, Function<String,Void> func) {
  20.         for (int i = 0; i< list.size(); i++) {
  21.             String string = list.get(i);
  22.             int hashCode = string.hashCode();
  23.  
  24.             if (!hashCodes.contains(hashCode)) {
  25.                 stringUsage.put(hashCode, null);
  26.                 func.apply(string);
  27.             } else {
  28.                 if (!stringUsage.containsKey(hashCode)) {
  29.                     Map<String, Integer> stringCountMap = new HashMap<>();
  30.                     stringCountMap.put(string, 0);
  31.                     stringUsage.putIfAbsent(hashCode, stringCountMap);
  32.                     func.apply(string);
  33.                 } else {
  34.                     Map<String, Integer> stringCountMap = stringUsage.get(hashCode);
  35.                     if (stringCountMap != null) {
  36.                         Integer count = stringCountMap.get(string);
  37.                         if (count == null) {
  38.                             stringCountMap.put(string, 1);
  39.                             func.apply(string);
  40.                         } else {
  41.                             stringCountMap.put(string, count + 1);
  42.                             func.apply(string);
  43.                         }
  44.                         stringUsage.put(hashCode, stringCountMap);
  45.                        
  46.                     } else {
  47.                         stringCountMap = new HashMap<>();
  48.                         stringCountMap.put(string, 1);
  49.                         stringUsage.put(hashCode, stringCountMap);
  50.                        
  51.                     }
  52.  
  53.                 }
  54.             }
  55.  
  56.             hashCodes.add(hashCode);
  57.         }
  58.  
  59.     }
  60.  
  61.     public void doAction(Function<String,Void> func) {
  62.  
  63.         for (int i = 0; i< 1000; i++) {
  64.             String string = StringProvider.provide();
  65.             int hashCode = string.hashCode();
  66.  
  67.             if (!hashCodes.contains(hashCode)) {
  68.                 stringUsage.put(hashCode, null);
  69.             } else {
  70.                 if (!stringUsage.containsKey(hashCode)) {
  71.                     Map<String, Integer> stringCountMap = new HashMap<>();
  72.                     stringCountMap.put(string, 0);
  73.                     stringUsage.putIfAbsent(hashCode, stringCountMap);
  74.                 } else {
  75.                     Map<String, Integer> stringCountMap = stringUsage.get(hashCode);
  76.                     if (stringCountMap != null) {
  77.                         Integer count = stringCountMap.get(string);
  78.                         if (count == null) {
  79.                             stringCountMap.put(string, 1);
  80.                         } else {
  81.                             stringCountMap.put(string, count + 1);
  82.                         }
  83.                         stringUsage.put(hashCode, stringCountMap);
  84.                     } else {
  85.                         stringCountMap = new HashMap<>();
  86.                         stringCountMap.put(string, 1);
  87.                         stringUsage.put(hashCode, stringCountMap);
  88.                     }
  89.  
  90.                 }
  91.             }
  92.  
  93.             hashCodes.add(hashCode);
  94.         }
  95.  
  96.         for (int i = 0; i< 1000; i++) {
  97.             String string = StringProvider.provide();
  98.             int hashCode = string.hashCode();
  99.  
  100.             Map<String, Integer> stringCountMap = stringUsage.get(hashCode);
  101.             if (stringCountMap == null) {
  102.                 func.apply(string);
  103.             } else {
  104.  
  105.                 Integer countOfString = stringCountMap.get(string);
  106.                 if (countOfString == null || countOfString == 1) {
  107.                     func.apply(string);
  108.                 }
  109.                 if (countOfString != null) {
  110.                     stringCountMap.put(string, countOfString - 1);
  111.                 }
  112.             }
  113.         }
  114.  
  115.     }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement