가장 큰 차이점은 동기화의 유무로써 StringBuffer는 동기화 키워드를 지원하여 멀티쓰레드 환경에서 안전하다는 점(thread-safe) 입니다. 참고로 String도 불변성을 가지기때문에 마찬가지로 멀티쓰레드 환경에서의 안정성(thread-safe)을 가지고 있습니다.
반대로 StringBuilder는 동기화를 지원하지 않기때문에 멀티쓰레드 환경에서 사용하는 것은 적합하지 않지만 동기화를 고려하지 않는 만큼 단일쓰레드에서의 성능은 StringBuffer 보다 뛰어납니다.
CreateListObject.java
package collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
public class CreateListObject {
public static void main(String[] args) {
// 1. List 구현 클래스의 생성자를 사용해 객체 생성
List<Integer> aList1 = new ArrayList<>(); // capacity = 10
List<Integer> aList2 = new ArrayList<>(30); // capacity = 30
List<Integer> aList3 = new Vector<>(); // capacity = 10
List<Integer> aList4 = new Vector<>(20); // capacity = 20
List<Integer> aList5 = new LinkedList<>(); // capacity = 10
// List<Integer> aList6 = new LinkedList<>(30); // 저장 용량 지정 불가
// 2. Arrays 클래스의 정적 메서드 활용
List<Integer> aList7 = new Arrays.asList(1, 2, 3, 4);
List<String> aList8 = new Arrays.asList("안녕", "방가");
aList7.set(1, 7);
aList8.set(0, "감사");
// aList7.add(5);
// aList8.remove(0); // 불가능
System.out.println(aList7);
System.out.println(aList8);
}
}
ArrayListMethod.java
package collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListMethod {
public static void main(String[] args) {
List<Integer> aList1 = new ArrayList<Integer>();
aList1.add(3);
aList1.add(4);
aList1.add(5);
System.out.println(aList1.toString());
aList1.add(1, 6);
System.out.println(aList1.toString());
List<Integer> aList2 = new ArrayList<>();
aList2.add(1);
aList2.add(2);
aList2.addAll(aList1);
System.out.println(aList2);
List<Integer> aList3 = new ArrayList<>();
aList3.add(1);
aList3.add(2);
aList3.addAll(1, aList3);
System.out.println(aList3);
aList3.set(1, 5);
aList3.set(3, 6);
System.out.println(aList3);
aList3.remove(1);
System.out.println(aList3);
aList3.remove(new Integer(2));
System.out.println(aList3);
aList3.clear();
System.out.println(aList3);
System.out.println(aList3.isEmpty());
System.out.println(aList3.size());
aList3.add(1);
aList3.add(2);
aList3.add(3);
System.out.println(aList3);
System.out.println(aList3.size());
System.out.println("0번째: " + aList3.get(0));
System.out.println("0번째: " + aList3.get(1));
System.out.println("0번째: " + aList3.get(2));
for(int i = 0; i < aList3.size(); i++)
System.out.println(i + "번째: " + aList3.get(i));
Object[] object = aList3.toArray();
System.out.println(Arrays.toString(object));
Integer[] integer1 = aList3.toArray(new Integer[0]);
System.out.println(Arrays.toString(integer1));
Integer[] integer2 = aList3.toArray(new Integer[5]);
System.out.println(Arrays.toString(integer2));
}
}
ArrayListVsLinkedList.java
package collection;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class ArrayListVsLinkedList {
public static void main(String[] args) {
List<Integer> aList = new ArrayList<>();
List<Integer> linkedList = new LinkedList<>();
long startTime = 0, endTime = 0;
startTime = System.nanoTime();
for(int i = 0; i < 100000; i++) {
aList.add(0, i);
}
endTime = System.nanoTime();
System.out.println("ArrayList 데이터 추가 시간 = " + (endTime-startTime) + " ns");
startTime = System.nanoTime();
for(int i = 0; i < 100000; i++) {
linkedList.add(0, i);
}
endTime = System.nanoTime();
System.out.println("LinkedList 데이터 추가 시간 = " + (endTime-startTime) + " ns");
startTime = System.nanoTime();
System.out.println("ArrayList 데이터 검색 시간 = " + (endTime-startTime) + " ns");
startTime = System.nanoTime();
for(int i = 0; i < 100000; i++) {
aList.remove(0);
}
endTime = System.nanoTime();
System.out.println("ArrayList 데이터 제거 시간 = " + (endTime-startTime) + " ns");
startTime = System.nanoTime();
for(int i = 0; i < 100000; i++) {
linkedList.remove(0);
}
endTime = System.nanoTime();
System.out.println("LinkedList 데이터 제거 시간 = " + (endTime-startTime) + " ns");
}
}
HashSetMethod.java
package collection;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class HashSetMethod {
public static void main(String[] args) {
Set<String> hSet1 = new HashSet<>();
hSet1.add("가");
hSet1.add("나");
hSet1.add("다");
System.out.println(hSet1.toString());
Set<String> hSet2 = new HashSet<>();
hSet2.add("나");
hSet2.add("다");
hSet2.addAll(hSet1);
System.out.println(hSet2.toString());
hSet2.remove("나");
System.out.println(hSet2.toString());
hSet2.clear();
System.out.println(hSet2.toString());
System.out.println(hSet2.isEmpty());
Set<String> hSet3 = new HashSet<>();
hSet3.add("가");
hSet3.add("나");
hSet3.add("다");
System.out.println(hSet3.contains("나"));
System.out.println(hSet3.contains("라"));
System.out.println(hSet3.size());
Iterator<String> iterator = hSet3.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
Object[] objArray = hSet3.toArray();
System.out.println(Arrays.toString(objArray));
String[] strArray1 = hSet3.toArray(new String[0]);
System.out.println(Arrays.toString(strArray1));
String[] strArray2 = hSet3.toArray(new String[5]);
System.out.println(Arrays.toString(strArray2));
}
}
LinkedHashSetMethod.java
package collection;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
public class LinkedHashSetMethod {
public static void main(String[] args) {
Set<String> linkedSet1 = new LinkedHashSet<>();
linkedSet1.add("가");
linkedSet1.add("나");
linkedSet1.add("다");
System.out.println(linkedSet1.toString());
Set<String> linkedSet2 = new LinkedHashSet<>();
linkedSet2.add("나");
linkedSet2.add("다");
linkedSet2.addAll(linkedSet1);
System.out.println(linkedSet2.toString());
linkedSet2.remove("나");
System.out.println(linkedSet2.toString());
linkedSet2.clear();
System.out.println(linkedSet2.toString());
System.out.println(linkedSet2.isEmpty());
Set<String> linkedSet3 = new LinkedHashSet<>();
linkedSet3.add("가");
linkedSet3.add("나");
linkedSet3.add("다");
System.out.println(linkedSet3.contains("나"));
System.out.println(linkedSet3.contains("라"));
System.out.println(linkedSet3.size());
Iterator<String> iterator = linkedSet3.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
Object[] objArray = linkedSet3.toArray();
System.out.println(Arrays.toString(objArray));
String[] strArray1 = linkedSet3.toArray(new String[0]);
System.out.println(Arrays.toString(strArray1));
String[] strArray2 = linkedSet3.toArray(new String[5]);
System.out.println(Arrays.toString(strArray2));
}
}
TreeSetMethod_1.java
package collection;
import java.util.Comparator;
import java.util.TreeSet;
class MyComparableClass implements Comparable<MyComparableClass> {
int data1;
int data2;
public MyComparableClass(int data1, int data2) {
this.data1 = data1;
this.data2 = data2;
}
@Override
public int compareTo(MyComparableClass other) {
if (this.data1 < other.data1) {
return -1;
} else if (this.data1 == other.data1) {
return 0;
} else {
return 1;
}
}
}
class MyClass {
int data1;
int data2;
public MyClass(int data1, int data2) {
this.data1 = data1;
this.data2 = data2;
}
}
public class TreeSetMethod_2 {
public static void main(String[] args) {
TreeSet<Integer> treeSet1 = new TreeSet<Integer>();
Integer intValue1 = new Integer(20);
Integer intValue2 = new Integer(10);
treeSet1.add(intValue1);
treeSet1.add(intValue2);
System.out.println(treeSet1.toString());
TreeSet<String> treeSet2 = new TreeSet<String>();
String str1 = "가나";
String str2 = "다라";
treeSet2.add(str1);
treeSet2.add(str2);
System.out.println(treeSet2.toString());
TreeSet<MyComparableClass> treeSet4 = new TreeSet<MyComparableClass>();
MyComparableClass myComparableClass1 = new MyComparableClass(2, 5);
MyComparableClass myComparableClass2 = new MyComparableClass(3, 3);
treeSet4.add(myComparableClass1);
treeSet4.add(myComparableClass2);
for (MyComparableClass mcc : treeSet4) {
System.out.println(mcc.data1);
}
TreeSet<MyClass> treeSet5 = new TreeSet<MyClass>(new Comparator<MyClass>() {
@Override
public int compare(MyClass o1, MyClass o2) {
if (o1.data1 < o2.data1) return -1;
else if (o1.data1 == o2.data1) return 0;
else return 1;
}
});
MyClass myClass1 = new MyClass(2, 5);
MyClass myClass2 = new MyClass(3, 3);
treeSet5.add(myClass1);
treeSet5.add(myClass2);
for (MyClass mcc : treeSet5) {
System.out.println(mcc.data1);
}
}
}
HashMapMachanism.java
package collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
class A {
int data;
public A(int data) {
this.data = data;
}
@Override
public int hashCode() {
return Objects.hash(data);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof A) {
A other = (A) obj;
return this.data == other.data;
}
return false;
}
}
class B {
int data;
public B(int data) {
this.data = data;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof B) {
B other = (B) obj;
return this.data == other.data;
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(data);
}
}
class C {
int data;
public C(int data) {
this.data = data;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof C) {
C other = (C) obj;
return this.data == other.data;
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(data);
}
}
public class HashMapMachanism {
public static void main(String[] args) {
Map<A, String> hashMap1 = new HashMap<>();
A a1 = new A(3);
A a2 = new A(3);
System.out.println(a1 == a2);
System.out.println(a1.equals(a2));
System.out.println(a1.hashCode() + ", " + a2.hashCode());
hashMap1.put(a1, "첫 번째");
hashMap1.put(a2, "두 번째");
System.out.println("hashMap1.size(): " + hashMap1.size());
System.out.println();
Map<B, String> hashMap2 = new HashMap<>();
B b1 = new B(3);
B b2 = new B(3);
System.out.println(b1 == b2);
System.out.println(b1.equals(b2));
System.out.println(b1.hashCode() + ", " + b2.hashCode());
hashMap2.put(b1, "첫 번째");
hashMap2.put(b2, "두 번째");
System.out.println("hashMap2.size(): " + hashMap2.size());
System.out.println();
Map<C, String> hashMap3 = new HashMap<>();
C c1 = new C(3);
C c2 = new C(3);
System.out.println(c1 == c2);
System.out.println(c1.equals(c2));
System.out.println(c1.hashCode() + ", " + c2.hashCode());
hashMap3.put(c1, "첫 번째");
hashMap3.put(c2, "두 번째");
System.out.println("hashMap3.size(): " + hashMap3.size());
}
}
TreeMapMethod_1.java
package collection;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.SortedMap;
import java.util.TreeMap;
public class TreeMapMethod_1 {
public static void main(String[] args) {
TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();
for(int i = 20; i > 9; i -= 2) {
treeMap.put(i, i+"번째 데이터");
}
System.out.println(treeMap.toString());
System.out.println(treeMap.firstKey());
System.out.println(treeMap.firstEntry());
System.out.println(treeMap.lastKey());
System.out.println(treeMap.lastEntry());
System.out.println(treeMap.lowerKey(11));
System.out.println(treeMap.lowerKey(10));
System.out.println(treeMap.higherKey(11));
System.out.println(treeMap.higherKey(10));
System.out.println(treeMap.pollFirstEntry());
System.out.println(treeMap.toString());
System.out.println(treeMap.pollLastEntry());
System.out.println(treeMap.toString());
SortedMap<Integer, String> sortedMap = treeMap.headMap(8);
System.out.println(sortedMap);
NavigableMap<Integer, String> navigableMap = treeMap.headMap(8, true);
System.out.println(navigableMap);
sortedMap = treeMap.tailMap(14);
System.out.println(sortedMap);
navigableMap = treeMap.tailMap(14, false);
System.out.println(navigableMap);
sortedMap = treeMap.subMap(6, 10);
System.out.println(sortedMap);
navigableMap = treeMap.subMap(6, false, 10, true);
System.out.println(navigableMap);
NavigableSet<Integer> navigableSet = treeMap.descendingKeySet();
System.out.println(navigableSet.toString());
System.out.println(navigableSet.descendingSet());
navigableMap = treeMap.descendingMap();
System.out.println(navigableMap.toString());
System.out.println(navigableMap.descendingMap());
}
}
QueueMethod.java
package collection;
import java.util.LinkedList;
import java.util.Queue;
public class QueueMethod {
public static void main(String[] args) {
Queue<Integer> queue1 = new LinkedList<Integer>();
queue1.add(3);
queue1.add(3);
queue1.add(3);
System.out.println(queue1.element());
System.out.println(queue1.remove());
System.out.println(queue1.remove());
System.out.println(queue1.remove());
System.out.println();
Queue<Integer> queue2 = new LinkedList<Integer>();
System.out.println(queue1.peek());
queue2.offer(3);
queue2.offer(4);
queue2.offer(5);
System.out.println(queue2.peek());
System.out.println(queue2.poll());
System.out.println(queue2.poll());
System.out.println(queue2.poll());
System.out.println(queue2.poll());
}
}
FunctionToLambdaExpression.java
package collection;
interface AA {
void method1();
}
interface BB {
void method2(int a);
}
interface CC {
int method3();
}
interface DD {
double method4(int a, double b);
}
public class FunctionToLambdaExpression {
public static void main(String[] args) {
AA a1 = new AA() {
@Override
public void method1() {
System.out.println("입력 x 리턴 x 함수");
}
};
AA a2 = () -> {System.out.println("입력 x 리턴 x 함수");};
AA a3 = () -> System.out.println("입력 x 리턴 x 함수");
BB b1 = new BB() {
@Override
public void method2(int a) {
System.out.println("입력 O 리턴 x 함수");
}
};
BB b2 = (int a) -> {System.out.println("입력 O 리턴 x 함수");};
BB b3 = (a) -> {System.out.println("입력 O 리턴 x 함수");};
BB b4 = (a) -> System.out.println("입력 O 리턴 x 함수");
BB b5 = a -> System.out.println("입력 O 리턴 x 함수");
CC c1 = new CC() {
@Override
public int method3() {
return 4;
}
};
CC c2 = () -> {return 4;};
CC c3 = () -> 4;
DD d1 = new DD() {
@Override
public double method4(int a, double b) {
return a + b;
}
};
DD d2 = (int a, double b) -> {return a + b;};
DD d3 = (a, b) -> {return a + b;};
DD d4 = (a, b)-> a + b;
}
}
RefOfIntanceMethod_Type1_1.java
package collection;
interface AAAA {
void abc();
}
class BBBB {
void bcd() {
System.out.println("메서드");
}
}
public class RefOfIntanceMethod_Type1_1 {
public static void main(String[] args) {
AAAA a1 = new AAAA() {
@Override
public void abc() {
BBBB b = new BBBB();
b.bcd();
}
};
AAAA a2 = () -> {
BBBB b = new BBBB();
b.bcd();
};
BBBB b = new BBBB();
AAAA a3 = b::bcd;
a1.abc();
a2.abc();
a3.abc();
}
}
RefOfStaticMethod.java
package collection;
interface AAAAA {
void abc();
}
class BBBBB {
static void bcd() {
System.out.println("메서드");
}
}
public class RefOfStaticMethod {
public static void main(String[] args) {
AAAAA a1 = new AAAAA() {
@Override
public void abc() {
BBBBB.bcd();
}
};
AAAAA a2 = () -> {BBBBB.bcd();};
AAAAA a3 = BBBBB::bcd;
a1.abc();
a2.abc();
a3.abc();
}
}
[참고자료]