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

Ads
 

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;
            }
         }
  }
}

Ads









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
Can a Class extend more than one Class?
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
how can i display the dates of one week by selecting the date randomly from the calendar?
How to Display Font Other than english
How to write more than 65536 rows in single excel file but more than 1 sheets?
write a java pogram to add elements to linked list using keyboard and display it
Display related data in other drop down list on selecting one data in one drop down list
J query event for selecting the more than one div in tablet browser(Select and drag)?
how to display records from database
How to retrieve and display image from database in Java?
Select from select list + display
how to display one image on jsp through java
Java arraylist duplicate elements search
Java2
How to fetch entries/values from database to a jsp page one by one?
How to navigate from one jsf to another in eclipse
How to Display values from databse into table
java list program for common elements

Ads