ENFJ 비전공자 개발스터디

자바 Java 컬렉션 collection set list map 제네릭 Generic Arraylist 본문

Java

자바 Java 컬렉션 collection set list map 제네릭 Generic Arraylist

madb 2021. 12. 9. 17:18
반응형

자바 java 컬렉션 collection 특징 : 

순서가 없다, 중복된값 저장하지 않는다 다형성, 공통기능 

여러개 저장, 크기 조정 가능, 다른 타입 저장 가능(불편하기 때문에 다른 타입 저장하는데 있어서 제한을 건다 => 제한을 건다는 뜻은 다른 타입을 저장불가하게 한다=> 이게 제네릭이다, 제네릭은 지정된 타입만 저장한다) 


hashset  : set은 공간이기 때문에 인덱스 없다. 그저 저장하는 기능 

중복되는 값이 있는 지 검색을 해보고 들어오지 못하게 막아야 한다.그래서 빠른 검색이 필요하다. 그래서 hash 기법이 필요한거다. 인덱스가 없기 때문에 hash가 필요한 것이다. 


리스트는 중복을 허용한다 

interface는 다중상속이 가능하다. 

map은 index가 없다- > 순서가 없다 -> 반복이 불가능하다 그래서 interator 필요 

 key의 value형태로 저장 


vector (collections) 

(Object obj) 받는 놈 파라미터 parameter

세상에 있는 모든 오브젝트가 다 들어갈 수 있다. 


자바 java 제네릭 generic 

상속받은 클래스 개수와 똑같은 개수만큼 instanceof 한 다음 다운캐스팅을 해야한다

=> 불가능 => 그렇기 때문에 제네릭한다 아래 <String>

Vector(Object obj) <String> 꺽쇠가 추가, String 꺼내서 쓰면 된다. 

<E> Element는 object 객체를 의미한다 <K> Key <V> Value 


Stack(Object)라 하면 다 받을 수 있는 건데

Stack <String> 이면 특정 타입으로 구체화. 

 


Vector<E>  안정 => 동일 => ArrayList<E> 불안정 

Vector<E>  Element는 object 객체를 의미한다  E는 1개만 지정 가능하다 

Vector에 삽입 가능한 것  객체, null 

기본 타입은 Wrapper 객체로 만들어 저장    

Vector에 객체 삽입  벡터의 맨 뒤에 객체 추가// 벡터 중간에 객체 삽입   

Vector에서 객체 삭제  임의의 위치에 있는 객체 삭제 가능 : 객체 삭제 후 자동 자리


ArrayList  = 라이브러리 = 만들어져 있는 놈 

ArrayList<E> 의 주요 메서드 기능 

boolean add (E element) 지정된 E가 맨뒤에 추가하는데 제대로 추가 되었으면 true, 아니면 false 

void add (int index, E element) 지정한 위치에 추가 (삽입)

int capacity () 용량을 (전체) 반환해주는데 int로 반환 

boolean addAll (Collection<? extends E> c)  콜렉션 인터페이스 interface

void clear() 저장된거 삭제 

boolean contains(Object o) 

E elementAt (int index) ~번째 정수 데이터 타입 

E get(int index) 해당 인덱스에 있는 값 추출, 즉 꺼낸다 

int indexOf (Object o) 오브젝트의 인덱스 반환 

boolean isEmpty() 비었니? 비어있으면 true, 비어있지 않으면 false. 즉 비어있지 않으면 get할 수 있다.

E remove (int index) 삭제된 object가 반환된다

boolean remove(Object o) 해당 object삭제

int size () 저장된 갯수

Object[] toArray() ~로 변환 


Methods in Java ArrayList

add(int index, Object element) This method is used to insert a specific element at a specific position index in a list. add(Object o) This method is used to append a specific element to the end of a list. 

addAll(Collection C) This method is used to append all the elements from a specific collection to the end of the mentioned list, in such an order that the values are returned by the specified collection’s iterator. 

addAll(int index, Collection C) Used to insert all of the elements starting at the specified position from a specific collection into the mentioned list. 

clear() This method is used to remove all the elements from any list. 

clone() This method is used to return a shallow copy of an ArrayList. 

contains?(Object o) Returns true if this list contains the specified element. 

ensureCapacity?(int minCapacity) Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. 

forEach?(Consumer<? super E> action) Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. 

get?(int index) Returns the element at the specified position in this list. 

indexOf(Object O) The index the first occurrence of a specific element is either returned, or -1 in case the element is not in the list. 

isEmpty?() Returns true if this list contains no elements. 

lastIndexOf(Object O) The index of the last occurrence of a specific element is either returned or -1 in case the element is not in the list. 

listIterator?() Returns a list iterator over the elements in this list (in proper sequence). 

listIterator?(int index) Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. 

remove?(int index) Removes the element at the specified position in this list. 

remove?(Object o) Removes the first occurrence of the specified element from this list, if it is present. 

removeAll?(Collection c) Removes from this list all of its elements that are contained in the specified collection. 

removeIf?(Predicate filter) Removes all of the elements of this collection that satisfy the given predicate. removeRange?(int fromIndex, int toIndex) Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. 

retainAll?(Collection<?> c) Retains only the elements in this list that are contained in the specified collection. 

set?(int index, E element) Replaces the element at the specified position in this list with the specified element. 

size?() Returns the number of elements in this list. 

spliterator?() Creates a late-binding and fail-fast Spliterator over the elements in this list. 

subList?(int fromIndex, int toIndex) Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. 

toArray() This method is used to return an array containing all of the elements in the list in the correct order. 

toArray(Object[] O) It is also used to return an array containing all of the elements in this list in the correct order same as the previous method. 

trimToSize() This method is used to trim the capacity of the instance of the ArrayList to the list’s current size.


wrapper class&amp;nbsp;

<Interger>  : wrapper 클래스 

 

자동 형변환 = 컴파일러에 의해 자동으로 박싱과 언박싱 일어난다 


 

() : 생성자 호출

new -> object생성 -> 공간 생성 

타입 매개변수 사용하지 않는 경우 (제네릭을 사용하지 않았다는 이야기, 즉 제한을 걸지 않았다) , 경고가 발생한다 , error가 발생하진 않는다.  = 즉 제네릭을 사용하라는 이야기를 길게 한 거임 -_-;; 

배열은 크기 조정이 되지 않는다 

오버로딩 - 이름은 같고 파라미터 다른 거 

interface도 클래스 

collection - set, list, queue

HashMap은 데이터 타입을 2개 넣을 수 있다 

인터페이스 - 강제적 오버라이딩 overriding 


index가 있는 놈 - ArrayList 

Iterator를 만들면 while문을 쓸 수 있다. 

boolean hasNext() 다음이 있니? 있으면 true 없으면 false 

E next() 꺼낸다 


HashMap (K, V) 

빠른검색, 키 key 와 값 value 쌍으로 구성되는 요소를 다루는 컬렉션 

반응형

'Java' 카테고리의 다른 글

자바 Java 스레드 thread  (0) 2021.12.13
자바 Java 상속 inheritance 다형성 polymorphism 개념 및 정리  (0) 2021.12.06
자바 Java 메소드 method  (0) 2021.12.02