java序列防重复_检查随机序列重复[Java]

该Java程序用于检测数据文件中的随机序列记录是否存在重复。每个记录都是一个随机序列号,通过读取文件、存储序列到列表并进行比较来确保唯一性。
摘要由CSDN通过智能技术生成

呃。。。。代码编辑坏了- -! /*  * @(#)Main.java  * Author: 88250 , http://blog.csdn.net/DL88250  * Created on May 13, 2008, 4:11:44 PM  *  * This program is free software; you can redistribute it and/or modify  * it under the terms of the GNU General Public License as published by  * the Free Software Foundation; either version 3 of the License, or  * (at your option) any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  * GNU Library General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */ package checkthesame; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; /**  * Check the same record in a file.  *

 * Every record in data file is a random serial, like the followings:
 * // data file  * 1902323484354370234844
 * 1928473090393719374
 * ....
 *

 * @author 88250 , http://blog.csdn.net/DL88250  */ public class Main {     /**      * store the data records      */     public static List records = new ArrayList();     /**      * statistics      */     public static List> statistics = new ArrayList>();     /**      * Read the records from the data file which named "data.txt" into memory,      * using a java.util.ArrayList store them.      * @see #records      */     public static void readRecords() {         System.out.println("Get starting read records....");         try {             BufferedReader reader = new BufferedReader(                     new FileReader("data.txt"));             String aLine;             while ((aLine = reader.readLine()) != null) {                 records.add(aLine);             }             reader.close();         } catch (IOException ex) {             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);         }         System.out.println("The amount of records: " + records.size());     }     /**      * Create some records for test.      */     public static void createTestRecords() {         File file = new File("data.txt");         if (file.exists()) {             file.delete();         }         try {             BufferedWriter writer =                     new BufferedWriter(new FileWriter("data.txt"));             Random random = new Random();             byte[] bytes = new byte[16];             for (int i = 0; i < 1000000; i++) {                 StringBuffer aLine;                 random.nextBytes(bytes);                 aLine = new StringBuffer();                 for (int j = 0; j < 16; j++) {                     aLine.append((int) bytes[j]);                 }                 // System.out.println(aLine);                 writer.write(aLine.toString());                 //System.out.println();                 writer.newLine();             }             writer.close();         } catch (IOException ex) {             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);         }     }     /**      * Main program entry.      * @param args the command line arguments, should be null      */     public static void main(String[] args) {         createTestRecords();         readRecords();         displayRecords(10);         checkTheSame();     }     /**      * Check the same data records in {@link #records}.      */     public static void checkTheSame() {         sortTheRecords();   // sort them         // displayRecords(10);         for (int i = 0; i < records.size() - 1; i++) {             String record1 = records.get(i);             String record2 = records.get(i + 1);             if (record1.equals(record2)) {                 List equalities = new ArrayList();                 equalities.add(record1);                 equalities.add(record2);                 statistics.add(equalities);             }         }         displayStats();     }     /**      * Display the data records in console.      * @param amount display amount, start from {@link #records}'s beginning      */     public static void displayRecords(int amount) {         if (amount < 0 || amount > records.size()) {             System.out.println("The specified amount exceeds the Data records" +                     "size!");         }         System.out.println("Display: ");         for (int i = 0; i < amount; i++) {             System.out.println(records.get(i));         }         System.out.println();     }     /**      * Display the statistic results in console.      */     private static void displayStats() {         System.out.println("Statistics: ");         System.out.println("A amount of the same data records: " + statistics.                 size());         for (List aEqualities : statistics) {             System.out.println(aEqualities.get(0) + " occurs " + aEqualities.                     size());         }     }     /**      * Using {@link java.util.Collections#sort(java.util.List)} to sort the      * data records.      */     @SuppressWarnings("unchecked")     private static void sortTheRecords() {         java.util.Collections.sort(records,                 new Comparator() {                     @Override                     public int compare(Object o1,                             Object o2) {                         String r1 =                                 (String) o1;                         String r2 =                                 (String) o2;                         return r1.compareTo(r2);                     }                 });     } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值