Ex01_ArrayList
package com.mywork.ex;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Ex01_ArrayList {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(new Integer(1));
list.add(new Integer(2));
list.add(5);
list.add(4);
list.add(3);
System.out.println("첫 번째 요소 : " + list.get(0));
System.out.println("두 번째 요소 : " + list.get(1));
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
list.remove(0);
List<Integer> list2 = new ArrayList<>();
list2.add(new Integer(5));
list2.add(new Integer(6));
list2.add(new Integer(7));
for(int i = 0; i < list2.size(); i++) {
if(list.contains(list2.get(i))) {
System.out.println(list2.get(i));
}
}
System.out.println("list = " + list);
Collections.sort(list);
System.out.println("정렬 후 list = " + list);
while(list.size() > 0) {
list.remove(0);
}
}
}
Ex02_Iterator
package com.mywork.ex;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Ex02_Iterator {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for(int i = 0; i < 10; i++) {
list.add((i + 1) * 10);
}
Iterator<Integer> itr = list.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Ex03_LinkedList
package com.mywork.ex;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Ex03_LinkedList {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<String> list = new LinkedList<String>();
for(int i = 0; i < 3; i++) {
System.out.print("이름 >> ");
String name = scanner.next();
list.add(name);
}
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
scanner.close();
}
}
Ex04_HashSet
package com.mywork.ex;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Ex04_HashSet {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
set.add("C");
set.add("Java");
set.add("DB");
set.add("JSP");
set.add("Web");
set.add("Spring");
set.add("Java");
Iterator<String> itr = set.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Ex05_HashSet
package com.mywork.ex;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
class User{
private String id;
private String pw;
public User(String id, String pw) {
this.id = id;
this.pw = pw;
}
@Override
public String toString() {
return "아이디 : " + id;
}
@Override
public int hashCode() {
return id.charAt(0) + pw.length();
}
@Override
public boolean equals(Object obj) {
if(obj != null && obj instanceof User) {
String otherId = ((User)obj).id;
return id.equals(otherId);
}else{
return false;
}
}
}
public class Ex05_HashSet {
public static void main(String[] args) {
Set<User> set = new HashSet<User>();
set.add(new User("admin", "1234"));
set.add(new User("admin", "1234"));
Iterator<User> itr = set.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Ex06_HashMap
package com.mywork.ex;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Ex06_HashMap {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("alice", 20);
map.put("james", 20);
map.put("tom", 25);
map.put("alice", 21);
map.remove("tom");
Set<String> set = map.keySet();
Iterator<String> itr = set.iterator();
while(itr.hasNext()) {
String key = itr.next();
Integer value = map.get(key);
System.out.println("키 : " + key + ", 값 : " + value);
}
itr = set.iterator();
while(itr.hasNext()) {
String key = itr.next();
Integer value = map.get(key);
System.out.println("키 : " + key + ", 값 : " + value);
}
System.out.println("entry 개수 : " + map.size());
Set<Map.Entry<String, Integer>> entries = map.entrySet();
Iterator<Map.Entry<String, Integer>> i = entries.iterator();
while(i.hasNext()) {
Map.Entry<String, Integer> entry = i.next();
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}