Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

jjug-ccc 2019 Spring 発表資料 Collections Framework 入門 #jjug #jjug_ccc #ccc_c1

3,142 views

Published on

JJUG CCC 2019 Spring C1 の Collections Framework 入門の発表資料です

Published in: Technology
  • If you want to download or read this book, copy link or url below in the New tab ......................................................................................................................... DOWNLOAD FULL PDF EBOOK here { http://bit.ly/2m6jJ5M } .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THAT BOOKS/FILE INTO AVAILABLE FORMAT - (Unlimited) ......................................................................................................................... ......................................................................................................................... Download FULL PDF EBOOK here { http://bit.ly/2m77EgH } ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... accessibility Books Library allowing access to top content, including thousands of title from favorite author, plus the ability to read or download a huge selection of books for your pc or smartphone within minutes Christian, Classics, Comics, Contemporary, Cookbooks, Art, Biography, Business, Chick Lit, Children's, Manga, Memoir, Music, Science, Science Fiction, Self Help, History, Horror, Humor And Comedy, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • (Unlimited)....ACCESS WEBSITE Over for All Ebooks ................ accessibility Books Library allowing access to top content, including thousands of title from favorite author, plus the ability to read or download a huge selection of books for your pc or smartphone within minutes ......................................................................................................................... DOWNLOAD FULL PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn }
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • ACCESS that WEBSITE Over for All Ebooks (Unlimited) ......................................................................................................................... DOWNLOAD FULL PDF EBOOK here { http://bit.ly/2m77EgH } ......................................................................................................................... DOWNLOAD FULL EPUB Ebook here { http://bit.ly/2m77EgH } ......................................................................................................................... Download Full PDF EBOOK here { http://bit.ly/2m77EgH } ......................................................................................................................... Download EPUB Ebook here { http://bit.ly/2m77EgH }
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THI5 BOOKS 1NTO AVAILABLE FORMAT (Unlimited) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { http://bit.ly/2m77EgH } ......................................................................................................................... Download Full EPUB Ebook here { http://bit.ly/2m77EgH } ......................................................................................................................... ACCESS WEBSITE for All Ebooks ......................................................................................................................... Download Full PDF EBOOK here { http://bit.ly/2m77EgH } ......................................................................................................................... Download EPUB Ebook here { http://bit.ly/2m77EgH } ......................................................................................................................... Download doc Ebook here { http://bit.ly/2m77EgH } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

jjug-ccc 2019 Spring 発表資料 Collections Framework 入門 #jjug #jjug_ccc #ccc_c1

  1. 1. Collections Framework JJUG CCC 2019 spring C1 2019-05-18
  2. 2. #ccc_c1
  3. 3. • • twitter: @mike_neck • github : mike-neck • L is B direct (Java/Golang)
  4. 4. • Java ( ) • Java
  5. 5. • • Queue • Dequeue • Optional … • • •
  6. 6. • Collections Framework • API • •
  7. 7. Collections Framework
  8. 8. Java • Java Java java.lang.String int com.app.Course
  9. 9. • Java java.uti.Set<com.app. >
  10. 10. • Coffee[] coffees = new Coffee[];
  11. 11. (1) • • • (Effective Java 3rd ed No.59) •
  12. 12. String[] array = … String[] current = array; String[] array = new String[current.length + 1]; System.arraycopy(current, 0, array, 0, current.length); array[current.length] = "foo";
  13. 13. List<E> List<String> list = … list.add("foo");
  14. 14. (2) • • Java •
  15. 15. Coffee CaffeMocha Coffee[] CaffeMocha[] Java CaffeMocha Coffee CaffeMocha Coffee
  16. 16. Coffee CaffeMocha Coffee[] CaffeMocha[] Java CaffeMocha Coffee
  17. 17. Coffee CaffeMocha Coffee[] CaffeMocha[] CaffeMocha[] CaffeMocha CaffeMocha ( Coffee )
  18. 18. CaffeMocha[] caffeMochaArray = new CaffeMocha[1]; Coffee[] coffeeArray = caffeMochaArray; coffeeArray[0] = new American(); // ArrayStoreException Coffee Coffee American
  19. 19. List<E> (Java 5 ) Coffee CaffeMocha List<Coffee> List<CaffeMocha> List<Coffee> List<CaffeMocha>
  20. 20. List<CaffeMocha> caffeMochaList = new ArrayList<>(); List<Coffee> coffeeList = caffeMochaList; // compile error List<Coffee> List<CaffeMocha> List<Coffee> List<CaffeMocha>
  21. 21. Collections Framework • • Java • Collections Framework • Collections Framework
  22. 22. API
  23. 23. • API •
  24. 24. Iterable<T> Collection<E> List<E> Set<E> Map<K,V> SortedSet<E> SortedMap<K,V>
  25. 25. Set<E> • ( ) • ( )
  26. 26. SortedSet<E> • • ( )
  27. 27. List<E> • ( ) • 0 1 2 3 4 5
  28. 28. Map<K,V> • K (Set) V • ( -> ) 350JPY 550JPY 450JPY 350JPY
  29. 29. Collection<E> • ( ) • / / / /
  30. 30. • • • ( ) • • • UnsupportedOperationException ( )
  31. 31. !
  32. 32. Iterable<T> Iterable<T> Collection<E> List<E> Set<E> Map<K,V>
  33. 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); }
  34. 34. Iterable<E> (1) • Iterable<E> for Iterable<String> iterable = ...; for(String item : iterable) {
  35. 35. • forEach(Consumer<? extends E>) Iterable<E> (2) Iterable<String> iterable = ...; iterable.forEach(string -> System.out.println(string));
  36. 36. Collection<E> Iterable<E> Collection<E> List<E> Set<E> Map<K,V>
  37. 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() ! !
  38. 38. Collection<E> • ID
  39. 39. Collection<E> public Collection<CoffeeSale> findCoffeeSales( Instant date, Collection<CoffeeId> coffeeIds) { // cofeeIds 0 return if (coffeeIds.isEmpty()) { return Collections.emptyList(); } // }
  40. 40. removeAll(Collection) collection.removeAll(parameter); collection parametercollection
  41. 41. retainAll(Collection) collection.retainAll(parameter); parametercollection
  42. 42. Set<E> Iterable<E> Collection<E> List<E> Set<E> Map<K,V> SortedSet<E> SortedMap<K,V>
  43. 43. Collection<E> Collection<E> ( ) Set<E>
  44. 44. List<E> Iterable<E> Collection<E> List<E> Set<E> Map<K,V>
  45. 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) ! !
  46. 46. List#remove(int) List<Integer> list = new ArrayList<>( List.of(1, 2, 3, 4, 5)); // remove(int)? remove(E)? list.remove(1); !
  47. 47. Map<K,V> Iterable<E> Collection<E> List<E> Set<E> Map<K,V>
  48. 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>)
  49. 49. Map<K,V> get • Map<CoffeeName, CoffeeProduct> coffees; CoffeeProduct findProduct(CoffeeName name) { return coffees.get(name); } CoffeeProduct product = coffeeRepository .findProduct(coffeeName(" ")); Order newOrder = product.createNewOrder(1); // NullPointerException
  50. 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. 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>)
  52. 52. put(K,V) put(K,V) putIfAbsent(K,V) replace(K,V) put(K,V) putIfAbsent/replace
  53. 53. compute • • Stream (10 ) DrinkProduct DrinkName DrinkCategory Price
  54. 54. Map<DrinkCategory,List<DrinkProduct>> groupByCategory( List<DrinkProduct> list) { Map<DrinkCategory,List<DrinkProduct>> map = new HashMap<>(); for(DrinkProduct product: list) { if(!map.containsKey(product.category)) { map.put(product.category, new ArrayList<>()); } map.get(product.category).add(product); } return Collections.unmodifiableMap(map); }
  55. 55. Map<DrinkCategory,List<DrinkProduct>> groupByCategory( List<DrinkProduct> list) { Map<DrinkCategory,List<DrinkProduct>> map = new HashMap<>(); for(DrinkProduct product: list) { map.computeIfAbsent(product.category, k -> new ArrayList<>()) .add(product); } return Collections.unmodifiableMap(map); }
  56. 56. Map<K,V> • • V remove(Object) • boolean remove(Object, Object) • • void clear() !
  57. 57. • • (Set/List/Map) • UnsupportedOperationException
  58. 58. List<E> • … add/remove/sort • ArrayList<E> - List<E> • LinkedList<E> - List<E>
  59. 59. ArrayList<E> vs LinkedList<E> • pros • ArrayList<E> - • LinkedList<E> - / • cons • ArrayList<E> - • LinkedList<E> -
  60. 60. LinkedList Queue Dequeue(Stack) ArrayList LinkedList ArrayList
  61. 61. List<E> • … add/remove/sort UnsupportedOperationException • • List.of(E…) • List.copyOf(Collection<? extends E>) • Arrays.asList(E…) • Collections.emptyList() • Collections.singletonList(E) • Collections.unmodifiableList(List<? extends E>)
  62. 62. 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>
  63. 63. Set<Coffee> Set<E> Map<K,V> Set<Price> 350 JPY 450 JPY 550 JPY
  64. 64. Set<Coffee> Set<E> Map<K,V> Set<Price> 350 JPY 450 JPY 550 JPY
  65. 65. Collection<Price> IdentifiedBy<Coffee> Set<E> Map<K,V> 350 JPY 350 JPY 450 JPY 550 JPY
  66. 66. Map<Coffee,Price> Set<E> Map<K,V> 350 JPY 350 JPY 450 JPY 550 JPY
  67. 67. Set<E> • … add/remove/sort UnsupportedOperationException • • Set.of(E…) • Set.copyOf(Collection<? extends E>) • Collections.emptySet() • Collections.singleton(E) • Collections.unmodifiableSet(List<? extends E>)
  68. 68. 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>
  69. 69. HashMap<K,V> Map<ProductId, DrinkProduct> toMap(SortedSet<DrinkProduct> set) { Map<ProductId, DrinkProduct> map = new HashMap<>(); 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));
  70. 70. 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>
  71. 71. Map<K,V> • … put/remove/compute UnsupportedOperationException • • Map.of(K,V) • Map.copyOf(Map<? extends K,? extends V>) • Collections.emptyMap() • Collections.singletonMap(K,V) • Collections.unmodifiableMap(Map<? extends K,? extends V>)
  72. 72. • •
  73. 73. 6
  74. 74. 6 • • equals/hashCode • ( ) • ( ) • null • null • Map<String, ?>
  75. 75. I. equals/hashCode • equals contains(containsKey) • Set<E> add • HashSet hashCode hashCode
  76. 76. 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
  77. 77. hashCode • equals hashCode • x.equals(y) true x.hashCode() y.hashCode() • hashCode() • x.equals(y) false x y hashCode()
  78. 78. equals/hashCode • • IDE • • Lombok • • google/auto AutoValue • IDE
  79. 79. II. • Java 1.4 Java 5 • List<String> List
  80. 80. 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); } !
  81. 81. <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
  82. 82. III. • List<E> (ArrayList<E>/LinkedList<E>) (List.of()/Arrays.asList()) • Collections Framework (add/remove/clear)
  83. 83. void addSomeEntry(List<String> list) { list.add("bar"); // UnsupportedOperationException } List<String> list = List.of("foo"); unsafeAdd(list); !
  84. 84. • UnsupportedOperationException • • ( ) • ArrayList<E>/ LinkedList<E> •
  85. 85. List<String> someEntry() { return List.of("bar"); // } List<String> list = new ArrayList<>(List.of(“foo")); list.addAll(someEntry());
  86. 86. 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
  87. 87. 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 !
  88. 88. 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));
  89. 89. null : ArrayList findbugs/spotbugs class ListBuilder<E>{ final List<E> list = new ListBuilder<>(); final add(@Nonnull E e) { list.add(e); } } List<String> someEntry() { ListBuilder<String> listBuilder = new ListBuilder<>(); listBuilder.add("foo"); return listBuilder.getList(); }
  90. 90. V. null • IV •
  91. 91. null List<String> someEntries(Key key) { return map.get(key); } List<String> list = someEntries(); return list.size(); // NullPointerException !
  92. 92. null List<String> someEntries(Key key) { return map.getOrDefault(key, Collections.emptyList()); } List<String> list = someEntries(); return list.size(); // 0
  93. 93. VI. Map<String,Object> • Map<String,Object> • Map<String,Object> •
  94. 94. Map<String,Object> PUT /users/1234 {“type":"image", "file":"https://example.com/files/123"} PUT /users/1234 {"type":"fields", "name":" ", "color":"#009933"}
  95. 95. 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); !
  96. 96. 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); }
  97. 97.
  98. 98. • 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)
  99. 99. 1. ArrayList
  100. 100. ArrayList<E> (Object[])
  101. 101. add(int, E) ArrayList<E>
  102. 102. ArrayList<E>
  103. 103. ArrayList<E>
  104. 104. 2. LinkedList
  105. 105. LinkedList<E> previous next previous next previous next first last Node<E> LinkedList Node<E>
  106. 106. LinkedList<E> previous next previous next previous next add(int, E)
  107. 107. LinkedList<E> previous next previous next previous next Node<E> previous next
  108. 108. LinkedList<E> previous next previous next previous next (next/previous) previous next
  109. 109. 3. ArrayList vs LinkedList
  110. 110. ArrayList vs LinkedList add(E) - throughput/ms ArrayList LinkedList JDK: oracle openjdk 12.0.1 / OS: Amazon Linux2 / : m5a.large / : Xmx256M,Xms256M
  111. 111. ArrayList vs LinkedList add(0,E) - throughput/ms ArrayList LinkedList JDK: oracle openjdk 12.0.1 / OS: Amazon Linux2 / : m5a.large / : Xmx256M,Xms256M
  112. 112. ArrayList vs LinkedList get(int) - throughput/ms ArrayList LinkedList JDK: oracle openjdk 12.0.1 / OS: Amazon Linux2 / : m5a.large / : Xmx256M,Xms256M
  113. 113. ArrayList vs LinkedList forEach(Consumer) - throughput/ms ArrayList LinkedList JDK: oracle openjdk 12.0.1 / OS: Amazon Linux2 / : m5a.large / : Xmx256M,Xms256M
  114. 114. 4. HashMap
  115. 115. HashMap<E> Node K key int hash/K key/V value Node int hash V value next
  116. 116. HashMap<E> Node (Node[]) length
  117. 117. HashMap<E> Node index Node hash mod Node hash length index
  118. 118. HashMap<E> index LinkedList next next
  119. 119. HashMap<E> index LinkedList next next LinkedList
  120. 120. HashMap<E> LinkedList hashCode B R left right R
  121. 121. 5. TreeMap
  122. 122. TreeMap<K,V> • TreeMap<K,V> • • • K key V value • • • ( )
  123. 123. TreeMap<K,V> • • ( ) • • • •
  124. 124. TreeMap<K,V> 74 3 key: value: 4 key:4 key:7 4 7
  125. 125. TreeMap<K,V> 7 3 key: value: 4 key:3 key:7 key:3 key:4 3
  126. 126. TreeMap<K,V> 7 key: value: 4 3
  127. 127. TreeMap<K,V> 7 4 3 ← (4) 4 73

×