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?
anotherTestsandtagList- they are separate and distinct.List(s)then when you addgetTaglist; but what are you trying to do?getTaglist(which is a horrible variable name) into aMap<Integer, List<String>>where Integer is your id and the List<String> are your tag(s)?