자바/인프런 인강

자바 인프런 - Collections(자료구조)

Gamcho 2018. 2. 14. 23:09

콜렉션은 쉽게 말해서 자료구조 이다.

자바는 다양한 자료구조형을 제공하고 있다. 데이터의 성질에 따라서 관리하는 방식도 다르기 때문에.

자료구조형에서는 객체의 레퍼런스(주소값)만 관리한다.



-ArrayList

인덱스 사용, 데이터 중복 가능


ArrayList<String> arraylist = new ArrayList<String>();


arraylist.add("str1");

arraylist.add("str2");

arraylist.add("str3");

arraylist.add("str4");

arraylist.add(4, "str0");


System.out.println(arraylist.toString());


//[str1, str2, str3, str4, str0]



arraylist.set(2, "str222");


//[str1, str222, str3, str4, str0]



arraylist.remove(2);


//[str1, str3, str4, str0]



arraylist.size();


arraylist.clear();


arraylist = null;



-LinkedList
arraylist와 비슷하다. 데이터를 가져오는 속도가 좀 더 빠르다.

-Vector
arraylist보다 속도는 떨어지지만 멀티스레드 환경에서 더 안전하다. 


-HashMap
list 계열과 다르게 인덱스가 없고 데이터를 찾을 수 있는 키 값을 이용한다. 값은 중복될 수 있지만 키는 중복될 수 없다.



HashMap <String, String> hashmap = new HashMap<String, String>();

hashmap.put("one", "str1");

hashmap.put("two", "str1");

hashmap.put("three", "str1");

hashmap.put("four", "str1");

System.out.println(hashmap.toString());

//{four=str1, one=str1, two=str1, three=str1}

hashmap.remove("two");

System.out.println(hashmap.toString());

//{four=str1, one=str1, three=str1}

hashmap.clear();

Iterator<String> iterator = hashmap.keySet().iterator();

while (iterator.hasNext()) {

System.out.println(hashmap.get(iterator.next()));

    //str1

    //str1

    //str1


-HashSet

Set계열 자료구조는 데이터의 순서가 없다. 데이터를 다시 출력하면 입력한 순서가 뒤바뀐다. 그리고 데이터는 중복될 수 없다. 

hash 값으로 데이터를 관리한다.

hash는 데이터의 주소값을 정수화 시킨것.



-기본문법

HashSet<String> hashSet = new HashSet<String>();

hashSet.add("str4");

hashSet.add("str2");

hashSet.add("str1");

hashSet.add("str3");

System.out.println(hashSet.toString());

//[str3, str4, str1, str2]

hashSet.remove("str3");

System.out.println(hashSet.toString());

//[str4, str1, str2]

System.out.println(hashSet.size());

//3


-응용문법

자료형이 직접 생성한 클래스 객체일 경우 아래와 같이 추가하고 출력하면 객체 자료형과 해쉬값이 출력되므로


HashSet<Student> hashSet = new HashSet<Student>(); 


hashSet.add(new Student("홍길동", 3)); 

hashSet.add(new Student("이순신", 1)); 

hashSet.add(new Student("장보고", 6)); 

System.out.println(hashSet.toString());


 //[Student@161cd475, Student@57fa26b7, Student@532760d8]



-아래와 같이 Student 클래스에서 toString() 메소드를 오버라이드 해줘야한다.


public class Student {

private String name;

private int grade;

public Student(String name, int grade) {

this.name = name;

this.grade =grade;

}

@Override

public String toString() {

return name + ":" + grade;


System.out.println(hashSet.toString());

//[홍길동:3, 장보고:6, 이순신:1]



-데이터를 삭제하려면?


hashSet.remove(new Student("홍길동", 3));

System.out.println(hashSet.toString());



-위 처럼 할 경우, 새로 생성된 객체의 주소값은 달라지기 때문에 기존 데이터는 지워지지 않는다. 일치하는 기존 데이터의 해쉬코드를 구해서 삭제해야 한다.

Student 클래스의 아래 코드를 추가한다.



@Override

public boolean equals(Object obj) {

String compareValue = obj.toString();

String thisValue = toString();

return thisValue.equals(compareValue);

}

@Override

public int hashCode() {

return toString().hashCode();



-결과는



hashSet.remove(new Student("홍길동", 3));

System.out.println(hashSet.toString());


//[장보고:6, 이순신:1]