Last active
August 29, 2015 14:18
-
-
Save up1/b00105968790e7484dd4 to your computer and use it in GitHub Desktop.
Demo :: java cast type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CalculateSize<T> { | |
public int sizeOf(Iterable<T> items) { | |
int size = 0; | |
if (items instanceof Collection) { | |
size = Collection.class.cast(items).size(); | |
} else { | |
for (Object item : items) { | |
size++; | |
} | |
} | |
return size; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CalculateSize<T> { | |
public int sizeOf(Iterable<T> items) { | |
int size = 0; | |
for (Object item : items) { | |
size++; | |
} | |
return size; | |
} | |
public int sizeOf(Collection<T> items) { | |
return items.size(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment