Comments (9)
Thanks for contributing this! I'll look it over later.
Original comment by [email protected]
on 19 Dec 2007 at 9:13
- Added labels: Type-Enhancement
- Removed labels: Type-Defect
from google-collections.
I apologize for taking so long to examine this.
Google's internal code base has a Pair class, which we didn't include in the
current
release of Google Collections but may in the future. Your Tuple class expands
Pair in
two dimensions: supporting tuples of 1-5 elements and including additional
methods.
While the tuple syntax is really nice in Python, I'm reluctant to add tuples of
more
than two values to Java. A value class would generally lead to more readable
code,
even though it requires more keystrokes.
I'm not sure whether we'd want Pair versions of your map, zip, unzip, and
rotate2
(swap) methods. The question is how often such methods would be called.
FYI, adding equals(), hashCode(), toString(), and serialization to each class
would
make them more usable.
Original comment by [email protected]
on 25 Feb 2008 at 10:18
from google-collections.
Thanks for your assessment. Taking your points in order:
1. I agree that T2 (Pair) is the most important tuple; but in languages that
have
tuples I have used bigger ones, hence I went to T5 as a compromise between 2 and
infinity (Scala goes to 22). T1 is needed when processing tuple data
recursively, it
is a list of one element. Testing if the tuple has size 1 acts as a terminating
case
for recursive processing, hence T0 isn't needed. However as a matter of style
people
might like a T0, the empty list.
2. For readability on something exposed publicly I am suggesting that people
write a
value class by extending one of the tuple classes. This way they have minimum
typing
and retain all the nice tuple functionality but have both a formally documented
class
(via Javadoc) and an informally documented class via getter names. This is an
advantage a tuple API has over a built in tuple construct in some languages;
you can
extend a tuple class (Scala also allows tuple extension). Also, since the
tuples are
built hierarchically (a T5 is a T4 ... is a T1), recursive algorithms are easy.
3. The methods provided for tuples are a matter of taste, the ones I gave are
the
ones I find useful and are typical of many languages (e.g. Haskell). Many
languages
have a map literal, I am using a T2 as a map literal. A method I thought of
adding,
but didn't, was a multimap constructor from tuples used as multimap literals.
As I
say, the exact method choice is a matter of taste.
4. equals, hashCode, toString are inherited from AbstractList. The tuples are
Object
Lists. This is useful because many methods understand Lists or Iterables and
because
for each understands Iterables.
Original comment by [email protected]
on 26 Feb 2008 at 7:09
from google-collections.
Tuples are indeed very useful. We have our own lgpl tuples available here:
http://openhms.sourceforge.net/common-util/apidocs/index.html
One suggestion i might add for the usefulness of Tuple2 is to make it a
Map.Entry
(and equals/hashCode compatible), cause there is no default implementation of
Map.Entry in the jdk, and it's very useful to have.
Original comment by [email protected]
on 26 Feb 2008 at 10:03
from google-collections.
Unfortunately, it's not possible for a 2-element object to implement both List
and
Map.Entry because of their conflicting hashCode() requirements:
List.hashCode():
int hashCode = 1;
Iterator<E> i = list.iterator();
while (i.hasNext()) {
E obj = i.next();
hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
}
Map.Entry.hashCode():
(e.getKey()==null ? 0 : e.getKey().hashCode()) ^
(e.getValue()==null ? 0 : e.getValue().hashCode())
If you do need a Map.Entry implementation, Google Collections provides one
through
com.google.common.collect.Maps.immutableEntry().
Original comment by [email protected]
on 26 Feb 2008 at 10:18
from google-collections.
Thanks for the comments.
@Jared: I forgot to mention Serialization in my previous reply to you; yes I
should
add this.
@jahlborn: Your tuple library is very similar. Either great minds think alike or
fools rarely differ - you decide which :)
@cpov...: It is indeed a pity that the hash code is different. In my own code I
do
compare T2s with Map.Entrys and I also create maps from T2s using the T2s as map
literals. So I haven't actually needed a T2 to be a Map.Entry. If however
others have
a need then toMapEntry could be added to T2 and toT2 added to
com.google.common.collect.Maps.immutableEntry's return class (provided that this
class was made public). I also notice
com.google.common.collect.Maps.containsEntry
and in a similar vein isInMap(Map map) could be added to T2, if there were
demand.
As a criticism (constructive) of my own code I would add that I extended
AbstractList, unfortunately this comes with the baggage of field modCount. I
should
really extend AbstractCollection and do the extra work of implementing iterator
etc.
Original comment by [email protected]
on 27 Feb 2008 at 6:47
from google-collections.
@cpov...: yes, we sacrificed list hashCode compatibility for Map.Entry
compatibility, which we use far more. in fact, i don't think we've ever used a
list
as a key in a HashMap.
@howard.lovatt: i prefer the term "wisdomly-challenged".
Original comment by [email protected]
on 28 Feb 2008 at 8:25
from google-collections.
While there are some good ideas here, I don't see us including this in the
Google
Collection Libraries.
There are other ways of making it easier to create value objects, which is the
underlying goal, but I'm not sure which, if any, of those we'll use.
Original comment by [email protected]
on 18 Jun 2008 at 9:25
- Changed state: WontFix
from google-collections.
@jared.l.levy: You mentioned future possible inclusion of your Pair class into
google-collections, is it still considered? I'd like to vote for this.
Original comment by [email protected]
on 6 Mar 2009 at 2:56
from google-collections.
Related Issues (20)
- Preconditions.checkArgumentNotNull HOT 2
- MapMaker with weak keys: keySet() doubles reference counts in map HOT 4
- broken checksums in maven artefacts for rc4 HOT 6
- Predicates.equalTo() should return a Predicate<Object> HOT 2
- Make com.google.common.collect.ImmutableCollection.Builder public HOT 3
- Easy way to deal with null Iterables when looping HOT 1
- Please provide Sets.symmetricDifference() HOT 3
- static method Functions.toStringFunction() should be parametrized HOT 5
- LinkedListMultimap values should be of type Deque<V> HOT 3
- add transformEntries method to Maps and SortedMaps HOT 4
- Be consistent with [Collection]s.new*[Collection](...) HOT 3
- CusomConcurrentHashMap.Values should override toArray(T[]) HOT 2
- with/withoutmethods on immutable collections HOT 2
- OutOfMemoryError when using MapMaker and Soft/Weak-Keys/Values HOT 1
- MemoryLeak(?) when using expiration in MapMaker HOT 2
- equals is not implemented for Collections2.filter HOT 1
- Allow asMap() to return a Map which has defined value Collections HOT 1
- GWT Module missing HOT 3
- Create an alternative to commons-collections MultiKey
- Patch for /trunk/testfw/com/google/common/collect/testing/MapInterfaceTest.java
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from google-collections.