collections

python defaultdict: 0 vs. int and [] vs list

我只是一个虾纸丫 提交于 2019-12-18 11:41:23
问题 Is there any difference between passing int and lambda: 0 as arguments? Or between list and lambda: [] ? It looks like they do the same thing: from collections import defaultdict dint1 = defaultdict(lambda: 0) dint2 = defaultdict(int) dlist1 = defaultdict(lambda: []) dlist2 = defaultdict(list) for ch in 'abracadabra': dint1[ch] += 1 dint2[ch] += 1 dlist1[ch].append(1) dlist2[ch].append(1) print dint1.items() print dint2.items() print dlist1.items() print dlist2.items() ## -- Output: -- [('a',

C# / .NET equivalent for Java Collections.<T>emptyList()?

為{幸葍}努か 提交于 2019-12-18 11:41:05
问题 What's the standard way to get a typed, readonly empty list in C#, or is there one? ETA: For those asking "why?": I have a virtual method that returns an IList (or rather, post-answers, an IEnumerable ), and the default implementation is empty. Whatever the list returns should be readonly because writing to it would be a bug, and if somebody tries to, I want to halt and catch fire immediately, rather than wait for the bug to show up in some subtle way later. 回答1: Personally, I think this is

How to swap keys and values in a Map elegantly

无人久伴 提交于 2019-12-18 11:26:23
问题 I already know how to do it the hard way and got it working - iterating over entries and swapping "manually". But i wonder if, like so many tasks, this one can be solved in a more elegant way. I have read this post, unfortunately it does not feature elegant solutions. I also have no possibility to use any fancy Guava BiMaps or anything outside the jdk (project stack is already defined). I can assume that my map is bijective, btw :) 回答1: The standard API / Java runtime doesn't offer a bi

Reload collection view data from another view class

荒凉一梦 提交于 2019-12-18 10:57:36
问题 I have two containers in a view. The top one has a collection view. I want to update my collection view from a button when a button is hit from the below container. My button is also changing the value of an array, which my collection view uses. I thought didSet would do the job but unfortunately did not work. Top: class TopViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { @IBOutlet weak var favoritesCV: UICollectionView! var myFavorites = [] { didSet {

Java: Composite key in hashmaps

本小妞迷上赌 提交于 2019-12-18 10:50:52
问题 I would like to store a group of objects in a hashmap , where the key shall be a composite of two string values. is there a way to achieve this? i can simply concatenate the two strings , but im sure there is a better way to do this. 回答1: You could have a custom object containing the two strings: class StringKey { private String str1; private String str2; } Problem is, you need to determine the equality test and the hash code for two such objects. Equality could be the match on both strings

Is there a typescript List<> and/or Map<> class/library? [closed]

大城市里の小女人 提交于 2019-12-18 10:47:40
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . Did they add a runtime List<> and/or Map<> type class to typepad 1.0? And if not, is there a solid library out there someone wrote that provides this functionality? And in the case of List<>, is there a linked list where the elements in the list have the next/prev property? We need a list where from an element

when to use Set vs. Collection?

半城伤御伤魂 提交于 2019-12-18 10:44:45
问题 Is there any practical difference between a Set and Collection in Java, besides the fact that a Collection can include the same element twice? They have the same methods. (For example, does Set give me more options to use libraries which accept Set s but not Collection s?) edit: I can think of at least 5 different situations to judge this question. Can anyone else come up with more? I want to make sure I understand the subtleties here. designing a method which accepts an argument of Set or

How to find Max Date in List<Object>?

北城余情 提交于 2019-12-18 10:32:17
问题 Consider a class User public class User{ int userId; String name; Date date; } Now I have a List<User> of size 20, how can I find the max date in the list without using manual iterator? 回答1: Since you are asking for lambdas, you can use the following syntax with Java 8: Date maxDate = list.stream().map(u -> u.date).max(Date::compareTo).get(); or, if you have a getter for the date: Date maxDate = list.stream().map(User::getDate).max(Date::compareTo).get(); 回答2: Comparator<User> cmp = new

why HashMap Values are not cast in List?

半腔热情 提交于 2019-12-18 10:17:58
问题 I'm putting values into the hashmap which is of the form, Map<Long, Double> highLowValueMap=new HashMap<Long, Double>(); highLowValueMap.put(1l, 10.0); highLowValueMap.put(2l, 20.0); I want to create a list by using values() method of map. List<Double> valuesToMatch=new ArrayList<>(); valuesToMatch=(List<Double>) highLowValueMap.values(); or List<Double> valuesToMatch=(List<Double>) highLowValueMap.values(); However, it throws an exception: Exception in thread "main" java.lang

Can Java 8 Streams operate on an item in a collection, and then remove it?

坚强是说给别人听的谎言 提交于 2019-12-18 10:17:52
问题 Like just about everyone, I'm still learning the intricacies (and loving them) of the new Java 8 Streams API. I have a question concerning usage of streams. I'll provide a simplified example. Java Streams allows us to take a Collection , and use the stream() method on it to receive a stream of all of its elements. Within it, there are a number of useful methods, such as filter() , map() , and forEach() , which allow us to use lambda operations on the contents. I have code that looks something