List of Topics:
Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Find Common Terms between Two Files in Java?

Find Common Terms in Java

Condition for Finding Common Terms between Two Files in Java

  • Description: To find common terms between two files in Java, the content of both files can be read line by line using classes like BufferedReader or Scanner. Each line is split into individual terms (e.g., words) using methods such as `split()` or regular expressions. These terms are then stored in data structures like `HashSet` to ensure uniqueness and facilitate efficient comparison. After reading and storing the terms from both files, the intersection of the two sets can be determined using the `retainAll()` method of the `HashSet` class. This will leave only the common terms between the two sets. Alternatively, Java streams can be used to filter the common elements. The common terms can be displayed in the UI or stored as needed.
Sample Source Code
  • # CommonTerms.java
    package JavaSamples;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;

    public class CommonTerms extends javax.swing.JFrame {

    public CommonTerms() {
    initComponents();
    }

    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    jTextField2 = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jButton3 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("Common Terms Finder ");

    jLabel2.setText("File Path :");

    jLabel3.setText("File Path :");

    jButton1.setText("Choose File");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    jButton2.setText("Choose File");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton2ActionPerformed(evt);
    }
    });

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jScrollPane1.setViewportView(jTextArea1);

    jButton3.setText("Common Terms");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton3ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(99, 99, 99)
    .addComponent(jLabel1))
    .addGroup(layout.createSequentialGroup()
    .addGap(132, 132, 132)
    .addComponent(jButton1))
    .addGroup(layout.createSequentialGroup()
    .addGap(30, 30, 30)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
    .addComponent(jLabel3)
    .addComponent(jLabel2))
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
    .addComponent(jTextField2)))
    .addGroup(layout.createSequentialGroup()
    .addGap(65, 65, 65)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(layout.createSequentialGroup()
    .addGap(130, 130, 130)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(jButton3)
    .addComponent(jButton2)))
    .addContainerGap(46, Short.MAX_VALUE))
Screenshots
  • STEP 1: The user selects a text document from local files.
  • The user selects a text document from local files.

  • STEP 2: To click the choose file button, it navigates the local device to choose a file.
  • To click the choose file button, it navigates the local device to choose a file.

  • STEP 3: The file path will be displayed in the text field.
  • The file path will be displayed in the text field.

  • STEP 4: The user selects another text document from local files for comparison purposes.
  • The user selects another text document from local files for comparison purposes.

  • STEP 5: The file path will be displayed in the text field.
  • The file path will be displayed in the text field.

  • STEP 6: When the user clicks the 'Common Terms' button, the common terms between the two files are displayed in the text area below.
  • Common terms between the two files will be displayed.

Related Links