通过码流串,格式为key,len,val。提供key,查找val的算法。
1 import java.util.ArrayList; 2 import java.util.HashMap; 3 import java.util.UUID; 4 5 public class HuaweiTest { 6 7 public static void main(String[] args) { 8 // System.out.println("Hello World!"); 9 String tag = "31"; 10 String codeStreamStr = "31 01 32"; 11 String codeStringVal = getCodeStringVal(tag, codeStreamStr); 12 System.out.println(codeStringVal); 13 } 14 15 16 public static String getCodeStringVal(String tag, String codeStreamStr) { 17 String trim = tag.trim(); 18 if (trim == null) { 19 return null; 20 } 21 if (trim.isEmpty()) { 22 return null; 23 } 24 if ((codeStreamStr.length() * 4) > 50000) { 25 return null; 26 } 27 String trim1 = codeStreamStr.trim(); 28 if (trim1 == null) { 29 return null; 30 } 31 if (trim1.isEmpty()) { 32 return null; 33 } 34 String[] split = trim1.split("\\s"); 35 if (!(split.length % 3 == 0)) { 36 return null; 37 } 38 for (int i = 0; i < split.length; i++) { 39 String s = split[i]; 40 if (s.length() > 2) { 41 return null; 42 } 43 String trim2 = s.trim(); 44 for (int j = 0; j < trim2.length(); j++) { 45 char c = trim2.charAt(j); 46 if (Character.isDigit(c)) { 47 continue; 48 } else { 49 if (Character.isLowerCase(c)) { 50 return null; 51 } 52 if (!(c >= 'A' && c <= 'F')) { 53 return null; 54 } 55 } 56 } 57 } 58 String[] split2 = codeStreamStr.split("\\s"); 59 HashMap<String, Integer> stringIntegerHashMap = new HashMap<>(); 60 for (int i = 0; i < split2.length; i++) { 61 String s = split2[i]; 62 String trim2 = s.trim(); 63 if (stringIntegerHashMap.containsKey(trim2)) { 64 stringIntegerHashMap.put(trim2, stringIntegerHashMap.get(trim2) + 1); 65 } else { 66 stringIntegerHashMap.put(trim2, 1); 67 } 68 } 69 System.out.println(stringIntegerHashMap); 70 ArrayList<CodeStream> codeStreamArrayList = new ArrayList<>(); 71 String[] split1 = codeStreamStr.split("\\s"); 72 int count = 0; 73 int codeTagIndex = 0; 74 for (int i = 0; i < split1.length; i++) { 75 if ((count + 1) % 3 == 0) { 76 continue; 77 } else { 78 CodeStream codeStream = new CodeStream(); 79 codeStream.setId(UUID.randomUUID().toString()); 80 String s = split1[codeTagIndex]; 81 if (extracted(s, stringIntegerHashMap)) return null; 82 codeStream.setTag(s); 83 String s1 = getString(split1, codeTagIndex); 84 if (s1 == null) return null; 85 codeStream.setLength(Integer.valueOf(s1)); 86 String s2 = split1[codeTagIndex + 2]; 87 codeStream.setValue(s2); 88 codeStreamArrayList.add(codeStream); 89 count += 3; 90 codeTagIndex = count; 91 } 92 } 93 System.out.println(codeStreamArrayList); 94 ArrayList<String> stringArrayList = new ArrayList<>(); 95 codeStreamArrayList.forEach(e -> { 96 String tag1 = e.getTag(); 97 if (tag.equals(tag1)) { 98 String value = e.getValue(); 99 stringArrayList.add(value); 100 } 101 }); 102 System.out.println(stringArrayList); 103 StringBuilder stringBuilder = new StringBuilder(); 104 stringArrayList.forEach(e -> { 105 stringBuilder.append(e); 106 stringBuilder.append("\\s"); 107 }); 108 return stringBuilder.toString(); 109 } 110 111 private static String getString(String[] split1, int codeTagIndex) { 112 String s1 = split1[codeTagIndex + 1]; 113 byte[] bytes1 = s1.getBytes(); 114 // if (bytes1.length>2){ 115 // return null; 116 // } 117 return s1; 118 } 119 120 private static boolean extracted(String s, HashMap<String, Integer> stringIntegerHashMap) { 121 byte[] bytes = s.getBytes(); 122 int length = bytes.length; 123 // if (length>1){ 124 // return true; 125 // } 126 if (stringIntegerHashMap.containsKey(s)) { 127 if (stringIntegerHashMap.get(s) >= 2) { 128 return true; 129 } 130 } 131 return false; 132 } 133 } 134 135 class CodeStream { 136 private String id; 137 private String tag; 138 private Integer length; 139 private String value; 140 141 public String getId() { 142 return id; 143 } 144 145 public void setId(String id) { 146 this.id = id; 147 } 148 149 public String getTag() { 150 return tag; 151 } 152 153 public void setTag(String tag) { 154 this.tag = tag; 155 } 156 157 public Integer getLength() { 158 return length; 159 } 160 161 public void setLength(Integer length) { 162 this.length = length; 163 } 164 165 public String getValue() { 166 return value; 167 } 168 169 public void setValue(String value) { 170 this.value = value; 171 } 172 173 @Override 174 public String toString() { 175 return "CodeStream{" + 176 "id='" + id + '\'' + 177 ", tag='" + tag + '\'' + 178 ", length=" + length + 179 ", value='" + value + '\'' + 180 '}'; 181 } 182 }