Is there a simple way of check, if an item of array1 exists in array2?
List<String> array1 = new ArrayList<>();
array1.add("Hello");
array1.add("Test");
List<String> array2 = new ArrayList<>();
array2.add("Test");
array2.add("Item");
So, "Test" is in array1 and array2, therefore the answer should be true.
Is there a simple way to do it?
Collections.disjoint(collection1, collection2) returns true if collection1 and collection2 have no elements in common.
So in your case, boolean haveElementsInCommon = !Collections.disjoint(array1, array2) would do the job, at least, as I understand the question.
Please check Collections.disjoint() docs for details.
You could use simple functional style:
List<String> result = array1.stream()
.filter(array2::contains)
.collect(Collectors.roList());
Related
I have a set of sets of strings.
I need to find every possible combination of strings.
Any ideas of the best way to go about this?
The language is C#, but I'm not looking for a concrete implementation, just a general approach to the problem.
Put string into Listn and then create method that will generate random combination of your elements. Something like in:
How to make a combination of strings in C#?
EDIT:
Merge all Lists of string List into one long List of string.
List<List<String>> sets = new List<List<String>>();
List<String> allProducts = new List<String>();
List<String> set1 = new List<String>() { "one", "two", "three" };
List<String> set2 = new List<String>() { "111", "222", "333" };
sets.Add(set1);
sets.Add(set2);
foreach (var set in sets)
{
allProducts.AddRange(set);
}
Then perform operation on allProducts like in entry from above.
I want to sort a list of strings (with possibly duplicate entries) by using as ordering reference the order of the entries in another list. So, the following list is the list I want to sort
List<String> list = ['apple','pear','apple','x','x','orange','x','pear'];
And the list that specifies the order is
List<String> order = ['orange','apple','x','pear'];
And the output should be
List<String> result = ['orange','apple','apple','x','x','x','pear','pear'];
Is there a clean way of doing this?
I don't understand if I can use list's sort and compare with the following problem. I tried using map, iterable, intersection, etc.
There might be a more efficient way but at least you get the desired result:
main() {
List<String> list = ['apple','pear','apple','x','x','orange','x','pear'];
List<String> order = ['orange','apple','x','pear'];
list.sort((a, b) => order.indexOf(a).compareTo(order.indexOf(b)));
print(list);
}
Try it on DartPad
The closure passed to list.sort(...) is a custom comparer which instead of comparing the passed item, compares their position in order and returns the result.
Using a map for better lookup performance:
main() {
List<String> list = ['apple','pear','apple','x','x','orange','x','pear'];
List<String> orderList = ['orange','apple','x','pear'];
Map<String,int> order = new Map.fromIterable(
orderList, key: (key) => key, value: (key) => orderList.indexOf(key));
list.sort((a, b) => order[a].compareTo(order[b]));
print(list);
}
Try it on DartPad
I'm trying Sort on IList, I have tried different approach but none works
here is what my code looks like:
IList<IWebElement> listCountIdsUi = driver.FindElements(By.CssSelector("table#ct_ tr td:nth-of-type(1)"));
List<Int32> ui = new List<Int32>();
foreach (IWebElement option in listCountIdsUi)
{
if (!option.Text.ToString().StartsWith("Page"))
{
ui.Add(Convert.ToInt32(option.Text));
}
}
the only way I able to figured out is working with ArrayList
ArrayList al = new ArrayList(ui);
al.Sort();
is not possible using IList ?
OrderBy does NOT modify the underlying collection - it returns an IEnumerable that gives you the items in the order you ask for. So you have a few options:
Use List.Sort(), which does modify the underlying collection:
ui.Sort();
Store the sorted list in a new variable
var uiSorted = ui.OrderBy(s => s);
replace the existing reference
ui = ui.OrderBy(s => s).ToList();
Have you tried the List<T>.Sort method?
ui.Sort((a, b) => a.CompareTo(b));
var uiOrdered = ui.OrderBy(s => s).ToList() should be all you need to do.
I have two lists:
myObject object1 = new myObject(id = 1, title = "object1"};
myObject object2 = new myObject(id = 2, title = "object2"};
myObject object3 = new myObject(id = 3, title = "object3"};
//List 1
List<myObject> myObjectList = new List<myObject>{object1, object2, object3};
//List 2
List<int> idList = new List<int>{2, 3,5};
Now I need to get output as follows:
If a id is present in both the lists, I need to print "A",
if a id is present in list1 only, then I need to print "B",
...and if the id is present in list2 only, I need to print "C"
Can I use linq to achieve this?
I would simply use the inbuilt functions of Except and Intersect
List1.Intersect(List2) = "A"
List1.Except(List2) = "B"
List2.Except(List1) = "C"
There are plenty of resources online about how you could go about doing this, as one example (I didn't look into it too much), check out this link - Linq - Except one list with items in another
Hope this does the trick...
I have 2 lists of type List<Course>
List<Course> courseTakenFilterListByStatus
List<Course> TakenCoursesNotApplied
I want to find if courseTakenFilterListByStatus.Id is present in the TakenCoursesNotApplied list. Then I need to update Boolean property IsRequired to TRUE. If the value will not be there the property should be set to False.
I don't understand what it has to do with linq.
couldn't you just do the following:
foreach (Course course in courseTakenFilterListByStatus){
// Possibility 1
course.IsRequired = TakenCoursesNotApplied.Contains(course);
// Possibility 2
course.IsRequired = TakenCoursesNotApplied.Any(c => c.Id == course.Id);
}
Don't know if i understodd the logic right. Possibly you have to negate the assigment.
HTH Tobi