Adding elements from a List to a List of Lists - java

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

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);

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.

Error: generic array creation with my own object

I want to create a list that contains linked list of MyObject.
LinkedList<MyObject>[] list = new LinkedList<MyObject>[n];
But it shows:
Main.java:19: error: generic array creation LinkedList<MyObject>[] list = new LinkedList<MyObject>[n];
How can I create this kind of list?
Why are you trying to create an array of LinkedLists? it is after all already a List.
Change your code to
List <MyObject> list = new LinkedList<MyObject>();
if you want a collection of these LinkedLists then I suggest that you create a new List
List <LinkedList<MyObject>> theBigList = new ArrayList <> ();
and then you can add to this list
theBigList.add (list);
A List of Lists would look like:
List<List<MyObject>> listList = new LinkedList<>();
List<MyObject> list1 = new LinkedList<>();
List<MyObject> list2 = new LinkedList<>();
listList.add(list1);
listList.add(list2);
List<MyObject> list3 = listList.get(0);
And an Array of LinkedLists would look like:
#SuppressWarnings("unchecked")
List<MyObject>[] listArray = new LinkedList[n];
List<MyObject> list1 = new LinkedList<>();
List<MyObject> list2 = new LinkedList<>();
listArray[0] = list1;
listArray[1] = list2;
List<MyObject> list3 = listArray[0];
Both assume the diamond operator (<>) which is a Java 7 shortcut, but can be replaced with the full type (new LinkedList<MyObject> for the second and LinkedList<List<MyObject>> for the first case).
And the fact that arrays cannot be instantiated of generic type (due to all kinds of weird internals). See for example http://bugs.java.com/bugdatabase/view_bug.do?bug_id=5105887

Pass by value trouble with java's list add method

ArrayList<ArrayList<String>> list1 = new ArrayList<ArrayList<String>>();
ArrayList<String> list2 = new ArrayList<String>();
list2.add("foo");
System.out.println(list2); //[foo]
list1.add(list2);
System.out.println(list1); //[[foo]]
list2.set(0, "bar");
System.out.println(list2); //[bar]
System.out.println(list1); //[[bar]]
The code above shows 2 lists. When I add list2 (containing foo) to list1 they both now contain foo. But when I modify list2 to bar, list1 will change as well. I've always thought the add method only gives a copy of list2 to list1 and what I do to list2 will not change for list1.
How can I make it that I add list2 to list1 but allow list2 to be freely modified in the future so list1 will not be affected again?
In Java references are passed by value, not the object referenced. This means that list2.add(list1); cannot alter the reference list1 but it takes a reference to the same object and if you change that it is visible.
The way you should write this is
List<List<String>> list1 = new ArrayList<List<String>>();
List<String> list2 = new ArrayList<String>();
list2.add("foo");
System.out.println(list2); //[foo]
list1.add(new ArrayList<String>(list2)); // take a copy of the list.
System.out.println(list1); //[[foo]]
list2.set(0, "bar");
System.out.println(list2); //[bar]
System.out.println(list1); //[[foo]]
or
list1.add(list2);
list2 = new ArrayList<String>();
list2.add("bar");
System.out.println(list2); //[bar]
System.out.println(list1); //[[foo]]
My example above is simple but my actual problem is buried in nested loops and such.
List<List<String>> list1 = new ArrayList<List<String>>();
for(some loop) {
List<String> list2 = new ArrayList<String>();
// populate list2, can be smaller than the previous list2 !!
list1.add(list2);
}
How about list1.add(list2.clone()); ?
This will create a copy of list2 (allocate memory, clone contents etc) and put reference to it in list1.
You should clone every item and add to a new 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