More Related Content Similar to jjug-ccc 2019 Spring 発表資料 Collections Framework 入門 #jjug #jjug_ccc #ccc_c1 (20) More from Shinya Mochida (20) jjug-ccc 2019 Spring 発表資料 Collections Framework 入門 #jjug #jjug_ccc #ccc_c112. String[] array = …
String[] current = array;
String[] array = new String[current.length + 1];
System.arraycopy(current, 0, array, 0, current.length);
array[current.length] = "foo";
18. CaffeMocha[] caffeMochaArray = new CaffeMocha[1];
Coffee[] coffeeArray = caffeMochaArray;
coffeeArray[0] = new American(); // ArrayStoreException
Coffee Coffee
American
20. List<CaffeMocha> caffeMochaList = new ArrayList<>();
List<Coffee> coffeeList = caffeMochaList; // compile error
List<Coffee> List<CaffeMocha>
List<Coffee>
List<CaffeMocha>
33. Iterable<E>
• Iterator<E> iterator()
• Iterator<E> ( )
Iterable<String> iterable = ...;
Iterator<String> iterator = iterable.iterator();
while(iterator.hasNext()) {
String string = iterator.next();
System.out.println(string);
}
35. • forEach(Consumer<? extends E>)
Iterable<E> (2)
Iterable<String> iterable = ...;
iterable.forEach(string -> System.out.println(string));
37. Collection<E>
•
• boolean isEmpty()
• int size()
•
• boolean contains(Object)
• boolean
containsAll(Collection<?>)
• /
• E[] toArray(E[])
• Stream<E> stream()
•
• boolean add(E)
• boolean addAll(Collection<E>)
•
• boolean remove(E)
• true
• boolean removeAll(Collection<?>)
• boolean retainAll(Collection<?>)
• boolean removeIf(Predicate<? super E>)
• 1 true
•
• void clear()
!
!
45. List<E>
•
• E get(int)
•
• int indexOf(Object)
• int lastIndexOf(Object)
• List
• List<E> subList(int, int)
• ( )
• void add(int, E)
• void addAll(int,
Collection<E>)
•
• void set(int, E)
• void
replaceAll(UnrayOperator<E>
)
•
• void sort(Comparator<E>)
•
• E remove(int)
!
!
48. Map<K,V>
•
• V get(K)
• V getOrDefault(K,V)
•
• boolean
containsKey(Object)
• boolean
containsValue(Object)
•
• int size()
• boolean isEmpty()
• /
• Set<K> keySet()
• Collection<V> values()
• Set<Map.Entry<K,V>>
entrySet()
• void forEach(BiConsumer<?
super K, ? super T>)
50. Map<K,V>
get
• Optional<T>
Map<CoffeeName, CoffeeProduct> coffees;
Optional<CoffeeProduct> findProduct(CoffeeName name) {
return Optional.ofNullable(coffees.get(name));
}
Optional<CoffeeProduct> product = coffeeRepository
.findProduct(coffeeName(" "));
Optional<Order> newOrder = product.map(p -> p.createNewOrder(1));
51. Map<K,V>
• Java8
• V put(K, V)
• void putAll(Map<? extends
K, ? extends V>)
• Java 8
• V putIfAbsent(K, V)
• V compute(K, BiFunction<?
super K, ? super V, ? extends
V>)
• V computeIfAbsent(K,
Function<? super K, ? extends
V>)
!
• Java 8
• V replace(K, V)
• boolean replace(K, V, V)
• void replaceAll(BiFunction<?
super K, ? super V, ?
extends V>)
• V computeIfPresent(K,
Function<? super K, ?
extends V>)
• V merge(K, V, BiFunction<?
super V, ? super V, ?
extends V>)
64. Set<E>/SortedSet<E>
• … add/remove
• HashSet<E> - hashCode
Set<E>
• HashMap<E, Object>
• LinkedHashSet<E> - HashSet<E> ( )
Set<E>
• LinkedHashMap<E, Object>
• TreeSet<E> - SortedSet<E> /Comparator<E>
• TreeMap<E, Object>
70. Map<K,V>
• … put/remove
• HashMap<K,V> - hashCode
Map<K,V>
• LinkedHashMap<K,V> - HashMap<K,V>
( )
Map<K,V>
• TreeMap<K,V> - SortedMap<K,V>
72. HashMap<K,V>
Map<ProductId, DrinkProduct> toMap(SortedSet<DrinkProduct> set) {
Map<ProductId, DrinkProduct> map = new LinkedHashMap<>();
for(DrinkProduct product: set) {
map.put(product.id, product);
}
return Collections.unmodifiableMap(map);
}
Map<ProductId, DrinkProduct> map = toMap(products);
map.forEach((id, product) -> System.out.println(id));
LinkedHashMap<K,V>
78. equals
• : x.equals(x) true (x ≠ null)
• : x.equals(y) true y.equals(x) true
(x ≠ null, y ≠ null)
• : x.equals(y) true y.equals(z) true
z.equals(x) true (x ≠ null, y ≠ null, z ≠ null)
• : x.equals(y)
• x.equals(null) false (x ≠ null)
• equals NullPointerException
82. void unsafeAdd(List list, Object o) {
list.add(o);
}
List<String> list = new ArrayList<>(List.of("foo"));
unsafeAdd(list, 100L);
for(String string : list) { // ClassCastException
String.out.println(string);
}
!
83. <E, F extends E> void unsafeAdd(List<? super E> list, F o) {
list.add(o);
}
List<String> list = new ArrayList<>(List.of("foo"));
unsafeAdd(list, 100L); // compile error
88. IV. null
• null
• ArrayList<E>/LinkedList<E>/Arrays.asList()/
HashSet<E>/HashMap<K,V>
• null
• List.of()/Set.of()/Map.of() /
TreeSet<E>/TreeMap<E>
• API
89. null
List<String> someEntry() {
List<String> list = new ArrayList<>();
list.add(null);
list.add("foo");
return list;
}
List<String> list = new ArrayList<>(List.of(“bar"));
list.addAll(someEntry());
return List.copyOf(list)); // NullPointerException
!
90. null
: null add
( )
List<String> someEntry() {
List<String> list = new ArrayList<>();
list.add("foo");
return list;
}
List<String> list = new ArrayList<>(List.of("bar"));
list.addAll(someEntry());
return List.copyOf(list));
97. Map<String,Object>
void updateProfile(UserId userId, Map<String,Object> newProfile) {
if(newProfile.get("type") != null &&
newProfile.get("type").equals("image")) {
String file = (String) newProfile.get("file");
if(file == null) {
throw new IllegalArgumentException("invalid parameter");
}
updateProfileImage(userId, newProfile);
} else if(newProfile.get("type") != null &&
newProfile.get(“type").equals("fields")) {
String name = (String) newProfile.get("name");
String color = (String) newProfile.get("color");
String url = (String) newProfile.get("url");
updateNameAndColor(userId, name, color, url);
!
98. Map<String,Object>
void updateProfileImage(UserId userId, NewProfileImage newProfileImage) {
if(newProfileImage.notHavingImageUrl()) {
throw new IllegalArgumentException("invalid parameter");
}
Optional<ProfileImageUrl> currentUrl = imageRepository.findProfileImageUrl(userId);
if(currentUrl.filter(url -> url.sameUrl(newProfileImage)).isPresent()) {
imageRepository.update(newProfileImage);
}
}
void updateProfileImage(UserId userId, NewProfileFields newProfileFields) {
if(newProfileFields.satisfiesCondition(satisfiesCondition())) {
throw new IllegalArgumentException("invalid parameter");
}
profileRepository.update(newProfileFields);
}
100. •
Acroquest Technology Java (
2017 )
• Joshua Bloch Effective Java 3
( 2018 )
• (Web) Dan Becker, HOW-TO Get started with the
Java Collections Framework(JAVA WORLD FROM IDG,
1998, https://www.javaworld.com/article/2076800/
get-started-with-the-java-collections-
framework.html)
114. ArrayList vs LinkedList
add(E) - throughput/ms
ArrayList
LinkedList
JDK: oracle openjdk 12.0.1 / OS: Amazon Linux2 /
: m5a.large / : Xmx256M,Xms256M