프로그래밍/Java 공부

Collection_[List, Set, Map]

개발계발게발 2021. 6. 11. 17:27
반응형

Collection

 

다수의 데이터를 쉽고 효과적으로 처리할 수 있는 표준화된 방법을 제공하는 클래스의 집합

 

데이터를 저장하는 자료구조와 데이터를 처리하는 알고리즘이 구조화된 클래스 

 

자바의 자료구조

 

List형  - 순서가 있는 목록(중복 허용)
Set형  - 순서가 중요하지 않은 목록(중복 X)
Queue형 - 선입 선출
Map형  - Key - Value로 저장. (키 중복 X)

 

 

Collection 인터페이스

Collection 인터페이스 메소드

List 인터페이스

List 인터페이스는 중복을 허용하면서 저장 순서가 유지되는 컬렉션

1-5. Set 인터페이스

Set 인터페이스는 중복을 허용하지 않고 저장순서가 유지되지 않는 컬렉션 클래스르 구현하는 데 사용된다.

1-6. Map 인터페이스

Map 인터페이스는 키(key)와 값(value)을 하나의 쌍으로 저장하는 컬렉션 클래스를 구현하는 데 사용된다.

값은 중복될 수 있지만 키의 중복은 허용하지 않는다.
기존에 저장된 데이터와 중복된 키와 값을 저장하면 기존의 값은 없어지고 마지막에 저장된 값이 남게 된다.

Map 인터페이스에서 값은 중복을 허용하기 때문에 Collection 타입으로 반환하고, 키는 중복을 허용하지 않기 때문에 Set 타입으로 반환한다.

Map.Entry 인터페이스

Map.Entry 인터페이스는 Map 인터페이스의 내부 인터페이스이다.
Map에 저장되는 key-value 쌍을 다루기 위해 내부적으로 Entry 인터페이스가 정의되어 있다. 보다 객체지향적인 설계를 하도록 유도한 것으로 Map 인터페이스 구현하는 클래스에서는 Map.Entry 인터페이스도 함께 구현해야 한다.

 

		ArrayList<Integer> al = new ArrayList<Integer>();
		
		for (int i = 1; i <= 5; i++) {
			al.add(i*10);			
		}
		//[10, 20, 30, 40, 50]
		
		//삭제 remove()
		al.remove(0);	//	[20, 30, 40, 50]
		
		//특정 번지에 추가 add(번지, 값)
		al.add(0,1000); //[1000, 20, 30, 40, 50]
		
		al.add(3,10); //[1000, 20, 30, 10, 40, 50]
		
		//특정 번지 수정 al.set(번지, 수정할 값)
		al.set(0, 5000);	//[5000, 20, 30, 10, 40, 50]
		
		//크기 size()
		al.set(al.size()-1, 5000);	//[5000, 20, 30, 10, 40, 5000]
		
		//값 검색
		System.out.println(al.contains(30));	//true
		System.out.println(al.contains(50));	//flase
		
		//그 값이 있다면 몇 번지에 있나
		
		System.out.println(al.indexOf(30));	//2	
		System.out.println(al.indexOf(50));	//-1 (값이 없을때)
		
		System.out.println(al);
		
		ArrayList<Integer> N = new ArrayList<Integer>();
		
		for (int i = 0; i < 30; i++) {
			//if(i%2==1) {
				N.add(i+1);				
			//}
		}
		
		for (int i = 0; i < N.size(); i++) {	
			if(N.get(i) % 2 == 0) {//[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]
				N.remove(i);
			}
		}
//		for (int i = 0; i < N.size(); i++) {	
//				N.remove(i+1);
//		}
		
		
		
		System.out.println(N);
		
		ArrayList<Integer> Lotto = new ArrayList<Integer>();
		
//		for(int i = 0; i<10; i++) {
//			Lotto.add((int) ((Math.random()*45)+1));
//			for (int j = 0; j < i; j++) {
//				if(Lotto.get(j)==Lotto.get(i)) {
//					System.out.println(Lotto.get(i)+" 중복 발생");
//					System.out.println(Lotto);
//					Lotto.remove(i);	//중복시 제거
//					i--;	//다시 뽑기
//					break;
//				}
//			}			
//		}
		for(int i = 0; i < 10; i++) {
			int ran = (int)(Math.random()*45+1);
			if(Lotto.indexOf(ran) != -1) {
				System.out.println(ran + " 중복 발생");
				i--;
				continue;
			}
			Lotto.add(ran);			
		}
		System.out.println(Lotto);

 

ArrayList와 일반 배열 차이

 


ArrayList는 List 인터페이스의 구현체로 ArrayList의 객체를 추가하면 객체가 인덱스로 관리된다.
일반 배열과 ArrayList는 인덱스로 객체를 관리

단, 큰 차이점은 배열은 크기가 고정이라 크기 변경이 불가능하지만 ArrayList는 크기 변경이 가능
(저장용량을 초과한 객체가 들어오면 자동으로 저장용량 증가)

		ArrayList<String> al01 = new ArrayList<String>();
		al01.add("홍길동");
		al01.add("김길동");
		al01.add("이길동");
		al01.add("박길동");
		al01.add("최길동");
		System.out.println(al01); //[홍길동, 김길동, 이길동, 박길동, 최길동]

		ArrayList<String> al02 = new ArrayList<String>(al01);
		
		al02.add("황길동");
		System.out.println(al01);	//[홍길동, 김길동, 이길동, 박길동, 최길동]
		System.out.println(al02);	//[홍길동, 김길동, 이길동, 박길동, 최길동, 황길동]
		
		System.out.println(al01 == al02); // false
		//724p
		
		al01.clear();
		
		System.out.println(al01.size());	//0
		System.out.println(al01.isEmpty()); //true
		System.out.println(al01);	// []
		
		System.out.println(al02.remove(0));			//홍길동
		System.out.println(al02.remove("김길동"));	//true - 삭제
		System.out.println(al02.remove("홍길동"));	//flase - 데이터 X
		System.out.println(al02);					//[이길동, 박길동, 최길동, 황길동]
		
		ArrayList<String> al3 = new ArrayList<String>(30);
		al3.add(null);
		al3.add(null);
		al3.add(null);
		System.out.println(al3.size());	//3
		System.out.println(al3);	// [null, null, null]
		
		List<String> al4 = Arrays.asList("홍길동", "김길동");
		//java.util.List=인터페이스
		System.out.println(al4); //[홍길동, 김길동]
		
		List<String> al5 = new ArrayList<String>();
		
		System.out.println(al5);
	}

 

List<String> al = new ArrayList<String>();
		al.add("가");
		al.add("나");
		al.add("다");
		
		System.out.println(al);	//[가, 나, 다]
		
		///////////////////////////////
		
		LinkedList<String> ll = new LinkedList<String>();
		
		ll.addFirst("A");
		ll.addFirst("B");
		ll.addFirst("C");
		ll.addFirst("D");
		System.out.println(ll);	//[D, C, B, A]
		
		ll.removeLast();
		
		System.out.println(ll);	//[D, C, B]
		
		
		////////////////////////////////
		ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
		ArrayList<String> innerList = new ArrayList<String>();
		
		innerList.add("1");
		innerList.add("2");
		innerList.add("3");
		
		list.add(innerList);
		
		list.add(innerList);
		
		list.add(innerList);
		
		//System.out.println(list);
		for (ArrayList<String> string : list) {
			System.out.println(string);
		}
		//					키		값
		//ArrayList<HashMap<String, Object>> list =
		list.clear();
		innerList.clear();
		for (int i = 1; i < 10; i++) {
			innerList.add(String.valueOf(i));			
			if(i%3==0) {
				list.add(innerList);
				//innerList.clear()  클리어시 번지수 동일
				innerList = new ArrayList<String>(); //새로 생성
			}
		}
		
		for (ArrayList<String> in : list) {
			System.out.println(in);
		}
		
		System.out.println(list);
		list.clear();
		innerList.clear();
		
		int num =1;
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {				
				innerList.add(String.valueOf(num));
				num++;
			}
			list.add(innerList);
			innerList = new ArrayList<String>(); //새로 생성
		}		
		for (ArrayList<String> in : list) {
			System.out.println(in);
		}
		System.out.println("----------------");
		
		ArrayList<String> list0 = list.get(0);
		
		
		list.remove(0);
		
		System.out.println(list0.get(list0.size()-1));
		
		ArrayList<String> temp = list.get(0);

		System.out.println(temp);

 

 

 

반응형