반응형
Q1. Collection 종합
package com.dinfree_1001;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
public class CollectionTest {
public static void main(String[] args) {
//Create a new HashSet and add data
Collection<String> c1=new HashSet<>();
c1.add("one");
c1.add("two");
//Create a new List with two values
Collection<String> c2=Arrays.asList("three", "four");
Collection<String> c3=Collections.singleton("five");
//add all data in c2, c3 to c1
c1.addAll(c2);
c1.addAll(c3);
//get size of collection
System.out.println("Size of c1 : "+ c1.size());
//convert collection to array
Object[] converted1 = c1.toArray();
String[] converted2= c1.toArray(new String[c1.size()]);
//print all data in collection using for loop
for(String s:c1) {
System.out.println(s);
}
//access and print all data in the array which converted from collection
for(int i=0;i<converted2.length; i++) {
System.out.println(converted2[i]);
}
//get Iterator object from collection
Iterator iter=c1.iterator();
while(iter.hasNext()) {
System.out.println(iter.next());
}
//run specific code using every elements in the collection
c1.forEach(System.out::println);
}
}
Q2. ArrayList, LinkedList 생성과 데이터다루기
package com.dinfree_1001;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
public class ListTest {
public static void main(String[] args) {
//create new List
List<String> l1 = new ArrayList<>();
List<String> l2= Arrays.asList("one", "two");
List<String> l3= List.of("three", "four");
//add data to List
l1.addAll(l2);
l1.addAll(1, l3);
l1.add("five");
System.out.println("## element in List");
System.out.println(l1);
//create new LinkedList and add data
LinkedList<String> llist=new LinkedList<>();
llist.addAll(l2);
llist.addAll(1,l3);
llist.add("five");
System.out.println("## element in LinkedList");
System.out.println(l1);
//access data with index
System.out.println("## first element: "+ l1.get(0));
System.out.println("## last index of three: "+ l1.lastIndexOf("three"));
//change data in List
l1.set(0, "zero");
System.out.println("## after set (): element in LinkedList");
System.out.println(l1);
//Descending sort
Collections.sort(l1);
System.out.println("## Descending sort of l1");
System.out.println(l1);
//Ascending sort
l1.sort(new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
return o2.toString().compareTo(o1.toString());
}
});
System.out.println("## Ascending sort of l1");
System.out.println(l1);
//Ascending sort with stream api
System.out.println("## Ascending sort with stream api");
l1.stream().sorted((o1,o2)->o2.toString().compareTo(o1.toString())).forEach(System.out::println);
}
}
Q3. Set 객체 생성과 데이터 다루기
package com.dinfree_1001;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
public class SetTest {
public static void main(String[] args) {
//create new Set
Set<String> s1=new HashSet<>();
Set<String> s2=Set.of("three", "four");
//add element to Set
s1.addAll(Arrays.asList("one","two"));
s1.addAll(s2);
s1.add("five");
s1.add("two");
s1.remove("five");
System.out.println("## element in Set");
System.out.println(s1);
//print all elements using stream api
s1.stream().forEach(System.out::println);
System.out.println("## check exist element in set");
System.out.println(s1.contains("one"));
//create new LinkedHashSet and add elements
Set<String> lset=new LinkedHashSet<>();
lset.addAll(Arrays.asList("one","two","three","four"));
lset.add("five");
System.out.println("\n## element in LinkedHashSet");
System.out.println(lset);
//get Iterator from LinkedHashSet
System.out.println("## print element using Iterator");
Iterator<String> iter=lset.iterator();
while(iter.hasNext()) {
System.out.println(iter.next());
}
//create new TreeSet and add elements
Set<Integer> tset=new TreeSet<>();
tset.addAll(Arrays.asList(50,10,60,20));
System.out.println("\n ## elements in TreeSet");
System.out.println(tset);
//Descending sort with stream api
System.out.println("## Ascending sort with stream api");
tset.stream().sorted((o1, o2) -> o2.toString().compareTo(o1.toString())).forEach(System.out::println);
}
}
Q4. Map
package com.dinfree_1001;
import java.util.HashMap;
import java.util.Map;
public class MapTest {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("1922876", "Apple iPhone");
map.put("1922877", "Apple iPad");
map.put("2136861", "Samsung Galaxy");
map.put("2136863","Samsung Tablet");
System.out.println("1922877: "+map.get("1922877"));
System.out.println("--------");
for(Map.Entry<String, String> entry:map.entrySet()){
System.out.printf("%s:%s\n", entry.getKey(), entry.getValue());
}
System.out.println("--------");
for(String s: map.keySet()) {
System.out.printf("%s\n", s);
}
System.out.println("--------");
for(String s:map.values()) {
System.out.printf("%s\n",s);
}
}
}
Q5. Map과 List
package com.dinfree_1001;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class MapListTest {
public static void main(String[] args) {
List<Integer> s1 = Arrays.asList(95,89,93,87,94);
List<Integer> s2=Arrays.asList(99,89,91,89,91);
List<Integer> s3=Arrays.asList(93,81,95,88,89);
Map<String, List<Integer>> student = new HashMap<>();
student.put("홍길동", s1);
student.put("김사랑", s2);
student.put("아무개", s3);
Scanner scan = new Scanner(System.in);
System.out.println("## 성적 조회할 이름을 입력하세요: ");
String input = scan.next();
int total=0;
for(int s: student.get(input)) {
total+=s;
}
System.out.printf("총점: %d, 평균: %d", total, total/5);
}
}
반응형
'Java > Java 2' 카테고리의 다른 글
[Java] 7. 입출력 프로그래밍 (0) | 2021.11.15 |
---|---|
[Java] 6. 자료구조와 컬렉션프레임워크 (0) | 2021.10.01 |
[Java 실습] 5. 자바 중급 활용 (0) | 2021.10.01 |
[Java] 5. 자바 중급 활용 (0) | 2021.07.16 |
[Java 실습] 4. 객체지향 개념과 자바 (0) | 2021.07.15 |