1

my title is bad but I don't have an idea what it should be. My question is simple, I have four arraylist and I want to get similar words from two of them and put another arraylists. Anyway my array lists like;

arrList1 = {car, apple, many, car, tyty, man, superman};
arrList2 = {stack, vs, etc, vs, car, tyty, stack,  tyty, many, car, apple};

I tried this;

for (int i = 0; i < arrList1.size(); i++) {
        for (int j = 0; j < arrList2.size(); j++) {

            if (arrList1.get(i).equals(arrList2.get(j))) {
              arrList3.add(arrList1.get(i);
              arrList4.add(arrList2.get(j);
         }
      }

But as you see arrList1 and arrList2 have duplicates so arrList4 will have same element more than normal. Also I have to count elements which are in arrList1 and arrList2 so I shouldn't use Set Collections. What should I do?

PeerNet
  • 855
  • 1
  • 16
  • 31
  • 1
    can post an expected result. – Rod_Algonquin Aug 22 '14 at 21:33
  • check out: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#contains(java.lang.Object) and http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object) – Mark W Aug 22 '14 at 21:35
  • possible duplicate of [Intersection and union of ArrayLists in Java](http://stackoverflow.com/questions/5283047/intersection-and-union-of-arraylists-in-java) – jdphenix Aug 22 '14 at 21:36
  • I expect; arrList3 = { car, car, apple, many, tyty} and arrList4 = { car, car, tyty, tyty, many, apple} – PeerNet Aug 22 '14 at 21:38
  • I don't understand where these are coming from? Explanation? – MusicMaster Aug 22 '14 at 21:40
  • I read a text file and tokenize it. And I put words to ArrayLists with a relation. Some words to arr1 and some words to arr2. These words just an example not exact word – PeerNet Aug 22 '14 at 21:41
  • What do you want to look for in lists 1 and 2 to put in lists 3 and 4? – MusicMaster Aug 22 '14 at 21:42
  • I want to count similar words, (how many times written) in arrList1 and arrList2. Also I want to put similar words from arr1 and arr2 so I put similar words arr1 to arr3 and arr2 to arr4 – PeerNet Aug 22 '14 at 21:45
  • I updated my answer again. – MusicMaster Aug 22 '14 at 21:56
  • Sounds like you're looking for a multiset. Check out Guava's impl – Andreas Aug 22 '14 at 21:57

3 Answers3

1

Try

ArrayList<String> temp = new ArrayList<String>();
boolean found = false;
for (int i = 0; i < arrList1.size(); i++) {
    found = false;
    for (int j = 0; j < arrList2.size(); j++) {
        if (arrList1.get(i).equals(arrList2.get(j))) {
            found = true;
            if (!temp.contains(arrList2.get(j)) {
                arrList4.add(arrList2.get(j));
            }
        }
    }
    if (found) {
        arrList3.add(arrList1.get(i));
        temp.add(arrList1.get(i));
    }
}

This will check if the new ArrayList does not already contain the item.

MusicMaster
  • 549
  • 4
  • 14
0

Try this

    ArrayList tempList=new ArrayList(arrList1);
    tempList.removeAll(arrList2);
    arrList4 = new ArrayList(arrList1);       
    arrList4.removeAll(tempList);
Tkachuk_Evgen
  • 1,334
  • 11
  • 17
0

You should use another type of list but you can also make-do with an arraylist like so:

for (int i = 0; i < arrList1.size(); i++) {
    for (int j = 0; j < arrList2.size(); j++) {

        if (arrList1.get(i).equals(arrList2.get(j))) {
          if(!(arrList3.contains(arrList1.get(i))) {
          arrList3.add(arrList1.get(i);
          }

          if(!(arrList4.contains(arrList2.get(j))) {
          arrList4.add(arrList2.get(j);
        }
     }
  }

hope that helps.

ABOODYFJ
  • 30
  • 1
  • 9