Create a string array length is 200
using random characters length is 2 to fill the string array
Statistics string array duplicate strings how many
use HashSet to solve this problem
1 package Collection; 2 3 import java.util.HashSet; 4 5 public class HashSetTest { 6 public static char randChar() { // 创建随机字符 7 while (true) { 8 char c = (char) (Math.round(Math.random() * 126)); 9 if (Character.isDigit(c) || Character.isLetter(c)) { 10 return c; 11 } 12 } 13 } 14 15 public static void print(String arr[]) { // 控制打印输出 16 for (int i = 0; i < arr.length; i++) { 17 if ((i + 1) % 20 == 0) { 18 System.out.printf(arr[i] + "\n"); 19 } else { 20 System.out.printf(arr[i] + "\t"); 21 } 22 } 23 } 24 25 public static void main(String[] args) { 26 // Create random string array 27 String ARR [] = new new String [200 is ]; 28 for ( int I = 0; I <arr.length; I ++ ) { 29 ARR [I] = "" + randChar () + randChar (); 30 } 31 is Print (ARR); // formatted print output 32 HashSet <string> SET = new new HashSet <> (); // used to determine which string is repeated 33 is HashSet <string> = RES new new HashSet <> (); // used to store duplicate strings 34 is for (string S: ARR) { 35 IF (set.add (S) == to false ) 36 res.add (S); 37 [ } 38 is System.out.printf ( "% d total one type of a repeating character string" , res.size ()); 39 the System. out.println ( "are: \ n-" + RES); 40 } 41 is }
Renderings: