collections

Java collections maintaining insertion order

旧时模样 提交于 2019-12-18 10:16:30
问题 Why do some collection data structures not maintain the order of insertion? What is the special thing achieved compared to maintaining order of insertion? Do we gain something if we don't maintain the order? 回答1: Performance. If you want the original insertion order there are the LinkedXXX classes, which maintain an additional linked list in insertion order. Most of the time you don't care, so you use a HashXXX, or you want a natural order, so you use TreeXXX. In either of those cases why

List<BusinessObject> or BusinessObjectCollection?

做~自己de王妃 提交于 2019-12-18 10:13:32
问题 Prior to C# generics, everyone would code collections for their business objects by creating a collection base that implemented IEnumerable IE: public class CollectionBase : IEnumerable and then would derive their Business Object collections from that. public class BusinessObjectCollection : CollectionBase Now with the generic list class, does anyone just use that instead? I've found that I use a compromise of the two techniques: public class BusinessObjectCollection : List<BusinessObject> I

shortcut for creating a Map from a List in groovy?

拥有回忆 提交于 2019-12-18 10:11:52
问题 I'd like some sorthand for this: Map rowToMap(row) { def rowMap = [:]; row.columns.each{ rowMap[it.name] = it.val } return rowMap; } given the way the GDK stuff is, I'd expect to be able to do something like: Map rowToMap(row) { row.columns.collectMap{ [it.name,it.val] } } but I haven't seen anything in the docs... am I missing something? or am I just way too lazy? 回答1: I've recently came across the need to do exactly that: converting a list into a map. This question was posted before Groovy

Best practice to validate null and empty collection in Java

大兔子大兔子 提交于 2019-12-18 09:55:20
问题 I want to verify whether a collection is empty and null . Could anyone please let me know the best practice. Currently, I am checking as below: if (null == sampleMap || sampleMap.isEmpty()) { // do something } else { // do something else } 回答1: If you use the Apache Commons Collections library in your project, you may use the CollectionUtils.isEmpty and MapUtils.isEmpty() methods which respectively check if a collection or a map is empty or null (i.e. they are "null-safe"). The code behind

get the count of elements of tuples of your own…not just the range or sequence

时间秒杀一切 提交于 2019-12-18 09:55:11
问题 The below code is running for first three elements of the tuple of this list SS1=[(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)] from collections import Counter c = Counter(elem[0:3] for elem in SS1) for k, v in c.items(): if (v > 0): print(k,v) and the output is: (1, 2, 3) 3 (1, 2, 4) 1 (1, 3, 4) 1 (2, 3, 4) 1 But my expectation is not just for first three tuple...i want the counter for tuple (0,2,3) or tuple (1,2,4) likewise i can pass

Java HashMap Hashing function [closed]

ぐ巨炮叔叔 提交于 2019-12-18 09:46:11
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I was going through Java's HashMap hash() implementation , its like below final int hash(Object k) { // some checks h ^= k.hashCode(); // This function ensures that hashCodes that differ only by // constant

How can I sort an arraylist without using collections.sort()? [duplicate]

心已入冬 提交于 2019-12-18 09:45:33
问题 This question already has answers here : sorting integers in order lowest to highest java (7 answers) Closed 5 years ago . I have been looking for a while for a way to sort an arraylist without using collections.sort as my own logic is flawed and I have been having a lot of trouble. I need to sort it in a way that I can use a method I created that basically does what collections.swap does in order to completely sort an arraylist. Here is my code: public static void mySort(ArrayList<Double>

How to store values from excel to some collection in java [closed]

非 Y 不嫁゛ 提交于 2019-12-18 09:37:48
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . i have a excel file like the following, **Method Name** **Status Code** **user** **password** getLoggedinUserDetails 400 anto test createRequisition 400 mayank hexgen excelMDM 400 xxxxx hexgen createOrder 400

comparing two collections for comparing two text files for additions, deletions, modifications

自古美人都是妖i 提交于 2019-12-18 09:36:33
问题 I have two collections as below which hold IDs for Students. The ids are Strings in the format 111-1111. e.g. of ids 221-2534, 215-6365, etc. Collection<String> newKeys = new ArrayList<String>(); Collection<String> oldKeys = new ArrayList<String>(); The ids are in a fixed format file along with other data. That is first 8 char ids, next 10 char name, next 10 char addr, etc. I am reading ids into collection as below: String oldFile = "C:\\oldFile.dat"; String newFile = "C:\\newFile.dat";

How to move contents of one ArrayList to another?

心不动则不痛 提交于 2019-12-18 08:52:44
问题 Is there a way to move the entire contents of an ArrayList to another instance of ArrayList in O(1)? I.e.: only the reference to the backing array is passed from one instance to the other (elements are not copied one by one). For example: ArrayList<String> a = new ArrayList<>(Arrays.asList("A", "B", "C")); ArrayList<String> b = new ArrayList<>(); a.moveContentsTo(b); // 'a' is now empty, while 'b' contains everything that 'a' did before and 'a != b' // It is desired that the 'moveContentsTo'