Can't add array of enums to Collection - java

This code doesn't work;
ArrayList<BlockFace> cardinalDirections = new ArrayList<>();
cardinalDirections.addAll(new BlockFace[] {BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST});
And neither does this;
ArrayList<BlockFace> cardinalDirections = Arrays.asList(new BlockFace[] {BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST});
It only works if I add each element manually one at a time, or iterate through the array;
ArrayList<BlockFace> cardinalDirections = new ArrayList<>();
for (BlockFace face : new BlockFace[] {BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST})
cardinalDirections.add(face);
Why don't the top two examples work?

Your approach does not work because ArrayList.addAll takes Collection not an array.
Converting enum to ArrayList
List<BlockFace> list = Arrays.asList(BlockFace.values());

Related

Created two Lists from same pojo array, Modifying one List, same thing affects on other list also

I have created two lists object from the same pojo and sorted one of them. When I tried to change one list, other lists also got updated.
List<FilterPojo.Data> filterList = new ArrayList<>();
List<FilterPojo.Data> subFilterList = new ArrayList<>();
If I change the value in filterList, same changes occur in subFilterList
With the limited information that is provided by you, it seems you are creating/populating subFilterList as subList of filterList. When you do that, all changes made in either of the list will be reflected in other.
This happens because List.subList(), returns a view of the list, so modifications to the original list will be reflected in the sub-list. As suggested by others, instead of subList use addAll to populate subFilterList
This could be reference problem. Lists maintains their references when items are copied to other list, if you do something like:
List<FilterPojo.Data> subFilterList = filterList;
Use addAll method instead,
subFilterList.clear();
subFilterList.addAll(filterList);
Try below
List<String> filterList = new ArrayList<String>();
List<String> subFilterList = new ArrayList<String>();
filterList.add("A");
filterList.add("B");
filterList.add("C");
/*subFilterList = filterList; // reference to same object , change will reflect in both
filterList.add("C");
System.out.println(filterList);
System.out.println(subFilterList);*/
subFilterList.addAll(filterList);
filterList.add("C");
System.out.println(filterList);
System.out.println(subFilterList);
I don't know exactly the context that you are asking.
Your lists are holding the same object. For example, in this case p1.
Person p1 = new Person();
List<Person> list1 = new ArrayList<Person>();
list1.add(p1);
List<Person> list2 = new ArrayList<Person>();
list2.add(p1);
p1.setName("new name");

Collections sort on a copy of a Arraylist (to keep the orginal order)

If i have an arraylist, and i want both a sorted and unsorted version of it, how can i achive it.
tried with Collections
ArrayList<Integer> someNumbers = new ArrayList<>();
if i then make a new arraylist = someNumbers and sort it. The orginal one gets sorted too ?
ArrayList<Integer> sortedNumbers = someNumbers;
Collections.sort(sortedNumbers);
both list gets sorted.
How can i simply achive what im trying to do ? ( get a sorted copy, and keep the orginal in its orginal ordering )
When you do ArrayList<Integer> sortedNumbers = someNumbers;, you are not creating a new list to sort. You are basically assigning the variable sortedNumbers to the exact same list that someNumbers is assigned to.
To fix it, you can create a new list from your other list. Like this:
ArrayList<Integer> sortedNumbers = new ArrayList<>(someNumbers);
The reason why both lists get sorted is because even you first create a new ArrayList(), you then override this instance with a reference to someNumbers. Therefore when you sort sortedNumbers you actually sort the original list someNumbers.
There are several ways you can achieve your goal:
ArrayList<Integer> sortedNumbers = new ArrayList<>();
sortedNumbers.addAll(someNumbers);
Or you can achieve the same in one step by using the copy constructor:
ArrayList<Integer> sortedNumbers = new ArrayList<>(someNumbers);
You need to make a copy of the list and then sort it, e.g.
ArrayList<Integer> sortedNumbers = new ArrayList<>(someNumbers);
Collections.sort(sortedNumbers);
This uses ArrayList(java.util.Collection) constructor
Just copy the array and sort the copy:
ArrayList<Integer> sortedNumbers = new ArrayList<>(someNumbers);
Collections.sort(sortedNumbers);
You have to copy the ArrayList first to have two versions. Then you can sort one of the two ArrayLists
ArrayList<Integer> someNumbers = ...
ArrayList<Integer> sortedNumbers = new ArrayList<>(someNumbers);
Collections.sort(sortedNumbers);

Initializing multiple Lists in java simultaneously

I'm trying to initialize a List in Java but I want to know if there's a more elegant way of initializing multiple lists with the same types.
So far I've done the following:
List<Model> list1 = new List<>();
List<Model> list2 = new List<>();
List<Model> list3 = new List<>();
But I'm trying to initialize about 10 different lists of the same type and it seems very ugly.
I've also tried doing:
List<Model> list1, list2, list3 = new List<>();
But this doesn't work.
After searching for the answer, all I could find were tips on how to initialize an array with multiple variables in one line using the asList() method but that's not what I'm trying to do.
Is this even possible?
You can use a Map where the key represents the list name and the value represents a List
Map<String,List<Model>> lists = new HashMap<>();
You can then populate the list in a for loop :
for(int i=0;i<10;++i) {
lists.put("list"+(i+1),new ArrayList<Model>());
}
You can access the lists using :
lists.get("list1").add(new Model(...));
lists.get("list2").add(new Model(...));
Disclaimer : I have not tried compiling this code since I am not on a computer.
If you have 10 lists or whatever, it's time to think: probably you need an array or list of lists.
List<List<Model>> lists = new ArrayList<>();
for(int i=0; i<10; i++) lists.add(new ArrayList<>());
// later in code instead of list5.add(...)
lists.get(5).add(...)
List is an interface (abstract type) and cannot be instantiated. You will have to use ArrayList as shown below. You can try:
List<Model> list1 = new ArrayList<Model>(), list2 = new ArrayList<Model>();
This should work as well
List<Model> list1 = new ArrayList<Model>(), list2 = new ArrayList<Model>(), list3 = new ArrayList<Model>();
The closest possible thing that you can do is following
List<Model> a = new ArrayList<>(), b = new ArrayList<>(), c = new ArrayList<>(), d = new ArrayList<>();
But either of the approach you consider has same memory consumption impact.
Here's my answer:
#SuppressWarnings({"unchecked"})
List<Model>[] lists = new List[3];
for(List list : lists) {
list = new ArrayList<Model>();
}
List<Model> list1 = lists[0];
List<Model> list2 = lists[1];
List<Model> list3 = lists[2];

How to correctly specify a list in java

I am using Eclipse Juno and Java.
I want to create a list and then store that list in another list so I can pass the list of lists to the server side. I have tried:
ArrayList<T> listAccountAndCubs = new ArrayList<Comparable>();
listAccountAndCubs.add(accountId);
listAccountAndCubs.add(sqlDateArchived);
However, I can not get the values "T" and "Comparable" correct. I tried "String" however that does not work for storing the date.
Once the above is correct how do I set up the list to contain "listAccountAndCubs"?
Thanks for any assistance,
Glyn
this is how you can create a list
List<String> l = new ArrayList<String>();
this is how you can create list of list
List<List<Comparable>> listOfList = new ArrayList<List<Comparable>>();
listOfList.add(new ArrayList<Comparable>());
...
Sounds like you want something like this
List<List<String>> listAccountAndCubs = new ArrayList<List<String>>();
I would recomment using Google Guava library to clean the syntax a bit
List<List<String>> listAccountAndCubs = Lists.newArrayList();
List<ArrayList<Comparable>> listAccountAndCubs = new ArrayList<>();
or
List<String> l1=new ArrayList<>();
List<List<String>> l2=new ArrayList<>();
l1.add("a");
l2.add(l1);
If I understand you crrectly you want to have a list of Strings, and store this in another list?
List<String> sl = new ArrayList<String>();
List<List<String>>sls = new ArrayList<List<String>>();
sls.add(sl);
sl.add("String 1");
The value "T" is just a placeholder for the type, as the list is a generic interface, which can take any arbitrary object.
If you want to create a list of unspecified types, you would use
List<?>list = new ArrayList<?>();
Then you can add untyped objects to it, but in your case this is not neccessary.
Instead you can of course also create a list of comparables. Like this:
List<Comparable<String>>list = new ArrayList<Comparable<String>>();

java Collection FrameWork

i want to store Arraylist inside Another List, some thing like this i know its wrong but pretty much like this....
List list=new ArrayList();
list.add(new("element1","element2","element3",element4=?);
list.add(new("element5","element6","element7",element8=?);
now if u see the above code its ok and fine to add elements or first row to list up to third element but the fourth element is another array[string array] how to add it or append it to the first row of element.
same with the second row.
NOTE:- element4 and element8 are also differ in length means element4 has 2 string and element 8 has 10 strings.
when i display the list it should show list like this
The first row of list is
element1 element2 element3 element4.1 element4.2
the second row of list is
element5 element6 element7 element8.1 element8.2 element8.3 element8.4 element8.5 element8.6 element8.7 element8.9 element8.10
So from what I understood about your question, you need a List<List<String>>. Do it as follows:
List<String> strs1 = new ArrayList<String>();
strs1.add("element1");
strs1.add("element2");
List<String> strs2 = new ArrayList<String>();
strs2.add("element3");
And then
List<List<String>> listOfList = new ArrayList<List<String>>();
listOfList.add(strs1);
listOfList.add(strs2);
OR
List<List<String>> asList = Arrays.asList(strs1, strs2);
Have you tried following way:
List list = new ArrayList();
list.add(Arrays.asList("element1", "element2", "element3",
Arrays.asList("element4.1", "element4.2")));
list.add(Arrays.asList("element5", "element6", "element7",
Arrays.asList("element8.1", "element8.2", "element8.3")));
// print values
System.out.println(list.get(0));
System.out.println(list.get(1));
Unify it to a List of lists of lists, a three dimensional matrix - even if your first elements are single values, it makes sense to wrap them in lists just to simplify the code:
List<List<List<String>>> matrix = new ArrayList<List<List<String>>>();
List<List<String>> row = new ArrayList<List<String>>();
matrix.add(row);
List<String> column1 = new ArrayList<String>();
column1.add("element1");
row.add(column1);
List<String> column2 = new ArrayList<String>();
column1.add("element2.1");
column1.add("element2.2");
row.add(column2);
You can add lists to other lists by doing something like so: List<List<String>> myList = new ArrayList<List<String>>();.... However, seeing that you are adding items which have a different type, I would recommend you do the following (assuming you always have 3 array lists and 1 array):
Create a new class which takes in 4 arguments, these being the 3 array lists and the 1 array.
Have your class override its own toString() method in such a way that it will iterate over the elements and print their content in whatever way you would like.
Create an list using generics, using something like this: List<MyClass> myList = new ArrayList<MyClass>();.... In this case, MyClass is the class I have described in point 1. This will allow you to create a type safe structure which does not need to do any casting, thus making your code look cleaner and probably run slightly faster.
Seeing that you say that the elements can contain list of strings of various lengths, you can do something like this:
public class MyClass
{
private List<String> arrayList1;...
private String[] myArray;...
public MyClass(List<String> list1, ..., String[] myArray)
{
this.arrayList1 = list1;
this.myArray = myArray;
...
}
...
#Override
public String toString()
{
StringBuilder sb = new StringBuilder();
//iterate adding your list contents to your string builder.
return sb.toString();
}
}
Adding your elements then will be something like this:
...
List<String> arrayList1 = ...;
arrayList1.add("...");...
String[] myArray = ...;
MyClass myClass = new MyClass(arrayList1, ..., myArray);
System.out.println(myClass.toString());

Categories

Resources