collections

Can a lambda be used to change a List's values in-place ( without creating a new list)?

巧了我就是萌 提交于 2019-12-18 18:53:42
问题 As per as per StackOverflow-On Topic, this is a specific programming problem, a software algorithm, about software tools commonly used by programmers and it is a practical, answerable problems that is unique to software development. ... I am trying to determine the correct way of changing all the values in a List using the new lambdas feature in the upcoming release of Java 8 without creating a **new** List . This pertains to times when a List is passed in by a caller and needs to have a

Java-like Collections in PHP

时光总嘲笑我的痴心妄想 提交于 2019-12-18 18:51:43
问题 I'm learning PHP5 (last time I checked PHP was in PHP4 days) and I'm glad to see that PHP5 OO is more Java-alike than the PHP4 one but there's still an issue that makes me feel quite unconfortable because of my Java background : ARRAYS. I'm reading "Proffesional PHP6" (Wrox) and It shows its own Collection implementation. I've found other clases like the one in http://aheimlich.dreamhosters.com/generic-collections/Collection.phps based on SPL. I've also found that there's some kind of

Getting a KeyValuePair<> directly from a Dictionary<>

一个人想着一个人 提交于 2019-12-18 18:50:10
问题 I have System.Collections.Generic.Dictionary<A, B> dict where A and B are classes, and an instance A a (where dict.ContainsKey(a) is true). Is it possible to get the KeyValuePair containing a directly from the Dictionary? Or do I need to create a new KeyValuePair: new KeyValuePair<A, B>(a, dict[a]) ? 回答1: You need to create a new KeyValuePair 1 - but bear in mind that KVP is a value type (a struct) anyway, so it's not like you're introducing a new inefficiency by doing this. Any method

Java iterator over an empty collection of a parameterized type

爷,独闯天下 提交于 2019-12-18 18:49:23
问题 In Java, I need to return an Iterator from my method. My data comes from another object which usually can give me an iterator so I can just return that, but in some circumstances the underlying data is null. For consistency, I want to return an "empty" iterator in that case so my callers don't have to test for null. I wanted to write something like: public Iterator<Foo> iterator() { if (underlyingData != null) { return underlyingData.iterator(); // works } else { return Collections.emptyList(

Filtering a Backbone Collection returns an array of Models

♀尐吖头ヾ 提交于 2019-12-18 18:38:22
问题 Sample Code: this.books = this.getBooksFromDatabase(); this.publishedBooks = this.books.filter(function(book) { return book.get("isPublished") === "1"; }); Here lies the problem: this.books.filter, returns an array of the models. I've tried wrapping the array, as such: var publishedBooks = _( this.books.filter(function(book) { return book.get("isPublished") === "1"; })) as recommended by this post: https://github.com/documentcloud/backbone/issues/120 But i still can't run things like:

java.lang.UnsupportedOperationException when combining two Sets

强颜欢笑 提交于 2019-12-18 18:37:07
问题 I have 2 different instances of HashMap I want to merge the keysets of both HashMaps; Code: Set<String> mySet = hashMap1.keySet(); mySet.addAll(hashMap2.keySet()); Exception: java.lang.UnsupportedOperationException at java.util.AbstractCollection.add(AbstractCollection.java:238) at java.util.AbstractCollection.addAll(AbstractCollection.java:322) I don't get a compile warning or error. From java doc this should work. Even if the added collection is also a set: boolean addAll(Collection c) Adds

Return type of iterator() method in java

北城余情 提交于 2019-12-18 17:28:23
问题 I am a new-comer in Java and in process of learning. I need a an answer of following question supported with valid theory. Consider the following line- Iterator itr = al.iterator(); where al is some collection object of ArrayList (class) type. I want to know what is here the return type of al.iterator() It is not definitely of a primitive data type, then it could be an object , but since every object belong to a class then it is of which class . Documentation and books etc says it has return

Return type of iterator() method in java

别说谁变了你拦得住时间么 提交于 2019-12-18 17:27:04
问题 I am a new-comer in Java and in process of learning. I need a an answer of following question supported with valid theory. Consider the following line- Iterator itr = al.iterator(); where al is some collection object of ArrayList (class) type. I want to know what is here the return type of al.iterator() It is not definitely of a primitive data type, then it could be an object , but since every object belong to a class then it is of which class . Documentation and books etc says it has return

How to intersect two different IEnumerable collections

坚强是说给别人听的谎言 提交于 2019-12-18 16:52:36
问题 i think this question has been asked before but i havent been able to deduce a clear answer. I am trying to find the best way (or a way) to intersect two completely different ienumerable collections. class A: int z1 int z2 int z3 string z4 class B: int j5 int j6 T j7 T j8 string j9 ..I want to intersect List<A> with List<B> on z2 == j6 . can this be done? 回答1: The question doesn't really make sense - what would the result type be? Intersections have to be performed on two sequences of the

Get index of an arraylist using property of an contained object in java [duplicate]

若如初见. 提交于 2019-12-18 16:34:32
问题 This question already has answers here : Searching in a ArrayList with custom objects for certain strings (9 answers) Closed 5 years ago . I'm having an list of Object type. In that I have one String property idNum. Now I want to get the index of the object in the list by passing the idNum. List<Object1> objList=new ArrayList<Object1>(); I don't know how to give objList.indexOf(// Don't know how to give here); Is it possible to do this without iterating the list. I want to use indexOf()