collections

What is/are the Python equivalent(s) to the Java Collections Framework?

Deadly 提交于 2021-02-18 07:22:47
问题 The Java Collections Framework is like the C++ Standard Template Library: "a unified architecture for representing and manipulating collections (objects that group multiple elements into a single unit)." http://java.sun.com/docs/books/tutorial/collections/intro/index.html 回答1: Other than the built-ins you might what to check out collections. >>> import collections >>> dir(collections) ['Callable', 'Container', 'Hashable', 'ItemsView', 'Iterable', 'Iterator', 'KeysView', 'Mapping',

The type T is not generic; it cannot be parameterized with arguments <?> error in a generic function

 ̄綄美尐妖づ 提交于 2021-02-18 06:05:33
问题 I want to create a generic function that takes any Map & a String key, if the key is not present in the map, then it should create a new instance of the Value Type (which is passed) & put it in the map & then return it. Here is my implementation public <T> T getValueFromMap(Map<String, T> map, String key, Class<T> valueClass){ T value = map.get(key); if (value == null){ try { value = valueClass.newInstance(); } catch (InstantiationException e) { // TODO Auto-generated catch block e

Check if an array exists in a HashSet<int[]>

ⅰ亾dé卋堺 提交于 2021-02-17 05:05:09
问题 How do I check if an array exists in a HashSet ? For example: int[] a = new int[]{0, 0}; HashSet<int[]> set = new HashSet<>(); set.add(a); Then: int[] b = new int[]{0, 0}; set.contains(b); // ===> true 回答1: Using arrays int[] a = new int[] { 0, 0 }; HashSet<int[]> set = new HashSet<>(); set.add(a); int[] b = new int[] { 0, 0 }; boolean contains = set.stream().anyMatch(c -> Arrays.equals(c, b)); System.out.println("Contains? " + contains); Output: Contains? true It doesn’t exploit the fast

Unable to constrain generic type

不羁岁月 提交于 2021-02-16 21:19:14
问题 I can't figure out what's happening here. I'm building a wrapper for a Dictionary collection. The idea is that, when the size of the collection is small, it will use a normal in-memory Dictionary; but, when a threshold number of items is reached, it will internally switch to an on-disk Dictionary (I'm using the ManagedEsent PersistentDictionary class). A snippet of the on-disk version is below. When compiling, it fails with the following error: "The type 'T_KEY' cannot be used as type

Unable to constrain generic type

萝らか妹 提交于 2021-02-16 21:17:08
问题 I can't figure out what's happening here. I'm building a wrapper for a Dictionary collection. The idea is that, when the size of the collection is small, it will use a normal in-memory Dictionary; but, when a threshold number of items is reached, it will internally switch to an on-disk Dictionary (I'm using the ManagedEsent PersistentDictionary class). A snippet of the on-disk version is below. When compiling, it fails with the following error: "The type 'T_KEY' cannot be used as type

Sort java.util.Deque

浪子不回头ぞ 提交于 2021-02-16 21:06:44
问题 I want to sort the contents of my Deque<Card> collection using the Card class' getRealValue() method. public class Card implements Comparable<Card> { private final int value; private final CardType cardType; public Card(int value, CardType cardType) { this.value = value; this.cardType = cardType; } public int getRealValue() { int realValue = this.value == 1 ? 52 : 0; return realValue + this.value * 4 + this.cardType.ordinal(); } public int compareTo(Card o) { return this.getRealValue() - o

Sort java.util.Deque

懵懂的女人 提交于 2021-02-16 21:06:25
问题 I want to sort the contents of my Deque<Card> collection using the Card class' getRealValue() method. public class Card implements Comparable<Card> { private final int value; private final CardType cardType; public Card(int value, CardType cardType) { this.value = value; this.cardType = cardType; } public int getRealValue() { int realValue = this.value == 1 ? 52 : 0; return realValue + this.value * 4 + this.cardType.ordinal(); } public int compareTo(Card o) { return this.getRealValue() - o

Sort java.util.Deque

不问归期 提交于 2021-02-16 21:04:57
问题 I want to sort the contents of my Deque<Card> collection using the Card class' getRealValue() method. public class Card implements Comparable<Card> { private final int value; private final CardType cardType; public Card(int value, CardType cardType) { this.value = value; this.cardType = cardType; } public int getRealValue() { int realValue = this.value == 1 ? 52 : 0; return realValue + this.value * 4 + this.cardType.ordinal(); } public int compareTo(Card o) { return this.getRealValue() - o

Remove elements from Dictionary<Key, Item>

 ̄綄美尐妖づ 提交于 2021-02-16 15:51:25
问题 I have a Dictionary, where items are (for example): "A", 4 "B", 44 "bye", 56 "C", 99 "D", 46 "6672", 0 And I have a List: "A" "C" "D" I want to remove from my dictionary all the elements whose keys are not in my list, and at the end my dictionary will be: "A", 4 "C", 99 "D", 46 How can I do? 回答1: It's simpler to construct new Dictionary to contain elements that are in the list: List<string> keysToInclude = new List<string> {"A", "B", "C"}; var newDict = myDictionary .Where(kvp=>keysToInclude

Sorting by two fields of object. Java

风流意气都作罢 提交于 2021-02-15 07:45:54
问题 I want to sort by date and name. For example I have date like this 2019 01 01 "CCCC" 2019 02 01 "Aaaa" 2019 03 01 "CCC" 2019 02 01 "BBBB" 2019 03 01 "Aaaa" 2019 01 01 "Aaaa" I need to sort by month (and year) and alphabet, for example it must be like this: 2019 01 01 "Aaaa" 2019 01 01 "CCCC" 2019 02 01 "Aaaa" 2019 02 01 "BBBB" 2019 03 01 "Aaaa" 2019 03 01 "CCC" I have written code like this: Collections.sort(mainList, new Comparator<DocSet>() { public int compare(DocSet o1, DocSet o2) {