Er .... The Code has been edited --!
/*
* @ (#) Main. Java
* Author: 88250 <DL88250@gmail.com>, http://blog.csdn.net/DL88250
* Created on May 13,200 8, 4:11:44
*
* This program is free software; you can redistribute it and/or modify
* It under the terms of the GNU General Public License as published
* 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
* Merchantability or fitness for a special purpose. See
* GNU Library General Public License for more details.
*
* You shoshould have written ed 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.
* <P>
* Every record in data file is a random serial, like the followings: <br>
* // Data file
* 1902323484354370234844 <br>
* 1928473090393719374 <br>
*... <Br>
* </P>
* @ Author 88250 <DL88250@gmail.com>, http://blog.csdn.net/DL88250
*/
Public class main {
/**
* Store the data records
*/
Public static list <string> records = new arraylist <string> ();
/**
* Statistics
*/
Public static list <string> Statistics = new arraylist <list <string> ();
/**
* Read the records from the data file which named "data.txt" into memory,
* Using a <code> JAVA. util. arraylist </code> 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, shocould be <code> null </code>
*/
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 <string> equalities = new arraylist <string> ();
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 <string> aequalities: Statistics ){
System. Out. println (aequalities. Get (0) + "occurs" + aequalities.
Size ());
}
}
/**
* Using {@ link java. util. Collections # Sort (Java. util. List)} to sort
* 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 );
}
});
}
}