كيفية إزالة العناصر المكررة من CSV أو أي ملف آخر في Java؟
نشرت: 2021-05-28العثور على أسطر مكررة من ملف ليس مشكلة صعبة. لكن في وقت ما في سؤال المقابلة ، يشعر الناس أحيانًا بالارتباك الشديد بشأن الطريقة التي يتعين عليهم استخدامها.
سننتقل في هذا البرنامج التعليمي إلى خطوات حول كيفية إزالة التكرارات من ملف CSV وأي ملف آخر.
هيا بنا نبدأ:
الخطوة 1.
قم بإنشاء ملف CrunchifyFindDuplicateCSV
.java
الخطوة 2.
- ضع الكود أدناه في الملف.
- نحن نستخدم BufferedReader لقراءة الملفات.
- واحدًا تلو الآخر بإضافة سطور إلى HashSet.
- تطبق فئة HashSet واجهة Set ، مدعومة بجدول تجزئة (في الواقع مثيل HashMap). لا تقدم أي ضمانات فيما يتعلق بترتيب التكرار للمجموعة ؛ على وجه الخصوص ، لا يضمن أن يظل الطلب ثابتًا بمرور الوقت. تسمح هذه الفئة بالعنصر الفارغ.
- استخدم طريقة add () للتحقق مما إذا كان الخط موجودًا بالفعل في المجموعة أم لا.
- يضيف العنصر المحدد إلى هذه المجموعة إذا لم يكن موجودًا بالفعل. بشكل أكثر رسمية ، يضيف العنصر المحدد e إلى هذه المجموعة إذا كانت هذه المجموعة لا تحتوي على عنصر e2 مثل Objects.equals (e ، e2). إذا كانت هذه المجموعة تحتوي بالفعل على العنصر ، فستترك المكالمة المجموعة دون تغيير
returns false
.
- يضيف العنصر المحدد إلى هذه المجموعة إذا لم يكن موجودًا بالفعل. بشكل أكثر رسمية ، يضيف العنصر المحدد e إلى هذه المجموعة إذا كانت هذه المجموعة لا تحتوي على عنصر e2 مثل Objects.equals (e ، e2). إذا كانت هذه المجموعة تحتوي بالفعل على العنصر ، فستترك المكالمة المجموعة دون تغيير
- بمجرد التخطي ، سنطبع هذا السطر كخط تم تخطيه.
crunchify.csv
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
name city number age kedar kapan 9843875 23 sedai ktm 97798433 23 ayush kalopul 9856324 12 dipal ratopul 9842567 34 malla setiopul 1258496 33 ayush kalopul 9856324 12 babin karki hariyopul 32589 11 raju dhading 58432 44 sedai ktm 97798433 23 Crunchify , LLC PayPal . com Google . com Twitter . com FaceBook . com Crunchify , LLC Google . com Visa . com MasterCard . com Citi . com California Austin California |
CrunchifyFindDuplicateCSV.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
package crunchify . com . tutorial ; import java . io . BufferedReader ; import java . io . FileNotFoundException ; import java . io . FileReader ; import java . io . IOException ; import java . util . HashSet ; /** * @author Crunchify.com * How to Remove Duplicate Elements from CSV file in Java? */ public class CrunchifyFindDuplicateCSV { public static void main ( String [ ] argv ) { String crunchifyCSVFile = "/Users/Shared/crunchify.csv" ; // Reads text from a character-input stream, buffering characters so as to provide for the // efficient reading of characters, arrays, and lines. BufferedReader crunchifyBufferReader = null ; String crunchifyLine = "" ; // This class implements the Set interface, backed by a hash table (actually a HashMap instance). // It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will // remain constant over time. This class permits the null element. HashSet < String > crunchifyAllLines = new HashSet < > ( ) ; try { crunchifyBufferReader = new BufferedReader ( new FileReader ( crunchifyCSVFile ) ) ; while ( ( crunchifyLine = crunchifyBufferReader . readLine ( ) ) ! = null ) { if ( crunchifyAllLines . add ( crunchifyLine ) ) { crunchifyLog ( "Processed line: " + crunchifyLine ) ; } else if ( ! crunchifyIsNullOrEmpty ( crunchifyLine ) ) { crunchifyLog ( "--------------- Skipped line: " + crunchifyLine ) ; } } } catch ( FileNotFoundException e ) { e . printStackTrace ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } finally { if ( crunchifyBufferReader ! = null ) { try { crunchifyBufferReader . close ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } } } // Check if String with spaces is Empty or Null public static boolean crunchifyIsNullOrEmpty ( String crunchifyString ) { if ( crunchifyString ! = null && !crunchifyString.trim().isEmpty()) return false; return true ; } // Simple method for system outs private static void crunchifyLog ( String s ) { System . out . println ( s ) ; } } // Linux command to remove duplicate lines from file: // $ sort -u /Users/Shared/crunchify.csv |

لنقم بتشغيل برنامج Java في IntelliJ IDE.
هذه نتيجة:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Processed line : name city number age Processed line : kedar kapan 9843875 23 Processed line : sedai ktm 97798433 23 Processed line : ayush kalopul 9856324 12 Processed line : dipal ratopul 9842567 34 Processed line : malla setiopul 1258496 33 --------------- Skipped line : ayush kalopul 9856324 12 Processed line : babin karki hariyopul 32589 11 Processed line : raju dhading 58432 44 --------------- Skipped line : sedai ktm 97798433 23 Processed line : Processed line : Crunchify , LLC Processed line : PayPal . com Processed line : Google . com Processed line : Twitter . com Processed line : FaceBook . com --------------- Skipped line : Crunchify , LLC --------------- Skipped line : Google . com Processed line : Visa . com Processed line : MasterCard . com Processed line : Citi . com Processed line : California Processed line : Austin --------------- Skipped line : California Process finished with exit code 0 |
آمل أن تجد برنامج Java هذا مفيدًا في العثور على سطور مكررة في CSV أو أي ملف آخر.
كيف تجد الأسطر المكررة في CSV باستخدام أمر Linux؟
1 |
$ sort - u / Users / Shared / crunchify . csv |
نتيجة: