collections

Scala pattern matching on sequences other than Lists

时光总嘲笑我的痴心妄想 提交于 2019-12-17 15:38:02
问题 I have the following code which recursively operates on each element within a List def doMatch(list: List[Int]): Unit = list match { case last :: Nil => println("Final element.") case head :: tail => println("Recursing..."); doMatch(tail) } Now, ignoring that this functionality is available through filter() and foreach() , this works just fine. However, if I try to change it to accept any Seq[Int] , I run into problems: Seq doesn't have ::, but it does have +:, which as I understand is

Checking if a collection is empty in Java: which is the best method?

ε祈祈猫儿з 提交于 2019-12-17 15:29:57
问题 I have two ways of checking if a List is empty or not if (CollectionUtils.isNotEmpty(listName)) and if (listName != null && listName.size() != 0) My arch tells me that the former is better than latter. But I think the latter is better. Can anyone please clarify it? 回答1: You should absolutely use isEmpty() . Computing the size() of an arbitrary list could be expensive. Even validating whether it has any elements can be expensive, of course, but there's no optimization for size() which can't

Clojure: cons (seq) vs. conj (list)

夙愿已清 提交于 2019-12-17 15:03:09
问题 I know that cons returns a seq and conj returns a collection. I also know that conj "adds" the item to the optimal end of the collection, and cons always "adds" the item to the front. This example illustrates both of these points: user=> (conj [1 2 3] 4) ; returns a collection [1 2 3 4] user=> (cons 4 [1 2 3]) ; returns a seq (4 1 2 3) For vectors, maps, and sets these differences make sense to me. However, for lists they seem identical. user=> (conj (list 3 2 1) 4) ; returns a list (4 3 2 1)

Clojure: cons (seq) vs. conj (list)

佐手、 提交于 2019-12-17 15:00:16
问题 I know that cons returns a seq and conj returns a collection. I also know that conj "adds" the item to the optimal end of the collection, and cons always "adds" the item to the front. This example illustrates both of these points: user=> (conj [1 2 3] 4) ; returns a collection [1 2 3 4] user=> (cons 4 [1 2 3]) ; returns a seq (4 1 2 3) For vectors, maps, and sets these differences make sense to me. However, for lists they seem identical. user=> (conj (list 3 2 1) 4) ; returns a list (4 3 2 1)

How can I obtain all the possible combination of a subset?

坚强是说给别人听的谎言 提交于 2019-12-17 14:55:02
问题 Consider this List<string> List<string> data = new List<string>(); data.Add("Text1"); data.Add("Text2"); data.Add("Text3"); data.Add("Text4"); The problem I had was: how can I get every combination of a subset of the list? Kinda like this: #Subset Dimension 4 Text1;Text2;Text3;Text4 #Subset Dimension 3 Text1;Text2;Text3; Text1;Text2;Text4; Text1;Text3;Text4; Text2;Text3;Text4; #Subset Dimension 2 Text1;Text2; Text1;Text3; Text1;Text4; Text2;Text3; Text2;Text4; #Subset Dimension 1 Text1; Text2

Arrays.asList give UnsupportedOperationException [duplicate]

做~自己de王妃 提交于 2019-12-17 14:50:10
问题 This question already has answers here : UnsupportedOperationException when trying to remove from the list returned by Array.asList (6 answers) Closed 6 months ago . The List returned by Arrays.asList can't be modified by using methods such as add or remove . But if you pass it to the Collections.sort method, it can sort the array without any problem (I expected an exception). This seems like a very inconsistent behaviour. So what are the allowed operations on the List , which returned by

Perform operation on n random distinct elements from Collection using Streams API

我的未来我决定 提交于 2019-12-17 12:47:40
问题 I'm attempting to retrieve n unique random elements for further processing from a Collection using the Streams API in Java 8, however, without much or any luck. More precisely I'd want something like this: Set<Integer> subList = new HashSet<>(); Queue<Integer> collection = new PriorityQueue<>(); collection.addAll(Arrays.asList(1,2,3,4,5,6,7,8,9)); Random random = new Random(); int n = 4; while (subList.size() < n) { subList.add(collection.get(random.nextInt())); } sublist.forEach(v -> v

How to sort ArrayList using Comparator? [duplicate]

早过忘川 提交于 2019-12-17 12:04:31
问题 This question already has answers here : Sort ArrayList of custom Objects by property (25 answers) Closed 5 years ago . I have a Class Student that Implements a static method public static Comparator<Student> getCompByName() that returns a new comparator object for Student that compares 2 Students objects by the attribute 'name'. I need to now test this by sorting a students ArrayList by 'name' using my function getCompByName(). Here is my Comparator method in my Student class. public static

How to sort ArrayList using Comparator? [duplicate]

不问归期 提交于 2019-12-17 12:04:09
问题 This question already has answers here : Sort ArrayList of custom Objects by property (25 answers) Closed 5 years ago . I have a Class Student that Implements a static method public static Comparator<Student> getCompByName() that returns a new comparator object for Student that compares 2 Students objects by the attribute 'name'. I need to now test this by sorting a students ArrayList by 'name' using my function getCompByName(). Here is my Comparator method in my Student class. public static

Automatically sorted by values map in Java

馋奶兔 提交于 2019-12-17 10:57:33
问题 I need to have an automatically sorted-by-values map in Java - so that It keeps being sorted at any time while I'm adding new key-value pairs or update the value of an existing key-value pair, or even delete some entry. Please also have in mind that this map is going to be really big (100's of thousands, or even 10's of millions of entries in size). So basically I'm looking for the following functionality: Supposed that we had a class 'SortedByValuesMap' that implements the aforementioned