(Groovy) Add multiple values to an ArrayList - groovy

like the title says, I want to add multiple values to my arrayList in Groovy.
But it is not accepted.
Collection<String> actorCollection = new ArrayList<>();
actorCollection.add("first","second");
If there is only a single value, it works perfectly.
Thanks in advance!

Use addAll: actorCollection.addAll("first","second")
Note: Groovy's list literal will give you an array list. So could just write def actorCollection = [] or even ... = ["first", "second"] to fill the list with the values right from the beginning.

Related

Hazelcast Predicate in clause

Can anyone tell me how to 'in' clause with Hazelcast predicate. I want to use the following,
Predicates.in('column',value);
I want to pass the value as an ArrayList of values. But it is throwing error as it expects object that implements comparable. Is there any workaround for this?
Predicates.in takes arguments of types (String, Comparable...).
So for the column name you pass a String as you have done.
Comparable... means you can pass the individual values in a comma-separated list, but you can also pass them in an array. An ArrayList won't automatically be converted, but you can do it as follows:
ArrayList<String> values = new ArrayList<>(Arrays.asList("one", "two", "three");
Predicates.in("column", values.toArray(new String[]));
The (new String[]) argument is just to ensure you get back an array of Strings, otherwise you'll get an array of Objects.

elements not getting added in list groovy script

The weird thing is happening while adding elements in list in groovy.
Scenario-
There are two List list1 and list2. List1 contains Object of X type and List2 is empty. List1 is getting populated from java file and while iterating List1 in groovy script, I am adding objects in List2.
But what happening is elements are not getting added. List2 remains empty.
If I debug the line and evaluate the expression/line it then it is getting added. But while normal debugging while executing this line, it suddenly jump to any random line.
No exception is coming.
Have created list as below:
List<X> dataToBeRemoved = new ArrayList<>()
Iterating the list as below:
for (X data in XList) {
if(something) {
dataToBeRemoved.add(data)
}
}
I am new to Groovy and If any one have ever faced this kind of issue. Please guide. Thanks.
You didn't ask, but type parameters don't get you much.
List elementsToRemove = []
And, in this case even better:
List elementsToRemove = allElements.findAll { ...some condition... }
After that, it's impossible to tell from your code. Questions such as "Why doesn't Groovy work?" are hard to answer.
You can define the an empty list by simply using
def mySmallList = []
and you may also use findAll to filter out the list
mySmallList = myBigList.findAll {//some condition }
Please check the link https://groovyconsole.appspot.com/script/5127180895911936

JavaFX 8 iterate hash map

Given this hashmap:
HashMap<TitledPane, Boolean> paneMap = new HashMap<>();
I'm trying to set all values in the map to false (values are set selectively in the application after 'clearing' the map), but no matter what I have tried I cannot get an appropriate iterator which will allow me to set each boolean value. Can someone help please?
Just spoke to someone by phone and there is a very simple answer which I just didn't see in the end, because I tried to make it too complicated (I hadn't realised that 'put' replaces any existing values.
for (TitledPane p : paneMap.keySet()){
paneMap.put(p, Boolean.FALSE);
}
You can use lambda:
paneMap.replaceAll((k, v) -> Boolean.FALSE);

ArrayList changing after sorting function

I have just started utilizing ArrayLists in some C# code and am having some problems when sorting.
First I define create an ArrayList object under my class:
ArrayList cutList = new ArrayList;
Then I set and sort the array list to find the minimum:
cutList.Add("2200","1800","1200","1");
int minList = (int)GetMinValue(cutList);
Using the function:
public static object GetMinValue(ArrayList arrList)
{
ArrayList sortArrayList = arrList;
sortArrayList.Sort();
return sortArrayList[0];
}
Later I try to find the index cutList[2] and I find "1200" because the function also sorted cutList. I have also had the same problem in the past, when I set a variable to an Application settings and then the Applications setting changes when I modify the variable. How to I correctly fix these problems. I have been learning C# on my own and am guilty of skipping around a little bit. Is there a lesson on Objects that I am missing?
The issue in your code is that ArrayList sortArrayList = arrList; does not copy arrList to sortArrayList: the assignment merely creates a new alias for the existing object. To make your code work, use
ArrayList sortArrayList = (ArrayList)arrList.Clone();
I must add that this is probably the most inefficient way of looking up the min element in a list, and also a rather archaic container. I would prefer using List<string> instead of ArrayList, and using LINQ's Min() function to get the minimum element.

multi dimensional string array in C#

I need to create a multi dimensional array of strings in C# like-
tiger elephant
pigeon lion
ant peacock
I'm unable to create. It either flags an error or throws an exception at the run time.
Please help me with it, by giving the correct syntax.
string[] animals = new string[]{"tiger", "elephant", "pigeon", "lion", "ant", "peacock"};
Am not sure I understand the question, the above doesn't appear to be a multidimensional array.
Do you simply want somthing along the lines of the below?
string[] animals = {"tiger", "elephant"};
LOOK AT THIS
string[,] siblings = new string[2, 2] { {"Mike","Amy"}, {"Mary","Albert"} };
and also look for jagged array
Here is an example of how to create multi-dimensional string array and have it initialized:
string[] myStringArray = new string[] { tiger, elephant, pigeon, lion, ant, peacock};

Resources