package day24_MapTest;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class MapTest1 {
public static void main(String[] args) {
TreeMap<Integer, String> map = new TreeMap<Integer, String>();
map.put(1, "일요일");
map.put(2, "월요일");
map.put(3, "화요일");
map.put(4, "수요일");
map.put(5, "목요일");
map.put(6, "금요일");
map.put(7, "토요일");
Set<Integer> kset = map.keySet(); //키값들의 모임
// Iterator<Integer> it = kset.iterator();
// while(it.hasNext()) {
// Integer p = it.next();
// String pp = map.get(p);
// System.out.println(p+"\t"+pp);
// }
//외부에서 키값을 받아와서 ...
Scanner sc = new Scanner(System.in);
System.out.println("원하는 키값을 입력하시오");
int n = sc.nextInt();
String nn = map.get(n);
boolean yn = kset.contains(n);
if(yn) {
System.out.println(n+" 키값은 존재하고, "+nn+"입니다.");
}
else {
System.out.println(n+" 키값이 없습니다.");
}
}
}
package day24_MapTest;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
class SampleDTO {
String spname;
int count,cost;
int money;
public SampleDTO() {}
public SampleDTO(String spname, int count, int cost, int money) {
super();
this.spname = spname;
this.count = count;
this.cost = cost;
this.money = money;
}
public String getspname() {
return spname;
}
public void setspname(String spname) {
this.spname = spname;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
public class MapTest2 {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<SampleDTO> list = new ArrayList<SampleDTO>();
FileReader fr = new FileReader("c:/data/01051.txt");
BufferedReader br = new BufferedReader(fr);
while(true) {
try {
SampleDTO dto = new SampleDTO();
String str = br.readLine();
if(str==null)break;
dto.setspname(str.substring(0, 3));
dto.setCount(Integer.parseInt(str.substring(3, 5)));
dto.setCost(Integer.parseInt(str.substring(5, 9)));
dto.setMoney(dto.getCount()*dto.getCost());
list.add(dto);
} catch (IOException e) {
e.printStackTrace();
}
}
DecimalFormat df = new DecimalFormat("#,##0");
TreeMap<String, Integer> map = new TreeMap<String, Integer>();
// list에 저장 자료중에서 상품명과 매출액을 map구조에 저장
for(int i=0;i<list.size();i++) {
map.put(list.get(i).getspname(), list.get(i).getMoney());
}
// 키값만 모으기
Set<String> kset = map.keySet();
// 모은 키값을 순차적으로 정리
Iterator<String> it = kset.iterator();
System.out.println("상품명\t매출액\n===============");
//반복하면 키 값을 가져옴..키값으로 값을 가져오기
while(it.hasNext()) {
//키를 가져오기
String aa = it.next();
//값을 가져오기
Integer bb = map.get(aa);
System.out.println(aa+"\t"+df.format(bb)+"원");
}
//상품명을 외부에서 입력하여 매출액 찾기
System.out.println("===============\n");
System.out.println("상품명을 입력해주세요");
Scanner sc = new Scanner(System.in);
String cc = sc.next();
Integer dd = map.get(cc);
boolean yn = kset.contains(cc);
if(yn) {
System.out.println(cc+"의 매출액은 "+df.format(dd)+"원입니다.");
}
else {
System.out.println("키값이 없습니다");
}
}
}
package day24_MapTest;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
class Saram {
String name;
int age;
public Saram() {}
public Saram(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Saram [name=" + name + ", age=" + age + "]";
}
}
public class VectorTest {
public static void main(String[] args) {
Vector<Saram> list = new Vector<Saram>();
list.add(new Saram("홍길동",33));
list.add(new Saram("최최최",12));
list.add(new Saram("김김김",48));
list.add(new Saram("박박박",27));
System.out.println("=======람다식 출력");
list.forEach(s->System.out.println(s.toString()));
System.out.println("=======이더레이터 출력");
Iterator<Saram> it = list.iterator();
while(it.hasNext()) {
Saram pp = it.next();
String name = pp.name;
int age = pp.age;
System.out.println(name+"\t"+age);
}
System.out.println("=======Enumeration 출력");
Enumeration<Saram> en = list.elements();
while(en.hasMoreElements()) {
Saram pp = en.nextElement();
String name = pp.name;
int age = pp.age;
System.out.println(name+"\t"+age);
}
System.out.println("=======빠른 for 출력");
for(Saram a:list) {
System.out.println(a.toString());
}
System.out.println("=======for문 출력");
for(int i=0;i<list.size();i++) {
System.out.println(list.get(i));
}
}
}
package day24_MapTest;
import java.util.Random;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
TreeSet<Integer> list = new TreeSet<Integer>();
Random rd = new Random();
while(list.size()<=4) {
int n = rd.nextInt(100)+1;
list.add(n);
}
// for(int i=1;i<=5;i++) { // 1-100 난수 5개를 발생
// int n = rd.nextInt(100)+1;
// list.add(n);
// }
list.forEach(s->System.out.print(s+"\t"));
System.out.println("\n가장 낮은 숫자");
int fir=list.first();
System.out.println(fir);
System.out.println("가장 큰 숫자");
int las=list.last();
System.out.println(las);
}
}
package day24_MapTest;
import java.util.NavigableSet;
import java.util.Random;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
TreeSet<Integer> list = new TreeSet<Integer>();
Random rd = new Random();
while(list.size()<10) {
int n = rd.nextInt(45)+1;
list.add(n);
}
// for(int i=1;i<=5;i++) { // 1-100 난수 5개를 발생
// int n = rd.nextInt(100)+1;
// list.add(n);
// }
list.forEach(s->System.out.print(s+"\t")); // 오름차순
System.out.println();
NavigableSet<Integer> des = list.descendingSet(); // 내림차순
des.forEach(s->System.out.print(s+"\t"));
System.out.println();
NavigableSet<Integer> asub = list.subSet(20, true, 29, true); //입력한 숫자 사이의 값만 나오게
asub.forEach(s->System.out.print(s+"\t"));
System.out.println();
NavigableSet<Integer> des1 = des.descendingSet(); // 재오름차순
des1.forEach(s->System.out.print(s+"\t"));
}
}
package day24_MapTest;
import java.util.NavigableSet;
import java.util.Random;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
TreeSet<Integer> list = new TreeSet<Integer>();
Random rd = new Random();
while(list.size()<10) {
int n = rd.nextInt(45)+1;
list.add(n);
}
list.forEach(s->System.out.print(s+"\t")); // 오름차순 정열
NavigableSet<Integer> n0 = list.subSet(0, true, 9, true);
NavigableSet<Integer> n1 = list.subSet(10, true, 19, true);
NavigableSet<Integer> n2 = list.subSet(20, true, 29, true);
NavigableSet<Integer> n3 = list.subSet(30, true, 39, true);
NavigableSet<Integer> n4 = list.subSet(40, true, 45, true);
System.out.println("\n================");
System.out.println("1 ~ 9 : "+n0.size()+"개");
System.out.println("10 ~ 19 : "+n1.size()+"개");
System.out.println("20 ~ 29 : "+n2.size()+"개");
System.out.println("30 ~ 39 : "+n3.size()+"개");
System.out.println("40 ~ 45 : "+n4.size()+"개");
}
}
package day24_MapTest;
import java.awt.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
class MyHomeDTO {
//점수,이름
int jum;
String name;
public MyHomeDTO() {}
public MyHomeDTO(int jum, String name) {
super();
this.jum = jum;
this.name = name;
}
}
public class TreeMapTest {
public static void main(String[] args) {
TreeMap<Integer, String> list = new TreeMap<Integer, String>();
list.put(90, "홍길동");
list.put(65, "최최최");
list.put(78, "박박박");
list.put(84, "김김김");
list.put(51, "행행행");
// 점수가 가장 낮은
Map.Entry<Integer, String> ent=null;
ent = list.firstEntry();
System.out.println(ent.getKey()+"의 이름은 "+ent.getValue());
ent = list.lastEntry();
System.out.println(ent.getKey()+"의 이름은 "+ent.getValue());
System.out.println(list.lastEntry().getKey()+"의 이름은 "+list.lastEntry().getValue());
}
}
package day24_MapTest;
import java.awt.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Set;
import java.util.TreeMap;
class MyHomeDTO {
//점수,이름
int jum;
String name;
public MyHomeDTO() {}
public MyHomeDTO(int jum, String name) {
super();
this.jum = jum;
this.name = name;
}
}
public class TreeMapTest {
public static void main(String[] args) {
TreeMap<Integer, String> list = new TreeMap<Integer, String>();
list.put(90, "홍길동");
list.put(65, "최최최");
list.put(78, "박박박");
list.put(84, "김김김");
list.put(91, "행행행");
// 점수가 내림차순
Map.Entry<Integer, String> ent = null; //map구조로 숫자,문자 공간 확보
NavigableMap<Integer, String> des = list.descendingMap(); //역정열 내림차순
Set<Map.Entry<Integer, String>> des1 = des.entrySet();
des1.forEach(s->System.out.println(s));
// 점수별 오름차순 정열
System.out.println("=========");
NavigableMap<Integer, String> des2 = des.descendingMap();
Set<Map.Entry<Integer, String>> des3 = des2.entrySet();
des3.forEach(s->System.out.println(s));
}
}
package day24_MapTest;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
class DTO2 {
String name;
int kor,eng,math;
int total;
public DTO2() {}
public DTO2(String name, int kor, int eng, int math) {
super();
this.name = name;
this.kor = kor;
this.eng = eng;
this.math = math;
total = kor+eng+math;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
@Override
public String toString() {
return "DTO2 [name=" + name + ", kor=" + kor + ", eng=" + eng + ", math=" + math + ", total=" + total + "]";
}
}
public class TreeMapTest3 {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<DTO2> list = new ArrayList<DTO2>();
DTO2 dto = new DTO2();
list.add(new DTO2("한라산",10,99,25));
list.add(new DTO2("백두산",20,87,72));
list.add(new DTO2("금강산",50,10,38));
list.add(new DTO2("광교산",4,86,99));
list.add(new DTO2("속리산",99,96,93));
//가장 기본적이고 전체 자료 기억 list
//총점과 이름을 저장되는 TreeMap
TreeMap<Integer, String> tree = new TreeMap<Integer, String>();
for(int i=0;i<list.size();i++) {
tree.put(list.get(i).getTotal(), list.get(i).getName());
}
//키값들을 기억
Set<Integer> kset = tree.keySet();
Iterator<Integer> it = kset.iterator();
System.out.println("오름차순\n========");
while(it.hasNext()) {
Integer aa = it.next();
String bb = tree.get(aa);
System.out.println(aa+"\t"+bb);
}
Map.Entry<Integer, String> ent = null;
System.out.println("\n내림차순\n========");
NavigableMap<Integer, String> des = tree.descendingMap();
Set<Map.Entry<Integer, String>> des1 = des.entrySet();
des1.forEach(s->System.out.println(s.getKey()+"\t"+s.getValue()));
}
}
package day24_MapTest;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
class DTO {
String name;
int kor,eng,math;
int total;
public DTO() {}
public DTO(String name, int kor, int eng, int math) {
super();
this.name = name;
this.kor = kor;
this.eng = eng;
this.math = math;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
@Override
public String toString() {
return "DTO [name=" + name + ", kor=" + kor + ", eng=" + eng + ", math=" + math + ", total=" + total + "]";
}
}
public class TreeMapTest2 {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<DTO> list = new ArrayList<DTO>();
FileReader fr = new FileReader("c:/data/01052.txt");
BufferedReader br = new BufferedReader(fr);
while(true) {
String str;
try {
DTO dto = new DTO();
str = br.readLine();
if(str==null) break;
dto.setName(str.substring(0, 3));
dto.setKor(Integer.parseInt(str.substring(3, 6)));
dto.setEng(Integer.parseInt(str.substring(6, 9)));
dto.setMath(Integer.parseInt(str.substring(9, 12)));
dto.setTotal(dto.getKor()+dto.getEng()+dto.getMath());
list.add(dto);
}
catch (IOException e) {
e.printStackTrace();
}
}
TreeMap<Integer, String> tree = new TreeMap<Integer, String>();
for(int i=0;i<list.size();i++) {
tree.put(list.get(i).getTotal(), list.get(i).getName());
}
Set<Integer> kset = tree.keySet();
Iterator<Integer> it = kset.iterator();
// System.out.println("오름차순\n========");
while(it.hasNext()) {
Integer aa = it.next();
String bb = tree.get(aa);
System.out.println(aa+"\t"+bb);
}
Map.Entry<Integer, String> ent = null;
// System.out.println("\n내림차순\n========");
NavigableMap<Integer, String> des = tree.descendingMap();
Set<Map.Entry<Integer, String>> des1 = des.entrySet();
// des1.forEach(s->System.out.println(s.getKey()+"\t"+s.getValue()));
TreeSet<Integer> set = new TreeSet<Integer>();
for(int i=0;i<list.size();i++) {
set.add(list.get(i).getTotal());
}
NavigableSet<Integer> n0= set.subSet(0, true, 99, true);
NavigableSet<Integer> n1= set.subSet(100, true, 199, true);
NavigableSet<Integer> n2= set.subSet(200, true, 300, true);
System.out.println("\n점수 분포도\n=============");
System.out.println("0 ~ 99점: "+n0.size()+"\n100 ~ 199점: "+n1.size()+"\n200 ~ 300점: "+n2.size());
System.out.println("최고점: "+set.last()+"\n최저점: "+set.first());
}
}
package day24_MapTest;
import java.util.Stack;
class Stack1 {
int money;
public Stack1() {}
public Stack1(int money) {
super();
this.money = money;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
public class StackTest {
public static void main(String[] args) {
Stack<Stack1> st = new Stack<Stack1>(); //LIFO(last in, first out)
st.push(new Stack1(10000));
st.push(new Stack1(10));
st.push(new Stack1(400));
st.push(new Stack1(5500));
st.push(new Stack1(88000));
st.push(new Stack1(7770));
while(!st.empty()) {
Stack1 p = st.pop();
System.out.println("출력한 값"+p.getMoney());
}
}
}
package day24_MapTest;
import java.util.LinkedList;
import java.util.Queue;
class Money {
int don;
public Money() {}
public Money(int don) {
super();
this.don = don;
}
public int getDon() {
return don;
}
public void setDon(int don) {
this.don = don;
}
}
public class QueueTest {
public static void main(String[] args) {
Queue<Money> qq = new LinkedList<>();
qq.add(new Money(100));
qq.add(new Money(200));
qq.add(new Money(300));
qq.add(new Money(400));
qq.add(new Money(500));
qq.add(new Money(600));
while(!qq.isEmpty()) {
Money m = qq.poll();
System.out.println("출력값 : "+m.getDon());
}
}
}
'JAVA' 카테고리의 다른 글
230109_Stream (0) | 2023.01.09 |
---|---|
230106_Map (0) | 2023.01.06 |
230104_Map (0) | 2023.01.04 |
230103_Collection (0) | 2023.01.03 |
230102_Thread (0) | 2023.01.02 |
댓글