반응형
ackage com.demo.testing;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "CROCUS";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<Integer> list = createList();

        getUniqueDataByHash(list);
        getUniqueDataByStream(list);
    }

    private List<Integer> createList() {
        List<Integer> list = new CopyOnWriteArrayList<>();
        for (int i = 0; i < 100; i++) {
            int val = (int) (Math.random() / 10 * 100);
            list.add(val);
        }
        System.out.println("RESULT SIZE : " + list.size());
        System.out.println("RESULT : " + list);

        return list;
    }

    private void getUniqueDataByStream(List<Integer> list) {
        System.out.println("\n== getUniqueDataByStream ==");
        list = list.stream().distinct().sorted().collect(Collectors.toList());
        list.forEach(integer -> System.out.print(integer + " "));
        System.out.println("\n=====");
    }

    private void getUniqueDataByHash(List<Integer> list) {
        ConcurrentHashMap<Integer, Boolean> hashMap = new ConcurrentHashMap<>();
        list.forEach(val -> hashMap.putIfAbsent(val, true));

        System.out.println("\n== getUniqueDataByHash ==");
        hashMap.forEach((integer, aBoolean) -> System.out.print(integer + " "));
        System.out.println("\n=====");
    }
}

 

I/System.out: RESULT SIZE : 100
    RESULT : [3, 9, 7, 0, 8, 2, 0, 1, 6, 9, 6, 5, 3, 4, 1, 5, 6, 6, 5, 4, 4, 5, 5, 8, 0, 8, 5, 7, 4, 3, 9, 7, 7, 1, 9, 9, 3, 4, 2, 5, 8, 7, 7, 9, 6, 6, 2, 9, 9, 9, 7, 6, 2, 8, 2, 9, 3, 8, 9, 0, 7, 7, 5, 3, 9, 4, 1, 4, 9, 3, 6, 4, 8, 2, 4, 8, 7, 1, 1, 5, 6, 4, 1, 3, 3, 1, 9, 9, 7, 5, 9, 3, 9, 9, 4, 8, 5, 8, 2, 6]
I/System.out: == getUniqueDataByHash ==
    0 1 2 3 4 5 6 7 8 9 
I/System.out: =====
    == getUniqueDataByStream ==
I/System.out: 0 1 2 3 4 5 6 7 8 9 
    =====

 

첫번째로는 해싱을 이용하여 중복된 값을 처리하는 방법이다.

이를 이용하면 중복된 값을 피할 수 있게 된다.

    private void getUniqueDataByHash(List<Integer> list) {
        ConcurrentHashMap<Integer, Boolean> hashMap = new ConcurrentHashMap<>();
        list.forEach(val -> hashMap.putIfAbsent(val, true));

        System.out.println("\n== getUniqueDataByHash ==");
        hashMap.forEach((integer, aBoolean) -> System.out.print(integer + " "));
        System.out.println("\n=====");
    }

 

 

Java 8 이상 버전에서는 Stream과  distinct를 이용하여 더 간결하게 중복을 제거 할 수 있다.

    private void getUniqueDataByStream(List<Integer> list) {
        System.out.println("\n== getUniqueDataByStream ==");
        list = list.stream().distinct().sorted().collect(Collectors.toList());
        list.forEach(integer -> System.out.print(integer + " "));
        System.out.println("\n=====");
    }

 

이때 조심해야하는건 List<Object>일 경우에는 object의 hash가 다를 수 있으므로 distinct를하여도 중복된값이 걸러지지 않으니 잘 파악해보도록 하자.

반응형