Remove Duplicates from Two Dimensional ArrayList - java

I have a two dimensional ArrayList that I need to filter the duplicates out of.
current result of generateRows: [["one"], ["two"], ["three"], ["one"]]
Java Pseudo Code:
ArrayList<ArrayList<String>> rows = new ArrayList<ArrayList<String>>();
rows = generateRows(); //method not shown, but returns a ArrayList<ArrayList<String>>
Set<String> set = new LinkedHashSet<String>();
for (ArrayList<String> list:rows) {
set.addAll (list);
}
rows.clear();
rows.add(new ArrayList<String>(set));
current result after running above conversion code: [[one, two, three]]
desired result: [["one"], ["two"], ["three"]]

Keeping code in same lines as what you have posted, here is what you have to do.
ArrayList<ArrayList<String>> rows = new ArrayList<ArrayList<String>>();
rows = generateRows(); //method not shown, but returns a ArrayList<ArrayList<String>>
Set<ArrayList<String>> set = new LinkedHashSet<ArrayList<String>>();
set.addAll(rows);
rows.clear();
rows.addAll(set);
Main issue with your routine:
Set<String> set = new LinkedHashSet<String>(); you are saying set is going to contain String types while your real set of objects to be unique is of ArrayList type

You can't store String values ("one", "two", "three") in a list which is supposed to store ArrayList<String> values.
To store the three strings, you'll need to create a new list of type ArrayList<String>.
ArrayList<ArrayList<String>> rows = new ArrayList<ArrayList<String>>();
rows = generateRows();
Set<String> set = new LinkedHashSet<String>();
for (ArrayList<String> list:rows) {
set.addAll (list);
}
//rows.clear();
//rows.add(new ArrayList<String>(set));
ArrayList<String> noDups = new ArrayList<String>(set);
Btw,
These lines...
ArrayList<ArrayList<String>> rows = new ArrayList<ArrayList<String>>();
rows = generateRows();
are equivalent to
ArrayList<ArrayList<String>> rows = generateRows();
You should prefer to program against interfaces instead of concrete classes, so if I were you I would write it as
List<List<String>> rows = generateRows();

Related

Comparing two ArrayLists and remove duplicates from original ArrayList

I have two Custom Arraylist:
List<Item> before = new ArrayList<Item>();
List<ItemEx> after = new ArrayList<ItemEx>();
before.add(new Item(1L,"test1"));
before.add(new Item(2L,"test2"));
before.add(new Item(3L,"test3"));
after.add(new ItemEx(1L,"test4"));
after.add(new ItemEx(2L,"test5"));
after.add(new ItemEx(4L,"test6"));
after.add(new ItemEx(5L,"test7"));
I want to store the elements in the List<ItemEx> after and the element shoulds be after the removing of common element is {3L, 4L, 5L}.
FYI
List<Item> & List<ItemEx> should be SAME TYPE .
Logic
List<String> before = new ArrayList<String>();
List<String> after = new ArrayList<String>();
List<String> list_checking = new ArrayList<String>(before);
list_checking.addAll(after);
List<String> list_common = new ArrayList<String>(before);
list_common.retainAll(after);
list_checking.removeAll(list_common);
Try this :
HashSet hs = new HashSet();
hs.addAll(before);
hs.addAll(after);
after.clear();
after.addAll(hs);
Now, in after list you get desire values.
Its simple. Take a copy of the existing one into a temporary Variable if you want for future use.
ArrayList temporiginalArrList=OriginalArrayList;
//here 'T' can be a specific object to want to save
OriginalArrayList.removeAll(secondArrayList);

java - "int cannot be converted to ArrayList"

I'm trying to create a two dimensional ArrayList which will store ints, strings & booleans.
I've got as far as entering the first int, but I get a red squiggle and the error "int cannot be converted to ArrayList".
ArrayList[][] qarray= new ArrayList [10][5];
qarray[0][0]= 1;
BTW, googling the phrase "int cannot be converted to ArrayList" is giving me exactly six results.
The error is correct.
Your array type is ArrayList. You can insert only ArrayLists in that array.
If you want to store int's, your declaration should be.
int[][] qarray= new int [10][5];
And also, as someone commented, you cannot store strings and booleans in this array anymore.
ArrayList[][] qarray= new ArrayList [10][5];
Basically your code creates 2 dimensional array list objects (50 array list objects).
qarray[0][0]= 1;
And you are trying to assign integer, where you need to create a ArrayList object. It expects something like
qarray[0][0]= new ArrayList();
However this would not meet your objective. The following piece of code could meet your objectives:
ArrayList[] qarray = new ArrayList[10];
qarray[0]= new ArrayList();
qarray[0].add(1);
qarray[1]= new ArrayList();
qarray[1].add(true);
qarray[2]= new ArrayList();
qarray[2].add("hello");
Try like this:
List<Integer> qarray = new ArrayList<>();
qarray.add(1);
First I believe that you need array and not ArrayList. 2d array can be created as following.
int[][] arr = new int [10][10];
Your next problem is that you tried to assign int constant 1 to variable of other type. The following example shows how to assign int to element of array
arr [0][0] = 1;
According to javadoc, you can not create arrays of ArrayList. Use 2D array instead.
If you need 2D ArrayList anyway, you should have tried this way:
ArrayList<ArrayList<Integer>> listOfLists = new ArrayList<>();
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(5);
listOfLists.add(list1);
listOfLists.add(list2);
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
you wan to add int,String and boolean then you can use
ArrayList<ArrayList<Object>> listOfLists = new ArrayList<ArrayList<Object>>();
it will help you
ArrayList<Integer>[][] list = new ArrayList[10][10];
list[0][0] = new ArrayList<>();
list[0][0].add(new Integer(10);
try like this.

how does list of list of string can copy the value of a sublist

is there any way to add a list A in a list of list B on iteration changing the list A everytime.
ArrayList<ArrayList<String>> totalFile = new ArrayList<ArrayList<String>>();
ArrayList<String> file = new ArrayList<String>();
while(fp.next()){
file.add(fp.getString(1));
file.add(fp.getString(2));
file.add(fp.getString(3));
file.add(fp.getString(4));
file.add(fp.getString(5));
file.add(fp.getString(6));
totalFile.add(file);
file.clear();
System.out.println(totalFile);
}
fp is resultSet.
i found that totalFile is making a reference to the file , but how can i copy a list A in a list of list B then clear the list A n populate it again n again copy list A to list of list B , and do that for n times
ArrayList<ArrayList<String>> totalFile = new ArrayList<ArrayList<String>>();
while(fp.next()){
ArrayList<String> file = new ArrayList<String>();
file.add(fp.getString(1));
file.add(fp.getString(2));
file.add(fp.getString(3));
file.add(fp.getString(4));
file.add(fp.getString(5));
file.add(fp.getString(6));
totalFile.add(file);
System.out.println(totalFile);
}
Create a new ArrayList inside the loop; don't try reusing the same list.

Adding elements from a List to a List of Lists

I trying to write some code in Java - I have two Lists:
List<List<Integer>> listOfLists;
List<Integer> listOfIntegers;
I want to add each of the Integers in listOfIntegers to a new element (List) in listOfLists, then I need to clear listOfIntegers
I tried:
listOfLists.add(listOfIntegers);
listOfIntegers.clear();
but this clears the reference to the List in listOfLists
Thanks to Manuel Silva I came up with what I need:
List<Integer> newList = new ArrayList<>();
for (Integer i : lineIntegersList)
{
newList.add(i);
}
listOfLists.add(newList);
lineIntegersList.clear();
You could do something like this...
ArrayList<ArrayList<String>> listOlists = new ArrayList<ArrayList<String>>();
ArrayList<String> singleList = new ArrayList<String>();
singleList.add("hello");
singleList.add("world");
listOlists.add(singleList);
ArrayList<String> anotherList = new ArrayList<String>();
anotherList.add("this is another list");
listOlists.add(anotherList);
thanks #tster - here you instantiate your "parent list" and as you would know how to do - just create a child list to add to the parent.. :D
for (Integer i: listOfIntegers) {
List<Integer> list = new LinkedList<Integer>();
list.add(i);
listOfLists.add(list);
}
This solution basically adds a new List with one element for each integer in the list of integer to the initial list of list

How to combine two list string into one list

I have two list string different,
List<String> A= [1,2,3,4];
List<String> B= [1,2,5,6];
And I want to combine two list, in new list string in
List C = new Arraylist ();
how to combine two list string , be like the example:
C = [1,2,3,4,5,6];
Use Collection.addAll(), and a TreeSet, to remove duplicates and keep the result sorted.
Set<String> c = new TreeSet<String>(a); //create a Set with all the elements in a
c.addAll(b); //add all the elements in b
This will get them combined
combined = new ArrayList<String>();
combined.addAll(A);
combined.addAll(B);
This will get the uniques
List<String> uniques = new ArrayList<String>(new HashSet<String>(combined));
Do it like this :
listOne.removeAll(listTwo);
listTwo.addAll(listOne);
Collections.sort(listTwo);
You can remove the third line if you do not want it to be sorted.
Declaration of A and B seems to be wrong.
However, given two lists A and B, to combine them into C, you can use the following code:
C.addAll(A);
C.addAll(B);
Set<String> set = new TreeSet<String>(A); // for keeping the output sorted else you can also use java.util.HashSet
set.addAll(B);
List<String> finalList = new ArrayList<String>(set);
There are two ways to merge the results of both lists: using List#addAll or Set#addAll. The main difference between both is heavily explained here: What is the difference between Set and List?
Based on your request, you should merge both lists without repeating the items using a Set
List<String> lstA = new ArrayList<String>();
lstA.add("1");
lstA.add("2");
lstA.add("3");
lstA.add("4");
List<String> lstB = new ArrayList<String>();
lstA.add("1");
lstA.add("2");
lstA.add("5");
lstA.add("6");
Set<String> lstC = new LinkedHashSet<String>();
lstC.addAll(A);
lstC.addAll(B);
List<String> lstAB = new ArrayList(lstC);
List<String> A= new ArrayList<String>();
List<String> B= new ArrayList<String>();
Set<String> set = new TreeSet<String>(A);
set.addAll(B);
System.out.println(new ArrayList<String>(set));
To get a List<String> in sorted order, use this piece of code
String a[] = {"1","2","3","4"};
String b[] = {"1","2","5","6"};
List<String> A= Arrays.asList(a);
List<String> B= Arrays.asList(b);
Set<String> CTemp = new TreeSet<>();
CTemp.addAll(A);
CTemp.addAll(B);
List<String> C = new ArrayList<>(CTemp);

Categories