0

Previously I asked on this and I realized that it was a mistake on my side. The list was actually tag3.add instead. Java how to compare 2 ArrayList of different objects

Anyway, here's the second part of the story... Now that I have 2 lists, how to I actually elegantly separate the 2 lists? Now it seems like I have the lists joined together. For the results I am using it to pace in different columns based on the id itself:

package com.java;
import java.util.*;
public class TestList {
 public static void main(String []args) {
    Tag tag1 = new Tag();
    tag1.setId(15);
    tag1.setTag("Test");

    Tag tag2 = new Tag();
    tag2.setId(15);
    tag2.setTag("Oracle");

    Tag tag3 = new Tag();
    tag3.setId(15);
    tag3.setTag("OHNO CANNOE");

    Tag tag4 = new Tag();
    tag4.setId(16);
    tag4.setTag("Test");

    Tag tag5 = new Tag();
    tag5.setId(16);
    tag5.setTag("Oracle");

    Tag tag6 = new Tag();
    tag6.setId(16);
    tag6.setTag("OHNO CANNOE");


    List<Tag> tagList = new ArrayList<Tag>();

    tagList.add(tag1);
    tagList.add(tag2);
    tagList.add(tag3);
    tagList.add(tag4);
    tagList.add(tag5);
    tagList.add(tag6);

    System.out.println(tagList.size());

    AnotherTest test1 = new AnotherTest();
    test1.setId(15);
    test1.setTestcol("Another test col");

    AnotherTest test2 = new AnotherTest();
    test2.setId(15);
    test2.setTestcol("HAHAHA");

    AnotherTest test3 = new AnotherTest();
    test3.setId(16);
    test3.setTestcol("name it");

    AnotherTest test4 = new AnotherTest();
    test4.setId(16);
    test4.setTestcol("checkmate");

    List<AnotherTest> anotherTests = new ArrayList<AnotherTest>();
    anotherTests.add(test1);
    anotherTests.add(test2);
    anotherTests.add(test3);
    anotherTests.add(test4);

    System.out.println(anotherTests.size());
    List<String> getTaglist = new ArrayList<String>(tagList.size());
    for(AnotherTest anotherTest : anotherTests) {
        for(Tag tag: tagList) {
            if(tag.getId()==anotherTest.getId()) {
                //getTaglist = new ArrayList<String>();
                getTaglist.add(tag.getTag());
            }
        }
    }

    for (String str: getTaglist) {
        System.out.println(str);
    }
    /* returns:

    Test
    Oracle
    OHNO CANNOE
    Test
    Oracle
    OHNO CANNOE
    Test
    Oracle
    OHNO CANNOE
    Test
    Oracle
    OHNO CANNOE
    But I want 2 separate lists.
    */
}
}

How do I do this?

6
  • What do you mean "separate"? You have two List(s); anotherTests and tagList - they are separate and distinct. Commented Apr 8, 2014 at 23:31
  • Well, the system.out.println at the last portion of the code was originally a list. Commented Apr 8, 2014 at 23:37
  • Three List(s) then when you add getTaglist; but what are you trying to do? Commented Apr 8, 2014 at 23:39
  • I am trying to get 2 lists from the "taglist" because of the ids. I want to separate id15 and id16 when I compare with anotherTest list. Is it possible? Commented Apr 8, 2014 at 23:59
  • If I understand you, then yes. And you're already doing it. Do you want to make getTaglist (which is a horrible variable name) into a Map<Integer, List<String>> where Integer is your id and the List<String> are your tag(s)? Commented Apr 9, 2014 at 0:25

1 Answer 1

0

Something with a Map<Integer, List<String>> like so -

Map<Integer, List<String>> tagMap = new HashMap<Integer, List<String>>();
for (AnotherTest anotherTest : anotherTests) {
  int id = anotherTest.getId();
  for (Tag tag : tagList) {
    if (id == tag.getId()) {
      List<String> al = null;
      if (tagMap.containsKey(id)) { // is this `id` already populated?
        al = tagMap.get(id);
      } else { // No! Create a new List.
        al = new ArrayList<String>();
        tagMap.put(id, al);
      }
      // Add the tag to the List.
      al.add(tag.getTag());
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.