collections

Comparator sort matching String at first, and rest using default sorting order

非 Y 不嫁゛ 提交于 2021-02-04 21:06:56
问题 String currency = EUR; List<Payment> payments = #has payments, with one field being Currency; //This is not it: payments.sort(Comparator.comparing(o -> o.getCurrency().equals(currency)); I want all the payments which currency equals to variable currency in my case EUR to be at the top of the list, others order stays the same. And if there is nothing that equals with the variable currency then sort by default value which for example is USD. I know this can be done other ways, but this is kind

Comparator sort matching String at first, and rest using default sorting order

人盡茶涼 提交于 2021-02-04 21:06:25
问题 String currency = EUR; List<Payment> payments = #has payments, with one field being Currency; //This is not it: payments.sort(Comparator.comparing(o -> o.getCurrency().equals(currency)); I want all the payments which currency equals to variable currency in my case EUR to be at the top of the list, others order stays the same. And if there is nothing that equals with the variable currency then sort by default value which for example is USD. I know this can be done other ways, but this is kind

How to compare two ArrayList and get list1 with filter using java streams

百般思念 提交于 2021-02-04 19:36:07
问题 I have two lists list1 & list2 of type List Term{ long sId; int rowNum; long psid; String name; } List<Term> list1 = new ArrayList<>(); List<Term> list2 = new ArrayList<>(); I want to return all the items from list1 where (list1.psid != list2.psid). I tried this but its not working public List<Term> getFilteredRowNum(List<Term> list1, List<Term> list2) { List<Long> psid = list2.stream().map(x -> x.getPsid()).collect(Collectors.toList()); return list1.stream().filter(x -> !psid.contains(x

How to split up an Iterator?

試著忘記壹切 提交于 2021-02-04 16:37:50
问题 How to split an iterator into a prefix with duplicates and the rest ? For instance, def splitDupes(it: Iterator[Int]): (Iterator[Int], Iterator[Int]) = ??? val (xs, ys) = splitDupes(List(1, 1, 1, 2, 3, 4, 5).iterator) xs.toList // List(1, 1, 1) ys.toList // List(2, 3, 4, 5) val (xs, ys) = splitDupes(List(1, 2, 3, 4, 5).iterator) xs.toList // List(1) ys.toList // List(2, 3, 4, 5) val (xs, ys) = splitDupes(List(1, 1, 1, 1, 1).iterator) xs.toList // List(1, 1, 1, 1, 1) ys.toList // List() val

How to split up an Iterator?

情到浓时终转凉″ 提交于 2021-02-04 16:37:23
问题 How to split an iterator into a prefix with duplicates and the rest ? For instance, def splitDupes(it: Iterator[Int]): (Iterator[Int], Iterator[Int]) = ??? val (xs, ys) = splitDupes(List(1, 1, 1, 2, 3, 4, 5).iterator) xs.toList // List(1, 1, 1) ys.toList // List(2, 3, 4, 5) val (xs, ys) = splitDupes(List(1, 2, 3, 4, 5).iterator) xs.toList // List(1) ys.toList // List(2, 3, 4, 5) val (xs, ys) = splitDupes(List(1, 1, 1, 1, 1).iterator) xs.toList // List(1, 1, 1, 1, 1) ys.toList // List() val

Why is OrderBy which returns IOrderedEnumerable<T> much faster than Sort?

a 夏天 提交于 2021-02-04 14:30:14
问题 This is a follow up of this excellent question C# Sort and OrderBy comparison. I will use the same example: List<Person> persons = new List<Person>(); persons.Add(new Person("P005", "Janson")); persons.Add(new Person("P002", "Aravind")); persons.Add(new Person("P007", "Kazhal")); The methods in contention are: persons.Sort((p1, p2) => string.Compare(p1.Name, p2.Name, true)); //and persons.OrderBy(n => n.Name); Let me start by saying that I understand there isn't any significant performance

Why is OrderBy which returns IOrderedEnumerable<T> much faster than Sort?

♀尐吖头ヾ 提交于 2021-02-04 14:29:06
问题 This is a follow up of this excellent question C# Sort and OrderBy comparison. I will use the same example: List<Person> persons = new List<Person>(); persons.Add(new Person("P005", "Janson")); persons.Add(new Person("P002", "Aravind")); persons.Add(new Person("P007", "Kazhal")); The methods in contention are: persons.Sort((p1, p2) => string.Compare(p1.Name, p2.Name, true)); //and persons.OrderBy(n => n.Name); Let me start by saying that I understand there isn't any significant performance

Shallow copy of a hashset

社会主义新天地 提交于 2021-02-04 11:54:02
问题 Whats the best way of doing it? var set2 = new HashSet<reference_type>(); Traverse the set with a foreach like this. foreach (var n in set) set2.Add(n); Or use something like union like this. set2 = set.UnionWith(set); // all the elements 回答1: Use the constructor: HashSet<type> set2 = new HashSet<type>(set1); Personally I wish LINQ to Objects had a ToHashSet extension method as it does for List and Dictionary . It's easy to create your own of course: public static HashSet<T> ToHashSet<T>(this

Shallow copy of a hashset

喜夏-厌秋 提交于 2021-02-04 11:53:26
问题 Whats the best way of doing it? var set2 = new HashSet<reference_type>(); Traverse the set with a foreach like this. foreach (var n in set) set2.Add(n); Or use something like union like this. set2 = set.UnionWith(set); // all the elements 回答1: Use the constructor: HashSet<type> set2 = new HashSet<type>(set1); Personally I wish LINQ to Objects had a ToHashSet extension method as it does for List and Dictionary . It's easy to create your own of course: public static HashSet<T> ToHashSet<T>(this

assertAlmostEqual in Python unit-test for collections of floats

纵然是瞬间 提交于 2021-02-04 09:31:05
问题 The assertAlmostEqual(x, y) method in Python's unit testing framework tests whether x and y are approximately equal assuming they are floats. The problem with assertAlmostEqual() is that it only works on floats. I'm looking for a method like assertAlmostEqual() which works on lists of floats, sets of floats, dictionaries of floats, tuples of floats, lists of tuples of floats, sets of lists of floats, etc. For instance, let x = 0.1234567890 , y = 0.1234567891 . x and y are almost equal because