상세 컨텐츠

본문 제목

Java - Collection Framework

개발/Java

by 뉴에이스 2018. 9. 28. 16:21

본문

Collection Framework : 자바 1.2부터 지원
객체들에 대해서 관리한다.
	
		Set              List	   	Queue		Stack
중복을 허용하지 않는다.		순서가 유지된다.
순서를 유지하지 않는다.			
		HashSet		(검색) ArrayList
	(정렬)TreeSet	   (수정)LinkedList
	
Map -> VO 클래스 역활 대체 가능
"키"와 "값"을 같이 관리
			HashMap
		(정렬)TreeMap
			
순서를 유지하지 않는다.
검색속도가 좋다. (키값으로 바로 접근이 가능하기 때문에)
키값이 고유해야 한다. (중복X)
	
일반적으로 웹에서는 ArrayList와 HaspMap이 많이 사용된다.
 

소스

 

public static void main(String[] args) {
	// List -> ArrayList (내부적으로 배열로 관리)
	//			LinkedList (노드 클래스로 관리)
	
	// import 할때 awt 말고 util 사용 (awt는 옛날...방식)
	List list = new ArrayList<>();
	
	// 데이터추가하기
	list.add("one");
	list.add("two");
	list.add("three");
	
	// 리스트에 입력된 데이터의 크기
	System.out.println("입력된 데이터의 크기 : " + list.size());
	
	// 리스트에 있는 0번째 데이터를 삭제
	list.remove(0);
	
	System.out.println("삭제후의 크기 : " + list.size());
	
	// 리스트 자체를 출력, 재정의된 toString이 호출됨
	System.out.println(list);
	
	// 리스트의 0번째 데이터를 가져온다.
	// Gneric 되어 있어 형변환이 필요 없다
	String ele = list.get(0);
	System.out.println("첫번째 요소 : " + ele);
	
	// 리스트가 비어있는지 확인
	System.out.println("리스트가 비어있니? : " + list.isEmpty());
	
	// 리스트의 특정 요소의 위치를 가져오기 : indexOf
	int index = list.indexOf("three");
	System.out.println("찾은위치 : " + index);

	index = list.indexOf("four");
	// 찾지 못할경우 -1 반환
	System.out.println("찾은위치 : " + index);
	//list.lastIndexOf(o);
	
	// 특정 요소가 포함되어 있는지 확인 : contains
	boolean bool = list.contains("two");
	System.out.println("포함되어 있니? : " + bool);
	
	// 삭제시 데이터를 이용해서 삭제
	list.remove("two");
	System.out.println(list);
	
	// 모든 데이터 다 삭제
	list.clear();
	System.out.println("데이터 모두 삭제 후 크기 : " + list.size());
} 
 

결과

 

입력된 데이터의 크기 : 3
삭제후의 크기 : 2
[two, three]
첫번째 요소 : two
리스트가 비어있니? : false
찾은위치 : 1
찾은위치 : -1
포함되어 있니? : true
[three]
데이터 모두 삭제 후 크기 : 0

 

'개발 > Java' 카테고리의 다른 글

Java - Set (HashSet, TreeSet)  (0) 2018.09.28
Java - List, ArrayList 사용, Iterator 사용 (순환자)  (0) 2018.09.28
Java - Generic  (0) 2018.09.28
Java - Date, Calendar, SimpleDateFormat  (0) 2018.09.28
Java - Wrapper 클래스  (0) 2018.09.28

관련글 더보기

댓글 영역