Collection API

김신영·2022년 9월 23일
0

stream

목록 보기
3/3
post-thumbnail

List Factory

  • Arrays.asList(...)
List<String> list = Arrays.asList("a", "b");  
System.out.println(list);   // [a, b]  
list.add("c");              // UnsupportedOperationException  
list.set(0, "A");           // UnsupportedOperationException
  • List.of(...)
List<String> list2 = List.of("a", "b");  
System.out.println(list2);  // [a, b]  
list2.add("c");             // UnsupportedOperationException  
list.set(0, "A");           // UnsupportedOperationException

Set Factory

  • Set.of(...)
Set<Integer> set = Set.of(1, 2, 3, 4);  
System.out.println(set);    // [2, 3, 4, 1]  
set.add(4);                 // UnsupportedOperationException  

Map Factory

  • Map.of(key, value, key, value, ...)
Map<String, Integer> map = Map.of("a", 1, "b", 0, "c", 2);  
System.out.println(map);    // {a=1, b=0, c=2}  
map.put("c", 4);            // UnsupportedOperationException  
  • Map.ofEntries(Map.entry(key, value), Map.entry(key, value), ...)
Map<String, Integer> map2 = Map.ofEntries(entry("a", 2), entry("b", 1),  
    entry("c", 0));  
System.out.println(map2);   // {a=2, b=1, c=0}  
map2.put("d", 1);           // UnsupportedOperationException

List, Set 에 추가된 Default 메서드

  • removeIf
  • replaceAll
  • sort
boolean removeIf(Predicate<E> filter);

void replaceAll(UnaryOperator<E> operator);

void sort(Comparator<? super E> comparator);

ListIterator

public interface ListIterator<E> extends Iterator<E> {
	boolean hasPrevious();
	E previous();
	int nextIndex();
	int previousIndex();
	void set(E e);
	void add(E e);
}

Map 에 추가된 Default 메서드

  • Set<Map.Entry<K, V>> entrySet()
  • forEach((k, v) -> {...})
  • Map.Entry.comparingByKey()
  • Map.Entry.comparingByValue()
  • getOrDefault(key, defaultValue)
  • computeIfAbsent(key, Function<K, V> mappingFunction)
  • computeIfPresnet(key, BiFunction<K, V, V> remappingFunction)
  • compute(key, BiFunction<K, V, V> remappingFunction)
  • remove(key, value)
  • replaceAll(BiFunction<K, V, V> remappingFunction)
  • replace(key, oldValue, newValue)
  • merge(key, value, BiFunction<K, V, V> remappingFunction)
default void forEach(BiConsumer<? super K, ? super V> action)

default V getOrDefault(Object key, V defaultValue)

default V computeIfAbsent(K key, 
	Function<? super K, ? extends V> mappingFunction)

default V computeIfPresent(K key, 
	BiFunction<? super K, ? super V, ? extends V> remappingFunction)

default V compute(K key,  
    BiFunction<? super K, ? super V, ? extends V> remappingFunction)

default boolean remove(Object key, Object value)

default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)

default boolean replace(K key, V oldValue, V newValue)

default V merge(K key, V value,  
    BiFunction<? super V, ? super V, ? extends V> remappingFunction)

Example

  • Set<Entry<K, V>> entrySet()
Map<String, Integer> map = Map.of("a", 1, "b", 0, "c", 2);  

for (Entry<String, Integer> entry : map.entrySet()) {  
  System.out.println(entry.getKey() + "=" + entry.getValue());  
}

System.out.println();

map.forEach(System.out::println);

/***
a=1
b=0
c=2

a=1
b=0
c=2
****/
  • Map.Entry.comparingByKey()
map.entrySet().stream()  
    .sorted(Entry.comparingByKey())  
    .forEach(System.out::println);

/***
a=1
b=0
c=2
****/
  • Map.Entry.comparingByValue()
map.entrySet().stream()  
    .sorted(Entry.comparingByValue())  
    .forEach(System.out::println);  

/***
b=0
a=1
c=2
****/

ConcurrentHashMap

  • forEach

  • reduce

  • search

  • mappingCount

  • forEach

  • forEachKey

  • forEachValue

  • forEachEntry

void forEach(long parallelismThreshold, BiConsumer<? super K,? super V> action);

void forEach(long parallelismThreshold,  
        BiFunction<? super K, ? super V, ? extends U> transformer,  
	    Consumer<? super U> action);

void forEachKey(long parallelismThreshold, Consumer<? super K> action);

void forEachKey(long parallelismThreshold,  
		Function<? super K, ? extends U> transformer,  
		Consumer<? super U> action);

void forEachValue(long parallelismThreshold, Consumer<? super V> action);

void forEachValue(long parallelismThreshold,  
		Function<? super V, ? extends U> transformer,  
		Consumer<? super U> action);

void forEachEntry(long parallelismThreshold,
		Consumer<? super Map.Entry<K,V>> action);

void forEachEntry(long parallelismThreshold,  
		Function<Map.Entry<K,V>, ? extends U> transformer,  
		Consumer<? super U> action);
  • reduce
    - reduceToInt
    - reduceToLong
    - reduceToDobule
  • reduceKeys
    - reduceKeysToInt
    - reduceKeysToLong
    - reduceKeysToDobule
  • reduceValues
    - reduceValuesToInt
    - reduceValuesToLong
    - reduceValuesToDobule
  • reduceEntries
    - reduceEntriesToInt
    - reduceEntriesToLong
    - reduceEntriesToDobule
U reduce(long parallelismThreshold,  
		BiFunction<? super K, ? super V, ? extends U> transformer,  
		BiFunction<? super U, ? super U, ? extends U> reducer);

K reduceKeys(long parallelismThreshold,  
		BiFunction<? super K, ? super K, ? extends K> reducer);

U reduceKeys(long parallelismThreshold,  
		Function<? super K, ? extends U> transformer,  
		BiFunction<? super U, ? super U, ? extends U> reducer);

V reduceValues(long parallelismThreshold,  
		BiFunction<? super V, ? super V, ? extends V> reducer);

U reduceValues(long parallelismThreshold,  
		Function<? super V, ? extends U> transformer,  
		BiFunction<? super U, ? super U, ? extends U> reducer);

Map.Entry<K,V> reduceEntries(long parallelismThreshold,  
	BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer);

U reduceEntries(long parallelismThreshold,  
		Function<Map.Entry<K,V>, ? extends U> transformer,  
		BiFunction<? super U, ? super U, ? extends U> reducer);

// Unboxing
int reduceToInt(long parallelismThreshold,  
	    ToIntBiFunction<? super K, ? super V> transformer,  
	    int basis,  
	    IntBinaryOperator reducer);
                       
long reduceToLong(long parallelismThreshold,  
		ToLongBiFunction<? super K, ? super V> transformer,  
		long basis,  
		LongBinaryOperator reducer);

double reduceToDouble(long parallelismThreshold,  
		ToDoubleBiFunction<? super K, ? super V> transformer,  
		double basis,  
		DoubleBinaryOperator reducer);
  • search
  • searchKeys
  • searchValues
  • searchEntries
U search(long parallelismThreshold,  
		BiFunction<? super K, ? super V, ? extends U> searchFunction);

U searchKeys(long parallelismThreshold,  
		Function<? super K, ? extends U> searchFunction);

U searchValues(long parallelismThreshold,  
		Function<? super V, ? extends U> searchFunction);

U searchEntries(long parallelismThreshold,  
		Function<Map.Entry<K,V>, ? extends U> searchFunction);
profile
Hello velog!

0개의 댓글