Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[20210320] Java Map getOrDefault(), 정규표현식, List<Integer> to int[] #72

Open
JuHyun419 opened this issue Mar 20, 2021 · 0 comments
Labels

Comments

@JuHyun419
Copy link
Owner

JuHyun419 commented Mar 20, 2021

Map.getOrDefault(Object key, V defaultValue)

  • Map에서 Key가 존재하면 해당 Key의 Value를, 존재하지 않으면 기본값(defaultValue)를 반환
    /**
     * Returns the value to which the specified key is mapped, or
     * {@code defaultValue} if this map contains no mapping for the key.
     *
     * @implSpec
     * The default implementation makes no guarantees about synchronization
     * or atomicity properties of this method. Any implementation providing
     * atomicity guarantees must override this method and document its
     * concurrency properties.
     *
     * @param key the key whose associated value is to be returned
     * @param defaultValue the default mapping of the key
     * @return the value to which the specified key is mapped, or
     * {@code defaultValue} if this map contains no mapping for the key
     * @throws ClassCastException if the key is of an inappropriate type for
     * this map
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
     * @throws NullPointerException if the specified key is null and this map
     * does not permit null keys
     * (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
     * @since 1.8
     */
    default V getOrDefault(Object key, V defaultValue) {
        V v;
        return (((v = get(key)) != null) || containsKey(key))
            ? v
            : defaultValue;
    }
Map<Object, Integer> words = new HashMap<>();
// ch가 존재하면 해당 ch의 값에 + 1 을, 존재하지 않으면 1을 저장
words.put(ch, words.getOrDefault(ch, 0) + 1);

정규표현식(RegExp)

// 대문자(A-Z)가 포함되는지 판단
final String upperRegExp = ".*[A-Z].*";
Pattern.matches(upperRegExp, "AbbCd2"); // true
Pattern.matches(upperRegExp, "aeibba2"); // false

// 대소문자, 숫자, 특수문자만 포함되어있는지 확인
final String regExp = "^[A-Za-z0-9]*[~!@#$%^&*]*$";

Integer List to int Array

List<Integer> list = new ArrayList<>();
int[] answer = list.stream().mapToInt(Integer::intValue).toArray();
@JuHyun419 JuHyun419 added the Java label Mar 20, 2021
@JuHyun419 JuHyun419 changed the title [20210320] Map getOrDefault(), 정규식(matches) [20210320] Java Map getOrDefault(), 정규표현식 Mar 20, 2021
@JuHyun419 JuHyun419 changed the title [20210320] Java Map getOrDefault(), 정규표현식 [20210320] Java Map getOrDefault(), 정규표현식, List<Integer> to int[] Mar 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant