collections

What is the purpose of collections.ChainMap?

好久不见. 提交于 2019-12-17 06:32:45
问题 In Python 3.3 a ChainMap class was added to the collections module: A ChainMap class is provided for quickly linking a number of mappings so they can be treated as a single unit. It is often much faster than creating a new dictionary and running multiple update() calls. Example: >>> from collections import ChainMap >>> x = {'a': 1, 'b': 2} >>> y = {'b': 10, 'c': 11} >>> z = ChainMap(y, x) >>> for k, v in z.items(): print(k, v) a 1 c 11 b 10 It was motivated by this issue and made public by

Does C# have a way of giving me an immutable Dictionary?

删除回忆录丶 提交于 2019-12-17 06:25:28
问题 Is there anything built into the core C# libraries that can give me an immutable Dictionary? Something along the lines of Java's : Collections.unmodifiableMap(myMap); And just to clarify, I am not looking to stop the keys / values themselves from being changed, just the structure of the Dictionary. I want something that fails fast and loud if any of IDictionary's mutator methods are called ( Add, Remove, Clear ). 回答1: No, but a wrapper is rather trivial: public class ReadOnlyDictionary<TKey,

Why do C# collection initializers work this way?

倖福魔咒の 提交于 2019-12-17 06:25:26
问题 I was looking at C# collection initializers and found the implementation to be very pragmatic but also very unlike anything else in C# I am able to create code like this: using System; using System.Collections; class Program { static void Main() { Test test = new Test { 1, 2, 3 }; } } class Test : IEnumerable { public IEnumerator GetEnumerator() { throw new NotImplementedException(); } public void Add(int i) { } } Since I have satisfied the minimum requirements for the compiler (implemented

What is the Simplest Way to Reverse an ArrayList?

时光怂恿深爱的人放手 提交于 2019-12-17 06:22:04
问题 What is the simplest way to reverse this ArrayList? ArrayList<Integer> aList = new ArrayList<>(); //Add elements to ArrayList object aList.add("1"); aList.add("2"); aList.add("3"); aList.add("4"); aList.add("5"); while (aList.listIterator().hasPrevious()) Log.d("reverse", "" + aList.listIterator().previous()); 回答1: Collections.reverse(aList); Example (Reference): ArrayList aList = new ArrayList(); //Add elements to ArrayList object aList.add("1"); aList.add("2"); aList.add("3"); aList.add("4"

Is there an accepted Java equivalent to Python's zip()? [duplicate]

*爱你&永不变心* 提交于 2019-12-17 06:14:07
问题 This question already has answers here : How to most elegantly iterate through parallel collections? (8 answers) Closed 2 years ago . I've got two List objects and I want to pair them up, just like the zip() function in Python. I'm pretty sure this isn't available in the JDK, but is there something like this in a fairly widespread library, similar to Apache Commons Collections? Thanks. 回答1: Functional Java has zip, zipWith and zipIndex the way you would expect from Haskell or Scala. (Indeed,

How to convert Set to Array?

别等时光非礼了梦想. 提交于 2019-12-17 05:21:12
问题 Set seems like a nice way to create Arrays with guaranteed unique elements, but it does not expose any good way to get properties, except for generator [Set].values, which is called in an awkward way of mySet.values.next() . This would have been ok, if you could call map and similar functions on Sets. But you cannot do that, as well. I've tried Array.from , but seems to be converting only array-like (NodeList and TypedArrays ?) objects to Array. Another try: Object.keys does not work for Sets

How do I override List<T>'s Add method in C#?

天大地大妈咪最大 提交于 2019-12-17 05:03:31
问题 I am currently looking to make my own collection, which would be just like a regular list, except that it would only hold 10 items. If an item was added when there were already 10 items in the list, then the first item would be removed before the new item was appended. What I want to do is create a class that extends System.Collections.Generic.List<T> , and then modifies the Add(T item) method to include the functionality which removes the first item if necessary. 回答1: First, you can't

Empty an ArrayList or just create a new one and let the old one be garbage collected? [duplicate]

爷,独闯天下 提交于 2019-12-17 04:59:26
问题 This question already has answers here : Better practice to re-instantiate a List or invoke clear() (4 answers) Closed 5 years ago . What are the advantages and disadvantages of emptying a collection (in my case its an ArrayList) vs creating a new one (and letting the garbage collector clear the old one). Specifically, I have an ArrayList<Rectangle> called list . When a certain condition occurs, I need to empty list and refill it with other contents. Should I call list.clear() or just make a

How do I keep the iteration order of a List when using Collections.toMap() on a stream?

做~自己de王妃 提交于 2019-12-17 04:58:16
问题 I am creating a Map from a List as follows: List<String> strings = Arrays.asList("a", "bb", "ccc"); Map<String, Integer> map = strings.stream() .collect(Collectors.toMap(Function.identity(), String::length)); I want to keep the same iteration order as was in the List . How can I create a LinkedHashMap using the Collectors.toMap() methods? 回答1: The 2-parameter version of Collectors.toMap() uses a HashMap : public static <T, K, U> Collector<T, ?, Map<K,U>> toMap( Function<? super T, ? extends K

Collections.synchronizedList and synchronized

别说谁变了你拦得住时间么 提交于 2019-12-17 04:45:32
问题 List<String> list = Collections.synchronizedList(new ArrayList<String>()); synchronized (list) { list.add("message"); } Is the block "synchronized (list){} " really need here ? 回答1: You don't need to synchronize as you put in your example. HOWEVER, very important, you need to synchronize around the list when you iterate it (as noted in the Javadoc): It is imperative that the user manually synchronize on the returned list when iterating over it: List list = Collections.synchronizedList(new