대학 생활/JAVA
[JAVA] List 중복값 제거후 정렬하기
opid
2014. 7. 23. 14:05
|
실행환경 |
|
|
Desktop |
조립식 |
|
CPU |
Intel(R) Core(TM) i7-3770 3.50GHz |
|
Memory |
4 GB |
|
OS |
Window 7 Professional 32bit |
|
Java |
1.7.0_51 |
|
Android |
SDK : 4.4.2 (KitKat), Google APIs 4.4.2 TEST : Galaxy S3 4.3(Jelly Bean) |
|
WebServer |
Apache Tomcat 7.0 |
|
DB |
MySQL 5.6.15 |
문제점
List의 중복된 값을 제거하고 정렬한다.
해결방안
ArrayList<String> tempList = new ArrayList<String>(); ArrayList<String> dataList; dataList = new ArrayList<String>(new HashSet<String>(tempList)); Collections.sort(dataList);
2019.10.11. 옛날 코드를 보니 다이아몬드 연산자도 안쓰고, 다형성도 활용하지 않고, 사이드이펙트있는 정렬 메서드 쓰고 있어서... 새로 수정 with 자바8람다
List<String> duplicateStringList = Arrays.asList("2", "2", "1", "3");
List<String> result = duplicateStringList.stream()
.distinct()
.sorted()
.collect(Collectors.toList());