collections

c# sort a list by a column alphabetically

耗尽温柔 提交于 2019-12-31 04:21:34
问题 I have a class defined and I write records to this class to a List. Having trouble sorting the list before I write an error report. I'm trying to sort the list alphabetically by the 'finderror' type before the error report is written so that the list is sorted and more organized in the error report. Here is the class: public class types { public types() { } public string person { get; set; } public string state { get; set; } public string phone# { get; set; } public string finderror { get;

ArrayList<String>() vs arrayListOf<String>()

不打扰是莪最后的温柔 提交于 2019-12-31 03:48:27
问题 I was going through some Kotlin basics and found two syntaxes. ArrayList<String>() And arrayListOf<String>() What is the difference between these two as both are part of Kotlin.Collections ? 回答1: arrayListOf<T>() is mainly there for your convenience. vararg -functions usually come with a (sometimes negligible) performance impact and switching between the arrayListOf(someElements...) and arrayListOf() without that convenience method would basically delegate that problem to you as a programmer.

Writing a synchronized thread-safety wrapper for NavigableMap

倖福魔咒の 提交于 2019-12-31 02:57:09
问题 java.util.Collections currently provide the following utility methods for creating synchronized wrapper for various collection interfaces: synchronizedCollection(Collection<T> c) synchronizedList(List<T> list) synchronizedMap(Map<K,V> m) synchronizedSet(Set<T> s) synchronizedSortedMap(SortedMap<K,V> m) synchronizedSortedSet(SortedSet<T> s) Analogously, it also has 6 unmodifiedXXX overloads. The glaring omission here are the utility methods for NavigableMap<K,V>. It's true that it extends

logical inconsistence between Set and SortedSet interfaces in Java [closed]

萝らか妹 提交于 2019-12-31 00:44:16
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I noticed a logical inconsistence between Set and SortedSet interfaces in Java. SortedSet recognizes different objects (by equal()

ordered map implementation

大兔子大兔子 提交于 2019-12-30 22:56:53
问题 I'm looking for a Map implementation that iterates over the key-value pairs in the order in which they were added. For example Map orderedMap = // instantiation omitted for obvious reasons :) orderMap.put(4, "d"); orderMap.put(10, "y"); orderMap.put(2, "b"); for (Map.Entry entry : orderedMap.entrySet()) { System.out.println(entry.getKey() + ", " + entry.getValue()); } Will always print 4, d 10, y 2, b I'm using Java 5.0. Thanks, Don 回答1: That's LinkedHashMap 来源: https://stackoverflow.com

Clearing a private collection or setting it to null?

假装没事ソ 提交于 2019-12-30 18:26:16
问题 I have a mutable class which has a private List<T> field inside. In the Reset() method of my class, should I clear the list using its Clear() method or just assign its field a new list? Note that the list is not public and is only used by the class itself. So assigning a new list should make the old one unreachable. Since the Clear() method is an O(n) operation, I wonder what is the downside of just assigning a new list over it. 回答1: The only downside I can think of is if you need to use the

How create a new map from the values in an existing map

*爱你&永不变心* 提交于 2019-12-30 15:02:07
问题 Having the next original map: G1=[7,8,45,6,9] G2=[3,9,34,2,1,65] G3=[6,5,9,1,67,5] Where G1, G2 and G3 are groups of people's ages, How can I create a new map like this: 45=[7,8,45,6,9] 65=[3,9,34,2,1,65] 67=[6,5,9,1,67,5] Where the new keys are the max people's age in each group. I have tried this: Map<Integer, List<Integer>> newMap = originalMap.entrySet().stream() .collect(Collectors.toMap(Collections.max(x -> x.getValue()), x -> x.getValue())); But the compiler say me: "The target type of

Reordering a collection according to a related list of ids

亡梦爱人 提交于 2019-12-30 12:39:19
问题 I have a Collection (unordered) of objects with an id property, and an (ordered) List of ids . The id list is not sorted . I'd like to create a List of the objects in my Collection, ordered according to the List of ids. I didn't see a method for this in Guava or Apache Commons - but that's exactly what I'm looking for. A library function with a good implementation. 回答1: It sounds like your id list has its own order; you're not just using the natural order, right? Here's the Guava solution:

c# collection inheritance

安稳与你 提交于 2019-12-30 11:43:07
问题 Is there a collection in c# that supports the inheritance like concept that objects can have of appearing to include all the elements from another as well as themselves? For example: HashSet<animal> animals = new HashSet<animal>(); HashSet<dog> dogs = new HashSet<dog>(); animals.also_appears_to_include(dogs); So if I for example added 2 elements to dogs and 1 to animals, then looked at how many elements animals has I would see 3. Alternatively I could put references to the above mentioned 3

Mapping Java collections which contains super- and sub-types with JAXB

我怕爱的太早我们不能终老 提交于 2019-12-30 10:59:12
问题 I'm trying to produce something like this with JAXB: <person> <firstName>Foo</firstName> <lastName>Bar</lastName> <identities> <green id="greenId"> <some_elements.... </green> <blue id="blueId"/> </identities> The child elements of <identities> all stem from a common super-class. In Java it's like this: @XmlRootElement(name = "person") public class Person { public String firstName; public String lastName; @XmlElementWrapper(name = "identities") public Set<Identity> identities = new HashSet