CollectionUtil.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package com.jd.platform.jlog.common.utils;
  2. import java.util.*;
  3. /**
  4. * @author tangbohu
  5. * @version 1.0.0
  6. * @ClassName CollectionUtil.java
  7. * @Description TODO
  8. * @createTime 2022年02月21日 17:14:00
  9. */
  10. public class CollectionUtil {
  11. public static boolean isEmpty(Collection<?> collection) {
  12. return collection == null || collection.isEmpty();
  13. }
  14. public static boolean isNotEmpty(Collection<?> collection) {
  15. return collection != null && collection.isEmpty();
  16. }
  17. public static boolean isEmpty(Map<?, ?> map) {
  18. return !isNotEmpty(map);
  19. }
  20. public static boolean isNotEmpty(Map<?, ?> map) {
  21. return map != null && !map.isEmpty();
  22. }
  23. public static <T> boolean equals(List<T> c1, List<T> c2) {
  24. if (c1 == null && c2 == null) {
  25. return true;
  26. } else if (c1 != null && c2 != null) {
  27. if (c1.size() != c2.size()) {
  28. return false;
  29. } else {
  30. for(int i = 0; i < c1.size(); ++i) {
  31. if (!Objects.equals(c1.get(i), c2.get(i))) {
  32. return false;
  33. }
  34. }
  35. return true;
  36. }
  37. } else {
  38. return false;
  39. }
  40. }
  41. public static <K, V> boolean equals(Map<K, V> c1, Map<K, V> c2) {
  42. if (c1 == null && c2 == null) {
  43. return true;
  44. } else if (c1 != null && c2 != null) {
  45. if (c1.size() != c2.size()) {
  46. return false;
  47. } else {
  48. Iterator var2 = c1.entrySet().iterator();
  49. Object v1;
  50. Object v2;
  51. do {
  52. if (!var2.hasNext()) {
  53. return true;
  54. }
  55. Map.Entry<K, V> entry = (Map.Entry)var2.next();
  56. K k = entry.getKey();
  57. v1 = entry.getValue();
  58. v2 = c2.get(k);
  59. } while(Objects.equals(v1, v2));
  60. return false;
  61. }
  62. } else {
  63. return false;
  64. }
  65. }
  66. public static <K, V> Set<String> diffKeys(Map<K, V> m1, Map<K, V> m2){
  67. Set<String> diff = new HashSet<>(1);
  68. for (Map.Entry<K,V> kvEntry : m1.entrySet()) {
  69. V val = m2.get(kvEntry.getKey());
  70. if(!kvEntry.getValue().equals(val)){
  71. diff.add(kvEntry.getKey().toString());
  72. }
  73. }
  74. for (Map.Entry<K,V> kvEntry : m2.entrySet()) {
  75. V val = m1.get(kvEntry.getKey());
  76. if(!kvEntry.getValue().equals(val)){
  77. diff.add(kvEntry.getKey().toString());
  78. }
  79. }
  80. return diff;
  81. }
  82. public static <K, V> HashMap<K,V> diffMap(Map<K, V> m1, Map<K, V> m2){
  83. HashMap<K, V> diff = new HashMap<>(1);
  84. for (Map.Entry<K,V> kvEntry : m1.entrySet()) {
  85. V val = m2.get(kvEntry.getKey());
  86. if(!kvEntry.getValue().equals(val)){
  87. diff.put(kvEntry.getKey(), kvEntry.getValue());
  88. }
  89. }
  90. for (Map.Entry<K,V> kvEntry : m2.entrySet()) {
  91. V val = m1.get(kvEntry.getKey());
  92. if(!kvEntry.getValue().equals(val)){
  93. diff.put(kvEntry.getKey(), kvEntry.getValue());
  94. }
  95. }
  96. return diff;
  97. }
  98. public static void main(String[] args) {
  99. int fail = 0;
  100. int count = 10000;
  101. Set<Integer> set = new HashSet<>();
  102. for (int i = 0; i < count; i++) {
  103. int h = randomMac4Qemu().hashCode();
  104. int val = indexFor(h);
  105. boolean r = set.add(val);
  106. // System.out.println(val);
  107. if(!r){
  108. fail++;
  109. }
  110. }
  111. int fail2 = 0;
  112. Set<Integer> set2 = new HashSet<>();
  113. for (int i = 0; i < count; i++) {
  114. int h = randomMac4Qemu().hashCode();
  115. Random rd = new Random(h);
  116. int val = rd.nextInt(16385);
  117. boolean r = set.add(val);
  118. System.out.println(val);
  119. if(!r){
  120. fail2++;
  121. }
  122. }
  123. System.out.println("11--》 "+fail);
  124. System.out.println("22-》 "+ fail2);
  125. }
  126. static int indexFor(int h) {
  127. return h & (16384-1);
  128. }
  129. public static String randomMac4Qemu() {
  130. /* if(1==1){
  131. return getRandomIp();
  132. }*/
  133. Random random = new Random();
  134. String[] mac = {
  135. /* String.format("%02x", 0x52),
  136. String.format("%02x", 0x54),
  137. String.format("%02x", 0x00),*/
  138. String.format("%02x", random.nextInt(0xff)),
  139. String.format("%02x", random.nextInt(0xff)),
  140. String.format("%02x", random.nextInt(0xff))
  141. };
  142. return String.join(":", mac);
  143. }
  144. public static String getRandomIp(){
  145. //ip范围
  146. int[][] range = {{607649792,608174079},//36.56.0.0-36.63.255.255
  147. {1038614528,1039007743},//61.232.0.0-61.237.255.255
  148. {1783627776,1784676351},//106.80.0.0-106.95.255.255
  149. {2035023872,2035154943},//121.76.0.0-121.77.255.255
  150. {2078801920,2079064063},//123.232.0.0-123.235.255.255
  151. {-1950089216,-1948778497},//139.196.0.0-139.215.255.255
  152. {-1425539072,-1425014785},//171.8.0.0-171.15.255.255
  153. {-1236271104,-1235419137},//182.80.0.0-182.92.255.255
  154. {-770113536,-768606209},//210.25.0.0-210.47.255.255
  155. {-569376768,-564133889}, //222.16.0.0-222.95.255.255
  156. };
  157. Random rdint = new Random();
  158. int index = rdint.nextInt(10);
  159. return num2ip(range[index][0]+new Random().nextInt(range[index][1]-range[index][0]));
  160. }
  161. public static String num2ip(int ip) {
  162. int [] b=new int[4] ;
  163. String x = "";
  164. b[0] = (int)((ip >> 24) & 0xff);
  165. b[1] = (int)((ip >> 16) & 0xff);
  166. b[2] = (int)((ip >> 8) & 0xff);
  167. b[3] = (int)(ip & 0xff);
  168. x=Integer.toString(b[0])+"."+Integer.toString(b[1])+"."+Integer.toString(b[2])+"."+Integer.toString(b[3]);
  169. return x;
  170. }
  171. }