how to compare multple arraylist with each other java.

comparing arraylist with each other which is in linked hash map.
I have to compare each arraylist with other arrylist.whem matching arraylist is found then keep there keys(for which arrylist is same).
in a diff collection.For optimization we sud not compare same list with it self(like comparing arraylist 1 with arrylist 1).
 
test3.java is what i done.
need ur help ......excuse me if disturbed u...
 
ArrayList<String> al = new ArrayList<String>();
 
al.add("1");al.add("2");al.add("3");al.add("4");al.add("6");
 
ArrayList<String> a2 = new ArrayList<String>();
 
a2.add("1");a2.add("2");a2.add("3");a2.add("4");a2.add("6");
 
ArrayList<String> a3 = new ArrayList<String>();
 
a3.add("1");a3.add("2");a3.add("33");a3.add("44");a3.add("5");
 
ArrayList<String> a4 = new ArrayList<String>();
 
a4.add("1");a4.add("2");a4.add("33");a4.add("44");a4.add("5");
 
LinkedHashMap<String,ArrayList<String>> lhm = new LinkedHashMap<String,ArrayList<String>>();
 
lhm.put("rule1", al);
 
lhm.put("rule2", a2);
 
lhm.put("rule3", a3);
 
lhm.put("rule4", a4);
 
 
I have to compare each arraylist with other arrylist.whem matching arraylist is found then keep there keys(for which arrylist is same)
in a diff collection.For optimization we sud not compare same list with it self(like comparing arraylist 1 with arrylist 1).
 
test3.java is what i done.
 
insted of for loop i have used iterator that may be the problem.
 
In the attached java file the out put sud keep rule1,rule2 same and rule3,rule4 same because there corresponding list are same. 
 
plz check it.
wating 4 ur reply....
Regards
 
Satyabrata Panda

Open in new window

test3.txt
satyabrata25Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

a_bCommented:
Here is some pseudo code, let me know if this works for you. Then we can go ahead with coding issues
for(int i=0;i<lhm.size();i++)
        {
            for(int j=i;j<lhm.size();j++)
            {
                if(i!=j)
                {
                    ArrayList<String> a = 
                    ArrayList<String> b =
                    boolean flag = true;
                    for(int k=0;k<a.size();a++)
                    {
                        String temp = a.get(k);
                        if(!a.contains(temp))
                        {
                            flag = false;
                        }
                    }
                    
                    if(flag == true)  
                         List are equal
                    else List are not equal
                
                        
                }
            }
                
        }

Open in new window

0
satyabrata25Author Commented:
How to get these two values dude??

ArrayList<String> a =
ArrayList<String> b =

My requrement is when two list are equal then i have to keep there keys in some collection and when list are not equal then i have to keep there keys  in some other collection.hope u get what i am saying .plz reply..
0
a_bCommented:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
 
 
public class Test
{
    public static void main(String args[])
    {
	
	ArrayList<String> al = new ArrayList<String>();
	 
	al.add("1");al.add("2");al.add("3");al.add("4");al.add("6");
	 
	ArrayList<String> a2 = new ArrayList<String>();
	 
	a2.add("1");a2.add("2");a2.add("3");a2.add("4");a2.add("6");
	 
	ArrayList<String> a3 = new ArrayList<String>();
	 
	a3.add("1");a3.add("2");a3.add("33");a3.add("44");a3.add("5");
	 
	ArrayList<String> a4 = new ArrayList<String>();
	 
	a4.add("1");a4.add("2");a4.add("33");a4.add("44");a4.add("5");
	 
	LinkedHashMap<String,ArrayList<String>> lhm = new LinkedHashMap<String,ArrayList<String>>();
	 
	lhm.put("rule1", al);
	 
	lhm.put("rule2", a2);
	 
	lhm.put("rule3", a3);
	 
	lhm.put("rule4", a4);
	
	ArrayList<String> keySet = new ArrayList<String>();
	//String[] keySet = {"rule1, rule2,rule3,rule4"};
	
 
	Set<String> set = lhm.keySet();
	Iterator it = set.iterator();
	    while (it.hasNext()) {
	        // Get element
	        String element = (String)it.next();
	        keySet.add(element);
	    }
	
	for(int i=0;i<lhm.size();i++)
        {
            for(int j=i;j<lhm.size();j++)
            {
                if(i!=j)
                {
                    ArrayList<String> a = lhm.get(keySet.get(i));
                    ArrayList<String> b = lhm.get(keySet.get(j));
                    boolean flag = true;
                    for(int k=0;k<a.size();k++)
                    {
                        String temp = a.get(k);
                        if(!a.contains(temp))
                        {
                            flag = false;
                        }
                    }
                    
                    for(int k=0;k<b.size();k++)
                    {
                        String temp = b.get(k);
                        if(!a.contains(temp))
                        {
                            flag = false;
                        }
                    }
                    
                    if(flag == true)  
                         System.out.println(keySet.get(i) +" == "+ keySet.get(j));
                    else System.out.println(keySet.get(i) +" != "+ keySet.get(j));
                
                        
                }
            }
                
        }
    }
}

Open in new window

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

satyabrata25Author Commented:
its almost done but suppose there are 7 rules
rule1,rul2,rule3 are same rule 4,rul5 are same and rule 6,rule7 is diff..then plz make a collection
which will keep rules like { (rule1,rul2,rule3), ( rule 4,rul5) } and in diff collection keep {rule6,rule7}.

hope u r getting what i am trying to say.plz create a appropiate collection.

thanks 4 ur reply..
0
a_bCommented:
What have you written so far? Let's try and improve on that. Post your attempt.
0
satyabrata25Author Commented:
Can you get the requrement i have given.I am waiting for ur reply if ur still not clear what to do

plz ask me...but dont remain quite..can u check how to do it...

0
a_bCommented:
I understand the requirement, what I am asking is for your attempt at the problem. What is the code that you have written so far? Can you post that?
0
satyabrata25Author Commented:
hi this is what i have written after so mant hit and trail..its not modification of ur code.

i am not able to create a proper collection where it can be store in this format.

keep rules like { (rule1,rul2,rule3), ( rule 4,rul5) } and in diff collection keep {rule6,rule7}.

The program which i wrote has objects like rule1=rule2=,like this just check bro..need ur help

go through my code also...
test6.txt
0
a_bCommented:
I am thinkking almost the same way you are.

We can have 2 datastructure -

ArrayList<LinkedList> matchingLinkedList  = new ArrayList<LinkedList>();
if there is a match then look for the matching Linkedlist in the list add it there, otherwise create a new linkiedlist and add it to the list
      
ArrayList<String> notMatching = new ArrayList<String>();
if there is no match, then store here       
0
satyabrata25Author Commented:
can u modify ur code make it the way i am thinking.......plz test with diff data so that the
program can be complete..can i have ur personal mail id if u dont mind....
because ur progam is much clearer than mine..can u made some changes to ur program and make it work..the way i want..

0
a_bCommented:
satyabrata25:

Why don;t we take my code and start work on that.
0
a_bCommented:
Try formulating the test cases
0
satyabrata25Author Commented:
ok i think u r not going to modify ur code ur out put is diff. from what i want...
if u could have modify ur code it will help me rather than playing chat here.
its ur wish but u could do it fast...
wfr
0
a_bCommented:
I am working on the code, would help if you help with formulating the test cases.
0
satyabrata25Author Commented:
any help from ur side will be appreciated genius...
just find the test cases u ll know what i want...
testcases.txt
0
a_bCommented:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Set;
 
 
public class Test
{
    public static void main(String args[])
    {
	
	ArrayList<String> al = new ArrayList<String>();
	 
	al.add("1");al.add("2");al.add("3");al.add("4");al.add("6");
	 
	ArrayList<String> a2 = new ArrayList<String>();
	 
	a2.add("1");a2.add("2");a2.add("3");a2.add("4");a2.add("6");
	 
	ArrayList<String> a3 = new ArrayList<String>();
	 
	a3.add("1");a3.add("2");a3.add("33");a3.add("44");a3.add("5");
	 
	ArrayList<String> a4 = new ArrayList<String>();
	 
	a4.add("1");a4.add("2");a4.add("33");a4.add("44");a4.add("5");
	 
	LinkedHashMap<String,ArrayList<String>> lhm = new LinkedHashMap<String,ArrayList<String>>();
	 
	ArrayList<String> a5 = new ArrayList<String>();
	 
	a5.add("1");a5.add("2");a5.add("33");a5.add("44");a5.add("5111");
	
	ArrayList<String> a6 = new ArrayList<String>();
	 
	a6.add("1");a6.add("2");a6.add("33");a6.add("44");a6.add("5111");
	
	ArrayList<String> a7 = new ArrayList<String>();
	 
	a7.add("1");a7.add("2");a7.add("33");a7.add("44");a7.add("5111"); 
	
	ArrayList<String> a8 = new ArrayList<String>();
	 
	a8.add("1");a8.add("2");a8.add("33");a8.add("44");a8.add("51231111"); 
	 
	lhm.put("rule1", al);
	 
	lhm.put("rule2", a2);
	 
	lhm.put("rule3", a3);
	 
	lhm.put("rule4", a4);
	
	lhm.put("rule5", a5);
 
	lhm.put("rule6", a6);
 
	lhm.put("rule7", a7);
	
	lhm.put("rule8", a8);
 
	ArrayList<String> keySet = new ArrayList<String>();
	//String[] keySet = {"rule1, rule2,rule3,rule4"};
	
 
	Set<String> set = lhm.keySet();
	Iterator it = set.iterator();
	    while (it.hasNext()) {
	        // Get element
	        String element = (String)it.next();
	        keySet.add(element);
	    }
	ArrayList<LinkedList> matchingLinkedList  = new ArrayList<LinkedList>();
	ArrayList<String> matching  = new ArrayList<String>();
	
	ArrayList<String> notMatching = new ArrayList<String>();
	for(int i=0;i<lhm.size()-1;i++)
        {
            for(int j=i;j<lhm.size();j++)
            {
                if(i!=j)
                {
                    ArrayList<String> a = lhm.get(keySet.get(i));
                    ArrayList<String> b = lhm.get(keySet.get(j));
                    boolean flag = true;
                    for(int k=0;k<a.size();k++)
                    {
                        String temp = a.get(k);
                        if(!a.contains(temp))
                        {
                            flag = false;
                        }
                    }
                    
                    for(int k=0;k<b.size();k++)
                    {
                        String temp = b.get(k);
                        if(!a.contains(temp))
                        {
                            flag = false;
                        }
                    }
                    
                    if(flag == true)  
                    {
                	System.out.println(keySet.get(i) +" == "+ keySet.get(j));
                	if(matching.contains(keySet.get(i)) || matching.contains(keySet.get(j)))
                	{    
                		for(int c=0;c<matchingLinkedList.size();c++)
                        	{
                        	    LinkedList temp = matchingLinkedList.get(c);
                        	    if(temp.contains(keySet.get(i)) || temp.contains(keySet.get(j)))
                        	    {
                        		matchingLinkedList.remove(temp);
                        		if(!temp.contains(keySet.get(i)))
                        		{
                        		    temp.add(keySet.get(i));
                        		}
                        		if( !temp.contains(keySet.get(j)))
                        		{
                        		    temp.add(keySet.get(j));                        		    
                        		}
                        		matchingLinkedList.add(temp);
                        		notMatching.remove(keySet.get(i));
                                	notMatching.remove(keySet.get(j));
                                	
                                	if(!matching.contains(keySet.get(i)))
                                	{
                                	    matching.add(keySet.get(i));
                                	}
                                	if(!matching.contains(keySet.get(j)))
                                	{
                                	    matching.add(keySet.get(j));                	    
                                	}
                                    }
                        	    
                        	    
                        	}
                	}
                    	else
                    	{
                    	    LinkedList ll = new LinkedList();
                    	    ll.add(keySet.get(i));
                    	    ll.add(keySet.get(j));
                    	    matching.add(keySet.get(i));
                	    matching.add(keySet.get(j));
                    	    matchingLinkedList.add(ll);
                    	    notMatching.remove(keySet.get(i));
                    	    notMatching.remove(keySet.get(j));
                    	}
                    }
                    else
                    {	
                	System.out.println(keySet.get(i) +" != "+ keySet.get(j));
                	if(!matching.contains(keySet.get(i)) && !notMatching.contains(keySet.get(i)))
                	{
                	    notMatching.add(keySet.get(i));
                	}
                	if(!matching.contains(keySet.get(j)) && !notMatching.contains(keySet.get(j)))
                	{
                	    notMatching.add(keySet.get(j));                	    
                	}
                	    
                    }
                
                        
                }
            }
                
        }
	System.out.println("Matching");
	for(int i=0;i<matchingLinkedList.size();i++)
	{
	    System.out.println(matchingLinkedList.get(i).toString());
	    
	}
	System.out.println("Not Matching");
	for(int i=0;i<notMatching.size();i++)
	{
	    System.out.println(notMatching.get(i));
	}
    }
}

Open in new window

0
satyabrata25Author Commented:
hi dear,
            your logic is failing when there is only one rule that sud come to nonmatching list.

can u check once and resove...
0
a_bCommented:
can you post the test case
0
a_bCommented:
The test case used in the prg, rule8 is the only one that is nonMatching, works for me.
0
satyabrata25Author Commented:
     LinkedHashMap<String,ArrayList<String>> lhm = new LinkedHashMap<String,ArrayList<String>>();
            ArrayList<String> al = new ArrayList<String>();al.add("1");al.add("2");al.add("3");al.add("4");al.add("6");       
            ArrayList<String> a2 = new ArrayList<String>();a2.add("1");a2.add("2");a2.add("3");a2.add("4");a2.add("6");       
            ArrayList<String> a3 = new ArrayList<String>();a3.add("1");a3.add("2");a3.add("33");a3.add("44");a3.add("5");
            ArrayList<String> a4 = new ArrayList<String>();a4.add("1");a4.add("2");a4.add("33");a4.add("44");a4.add("5");       
            ArrayList<String> a5 = new ArrayList<String>();a5.add("1");a5.add("2");a5.add("33");a5.add("44");a5.add("5111");      
            ArrayList<String> a6 = new ArrayList<String>();a6.add("1");a6.add("2");a6.add("33");a6.add("44");a6.add("5111");
            ArrayList<String> a7 = new ArrayList<String>();a7.add("1");a7.add("2");a7.add("33");a7.add("44");a7.add("5111");       
            ArrayList<String> a8 = new ArrayList<String>();a8.add("1");a8.add("2");a8.add("33");a8.add("44");a8.add("51231111");  
            lhm.put("rule1", al);/*lhm.put("rule2", a2);lhm.put("rule3", a3);lhm.put("rule4", a4);lhm.put("rule5", a5);lhm.put("rule6", a6);
            lhm.put("rule7", a7);lhm.put("rule8", a8);*/



here i have added only one rule to lhm...so it has to come in nonmatching list...got it.
ur code is working when there is multiple rules in lhm but it is failing wen there is only one rule.
0
a_bCommented:
you can add an if condition as an exception saying that if there is only one rule, the add to the nonmatching array.
0
a_bCommented:
Add before the for loop -

if(keySet.size() == 1)
      {
          notMatching.add(keySet.get(0));
      }
0
a_bCommented:
is it working now?
0
satyabrata25Author Commented:
thnaks very much it is working the way i wanted....

can i have ur personal mail id...need to discuss some thing on programming.
or u can drop a mail in tumbinindia@gmail.com
waiting for ur reply..good job....
0
a_bCommented:
Please close this thread by assigning the points. Thanks.
0
a_bCommented:
Hi satyabrata25,

If you have accepted my solution, you have to assign the points to me by accepting the solution.. Right now you are directly closing the thread without assigning any points.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
satyabrata25Author Commented:
good logic dude...i have given points to u...dont know why u r not able to see..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.