jacques-n commented on a change in pull request #11063:
URL: https://github.com/apache/arrow/pull/11063#discussion_r700629594



##########
File path: 
java/memory/memory-core/src/main/java/org/apache/arrow/util/Collections2.java
##########
@@ -46,23 +45,55 @@ private Collections2() {}
    * Converts the iterable into a new {@link List}.
    */
   public static <T> List<T> toList(Iterable<T> iterable) {
-    return StreamSupport.stream(iterable.spliterator(), 
false).collect(Collectors.toList());
+    if (iterable instanceof Collection<?>) {
+      // If iterable is a collection, take advantage of it for a more 
efficient copy
+      return new ArrayList<T>((Collection<T>) iterable);
+    }
+    return toList(iterable.iterator());
   }
 
+  /**
+   * Converts the iterable into a new immutable {@link List}.
+   */
+  public static <T> List<T> toImmutableList(Iterable<T> iterable) {
+    return Collections.unmodifiableList(toList(iterable));
+  }
+
+
   /** Copies the elements of <code>map</code> to a new unmodifiable map. */
   public static <K, V> Map<K, V> immutableMapCopy(Map<K, V> map) {
-    Map<K, V> newMap = new HashMap<>();
-    newMap.putAll(map);
-    return java.util.Collections.unmodifiableMap(newMap);
+    return Collections.unmodifiableMap(new HashMap<>(map));
   }
 
   /** Copies the elements of list to a new unmodifiable list. */
   public static <V> List<V> immutableListCopy(List<V> list) {
-    return 
Collections.unmodifiableList(list.stream().collect(Collectors.toList()));
+    return Collections.unmodifiableList(new ArrayList<>(list));
   }
 
   /** Copies the values to a new unmodifiable list. */
   public static <V> List<V> asImmutableList(V...values) {
     return Collections.unmodifiableList(Arrays.asList(values));
   }
+
+  /**
+   * Creates a human readable string from the remaining elements in iterator.
+   *
+   * The output should be similar to {@code Arrays#toString(Object[])}
+   */
+  public static String toString(Iterator<?> iterator) {
+    if (!iterator.hasNext()) {

Review comment:
       Isn't there a better stream-y way to do this with join?

##########
File path: 
java/vector/src/main/java/org/apache/arrow/vector/types/pojo/Schema.java
##########
@@ -97,7 +97,7 @@ public static Schema 
convertSchema(org.apache.arrow.flatbuf.Schema schema) {
       String value = kv.value();
       metadata.put(key == null ? "" : key, value == null ? "" : value);
     }
-    return new Schema(Collections2.immutableListCopy(fields), 
Collections2.immutableMapCopy(metadata));
+    return new Schema(true, Collections.unmodifiableList(fields), 
Collections.unmodifiableMap(metadata));

Review comment:
       You're skipping the defensive copy here? Seems like a change in behavior.

##########
File path: 
java/vector/src/main/java/org/apache/arrow/vector/types/pojo/FieldType.java
##########
@@ -68,11 +69,9 @@ public FieldType(boolean nullable, ArrowType type, 
DictionaryEncoding dictionary
       extensionMetadata.put(ExtensionType.EXTENSION_METADATA_KEY_NAME, 
((ExtensionType) type).extensionName());
       extensionMetadata.put(ExtensionType.EXTENSION_METADATA_KEY_METADATA, 
((ExtensionType) type).serialize());
       if (metadata != null) {
-        for (Map.Entry<String, String> entry : metadata.entrySet()) {
-          extensionMetadata.put(entry.getKey(), entry.getValue());
-        }
+        extensionMetadata.putAll(metadata);
       }
-      this.metadata = Collections2.immutableMapCopy(extensionMetadata);
+      this.metadata = Collections.unmodifiableMap(extensionMetadata);

Review comment:
       You're removing the copy here. Is this safe?

##########
File path: 
java/memory/memory-core/src/main/java/org/apache/arrow/util/Collections2.java
##########
@@ -46,23 +45,55 @@ private Collections2() {}
    * Converts the iterable into a new {@link List}.
    */
   public static <T> List<T> toList(Iterable<T> iterable) {
-    return StreamSupport.stream(iterable.spliterator(), 
false).collect(Collectors.toList());
+    if (iterable instanceof Collection<?>) {
+      // If iterable is a collection, take advantage of it for a more 
efficient copy
+      return new ArrayList<T>((Collection<T>) iterable);
+    }
+    return toList(iterable.iterator());
   }
 
+  /**
+   * Converts the iterable into a new immutable {@link List}.
+   */
+  public static <T> List<T> toImmutableList(Iterable<T> iterable) {
+    return Collections.unmodifiableList(toList(iterable));

Review comment:
       Can we specialize this so it avoids a copy if the existing set is 
unmodifiable with a safe backing. Same with the unmodifiable map below. Seems 
like you could then keep the defensive copies you've removed below in Schema 
and FieldType




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to