How to Display Duplicate elements from more than one List in java?

How to Display Duplicate elements from more than one List in java?

How to Display Duplicate elements from more than one List in java?

//I mean common elements from two List?

Ex: List a1=new ArrayList(); a1.add(50); a1.add(10); a1.add(20); a1.add(30); List a2=new ArrayList(); a2.add(50); a2.add(10); a2.add(20); a2.add(25); a2.add(40);

  from above code How to Display  common elements Display
View Answers

August 27, 2012 at 12:16 PM

Hare is an example that compare two array lists and display the common elements from both of them.

import java.util.*;

public class DisplayCommonElement{
  public static void main(String[]args){
    ArrayList a1 = new ArrayList();
    ArrayList a2 = new ArrayList();
     a1.add("a");
     a1.add("b");
     a1.add("c");

    a2.add("a");
    a2.add("b");
    a2.add("c"); 
    a2.add("d");
    a2.add("e");
        int array1Size = a1.size();
        int array2Size = a2.size();
        if (a1.size() > a2.size()) {
            int k = 0;
            for (int i = 0; i < a2.size(); i++)            {
                if (((String)a1.get(i)).equals((String)a2.get(i)))  {
                    System.out.println((String)a2.get(i));
                }
                k = i;
            }
           }
        else {
           int k = 0;
            for (int i = 0; i < a1.size(); i++) {
                if (((String)a1.get(i)).equals((String)a2.get(i))) {
                    System.out.println((String)a1.get(i));
                }
                k = i;
            }
         }
  }
}









Related Tutorials/Questions & Answers:
How to Display Duplicate elements from more than one List in java?
How to get more than one value from ajax
Advertisements
how to display duplicate elements with out using collection Frame work?
how to count unique and duplicate values from one table in mysql?
how to handle action events in case of more than one JFrame
Server side validation on dynamically generated fields from more than one table on spring framework.
Apply more than one xsl for single XML
More than one Faces Configuration file
more than one struts-config.xml file
Can a Class extend more than one Class?
how to restrict user against marking same event more than one times - JSP-Servlet
Implementing more than one Job Details and Triggers
Hi.. how to write more than one sheets in a excel file... pls anybody help me....
how can retrieve more than one values in text field using ajax?
how to display the values of one list in other upon clicking a button in struts2
Display non-duplicate words from file
for writting java program why we are taking more than one class
Deployment on Server that can be used simultaneously by more than one user
Java duplicate elements
Writing more than one cards in a WML deck.
How to Display Font Other than english
write a java pogram to add elements to linked list using keyboard and display it
how can i display the dates of one week by selecting the date randomly from the calendar?
how to insert list box in java script dynamically and elements retrieving from database like oracle
how to insert list box in java script dynamically and elements retrieving from database like oracle
How to write more than 65536 rows in single excel file but more than 1 sheets?
Display Different Elements from two ArrayLists in Java
difference between java5 and java6 - Java Beginners
Display Common Elements from two ArrayLists in Java
Select from select list + display
J query event for selecting the more than one div in tablet browser(Select and drag)?
Java arraylist duplicate elements search
Display related data in other drop down list on selecting one data in one drop down list
To get the value of more than one text box in an HTML page to a jsp page - JSP-Interview Questions
Error in connecting to the mySQL database in TOMCAT using more than one PC (database connection pooling)
about java1
Java2
how to display one image on jsp through java
how to display records from database
How to retrieve and display image from database in Java?
Use of "|" separator for selecting more than one path
Javah
Is Python more powerful than C++?
java list program for common elements
how to display data from database in jsp
Remove duplicate characters from the string
How to fetch entries/values from database to a jsp page one by one?
How to extract HTML elements from a page?
How to extract HTML elements from a page?
Artifacts of javax

Ads