package demo.properties;

import java.io.FileReader;
import java.util.Properties;

public class PropertiesDemo {

	public static void main(String[] args) throws Exception {
		Properties prop = new Properties();

		FileReader reader = new FileReader("src/demo/properties/config.properties");
		
		// void load(Reader reader)
		// FileReader객체를 사용해서 파일의 정보를 읽어온다.
		prop.load(reader);
		
		// String getProperty(String key)
		// 프로퍼티 파일에서 지정된 키값으로 설정된 값을 반환한다.
		String value1 = prop.getProperty("user.name");
		System.out.println(value1);
		
		String value4 = prop.getProperty("select.users");
		System.out.println(value4);
	}
}
package demo.etc;

import java.util.LinkedList;

public class QueueDemo {

	public static void main(String[] args) {
		LinkedList<String> queue = new LinkedList<String>();
		
		queue.offer("홍길동");		
		queue.offer("김유신");		
		queue.offer("강감찬");		
		queue.offer("이순신");
		
		String value1 = queue.poll();
		System.out.println(value1);
		System.out.println("현재 개수 : " + queue.size());
		
		String value2 = queue.poll();
		System.out.println(value2);
		System.out.println("현재 개수 : " + queue.size());

	}
}
package demo.etc;

import java.util.Stack;

public class StackDemo {

	public static void main(String[] args) {
		
		Stack<String> stack = new Stack<String>();
		
		stack.push("홍길동");
		stack.push("김유신");
		stack.push("강감찬");
		stack.push("이순신");
		
		String value1 = stack.pop();
		System.out.println(value1);
		System.out.println("현재 개수 : " + stack.size());
		
		String value2 = stack.pop();
		System.out.println(value2);
		System.out.println("현재 개수 : " + stack.size());

	}
}

'자바 > collection' 카테고리의 다른 글

sort  (0) 2019.06.10
set  (0) 2019.06.10
map  (0) 2019.06.10
list  (0) 2019.06.10
package demo.sort;

import java.util.ArrayList;
import java.util.Collections;

public class ArrayListDemo1 {

	public static void main(String[] args) {
		ArrayList<User> users = new ArrayList<User>();
		users.add(new User(10, "김유신"));
		users.add(new User(80, "이순신"));
		users.add(new User(40, "유관순"));
		users.add(new User(50, "홍길동"));
		users.add(new User(20, "강감찬"));
		
		Collections.sort(users);
		
		for (User user : users) {
			System.out.println(user.getNo() + ", " + user.getName());
		}
	}
}
package demo.sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class ArrayListDemo2 {

	public static void main(String[] args) {
		ArrayList<Fruit> box = new ArrayList<Fruit>();
		box.add(new Fruit("사과", 2000));
		box.add(new Fruit("바나나", 6000));
		box.add(new Fruit("오렌지", 3000));
		box.add(new Fruit("아보카도", 8000));
		box.add(new Fruit("토마토", 4000));
		box.add(new Fruit("감", 9000));
		
		Comparator<Fruit> priceComparator = new Comparator<Fruit>() {
			@Override
			public int compare(Fruit o1, Fruit o2) {
				return o1.getPrice() - o2.getPrice();
			}
		};
		
		Collections.sort(box, priceComparator);
		
		for (Fruit fruit : box) {
			System.out.println(fruit.getName() + " " + fruit.getPrice());
		}
	}
}
package demo.sort;

import java.util.TreeSet;

public class TreeSetDemo {

	public static void main(String[] args) {
		TreeSet<User> users = new TreeSet<User>();
		
		users.add(new User(10, "김유신"));
		users.add(new User(80, "이순신"));
		users.add(new User(40, "유관순"));
		users.add(new User(50, "홍길동"));
		users.add(new User(20, "강감찬"));
		
		for (User user : users) {
			System.out.println(user.getNo() + ", " + user.getName());
		}		
	}
}
package demo.sort;

public class User implements Comparable<User>{

	private int no;
	private String name;
	
	public User() {}
	public User(int no, String name) {
		this.no = no;
		this.name = name;
	}

	public int getNo() {
		return no;
	}
	
	public void setNo(int no) {
		this.no = no;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public int compareTo(User o) {
		return name.compareTo(o.name);
	}
    
}
package demo.sort;

public class Fruit {

	private String name;
	private int price;
	
	public Fruit() {}
	public Fruit(String name, int price) {
		this.name = name;
		this.price = price;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public int getPrice() {
		return price;
	}
	
	public void setPrice(int price) {
		this.price = price;
	}
}

'자바 > collection' 카테고리의 다른 글

properties, queue, stack  (0) 2019.06.10
set  (0) 2019.06.10
map  (0) 2019.06.10
list  (0) 2019.06.10
package demo.set;

import java.util.HashSet;
import java.util.Iterator;

public class HashSetDemo1 {

	public static void main(String[] args) {
		HashSet<String> names = new HashSet<String>();
		
		names.add("홍길동");
		names.add("김유신");
		names.add("강감찬");
		names.add("홍길동");
		names.add("이순신");
		names.add("유관순");
		
		Iterator<String> it = names.iterator();
		while (it.hasNext()) {
			String name = it.next();
			System.out.println(name);
		}
		System.out.println();
		names.clear();
		
		System.out.println("비어 있는가? " + names.isEmpty());
		System.out.println("저장된 개수 : " + names.size());
		
		for (String name : names) {
			System.out.println(name);
			
		}
	}
}
package demo.set;

import java.util.HashSet;

public class HashSetDemo2 {

	public static void main(String[] args) {
		
		// Contact에서 hashCode()와 equals() 메소드를 재정의해서
		// 객체가 달라도 번호가 동일하면 동일한 객체로 간주하도록 만들었다.
		
		Contact contact1 = new Contact(10, "홍길동", "010-1111-1111");
		Contact contact2 = new Contact(20, "김유신", "010-2222-1111");
		Contact contact3 = new Contact(10, "홍길동", "010-1111-1111");
		Contact contact4 = new Contact(40, "이순신", "010-4444-1111");
		
		HashSet<Contact> contacts = new HashSet<Contact>();
		contacts.add(contact1);
		contacts.add(contact2);
		contacts.add(contact3);
		contacts.add(contact4);
		contacts.add(contact1);
		contacts.add(contact2);

		System.out.println(contacts);
	}
}
package demo.set;

import java.util.TreeSet;

public class TreeSetDemo {

	public static void main(String[] args) {
		TreeSet<String> names = new TreeSet<String>();
		
		names.add("이순신");
		names.add("김유신");
		names.add("강감찬");
		names.add("홍길동");
		names.add("유관순");
		names.add("김구");
		
		for (String name : names) {
			System.out.println(name);
		}
	}
}
package demo.set;

import java.util.Random;
import java.util.TreeSet;

public class TreeSetDemo2 {

	public static void main(String[] args) {
		TreeSet<Integer> lotto = new TreeSet<Integer>();
		Random random = new Random();
		
		while (true) {
			int number = random.nextInt(45) + 1;
			lotto.add(number);
			
			if (lotto.size() == 6) {
				break;
			}
		}
		System.out.println(lotto);
	}
}
package demo.set;

public class Contact {

	private int no;
	private String name;
	private String tel;
	
	public Contact() {}

	public Contact(int no, String name, String tel) {
		super();
		this.no = no;
		this.name = name;
		this.tel = tel;
	}

	public int getNo() {
		return no;
	}

	public void setNo(int no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	@Override
	public String toString() {
		return "Contact [no=" + no + ", name=" + name + ", tel=" + tel + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + no;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Contact other = (Contact) obj;
		if (no != other.no)
			return false;
		return true;
	}
	
}

'자바 > collection' 카테고리의 다른 글

properties, queue, stack  (0) 2019.06.10
sort  (0) 2019.06.10
map  (0) 2019.06.10
list  (0) 2019.06.10
package demo.map;

import java.util.HashMap;

public class HashMapDemo1 {

	public static void main(String[] args) {
		
		HashMap<Integer, String> map = new HashMap<Integer, String>();
		
		// put(K key, V value)
		// Map객체에 키, 값, 쌍으로 저장하기
		map.put(100, "홍길동");
		map.put(101, "김유신");
		map.put(102, "강감찬");
		map.put(103, "이순신");
		
		// V get(Object key)
		// Map객체에서 지정된 키에 해당하는 값 꺼내기
		String value1 = map.get(101);
		System.out.println(value1);
		
		// V remove(Object key)
		// Map객체에서 지정된 키에 해당하는 값을 반환하고 삭제하기
		map.remove(102);
		
		boolean empty = map.isEmpty();
		System.out.println("비어 있는가? " + empty);
		
		int len = map.size();
		System.out.println("저장된 개수: " + len);
		map.clear();
		System.out.println(map);
	}
}
package demo.map;

import java.util.HashMap;
import java.util.HashSet;

public class HashMapDemo2 {

	public static void main(String[] args) {
		
		HashMap<String, HashSet<String>> map = new HashMap<String, HashSet<String>>();
		
		HashSet<String> team1 = new HashSet<String>();
		team1.add("바다");
		team1.add("유진");
		team1.add("슈");
		
		HashSet<String> team2 = new HashSet<String>();
		team2.add("솔라");
		team2.add("문별");
		team2.add("화사");
		team2.add("휘인");

		HashSet<String> team3 = new HashSet<String>();
		team3.add("웬디");
		team3.add("아이린");
		team3.add("슬기");
		team3.add("조이");
		team3.add("예리");
		
		map.put("SES", team1);
		map.put("마마무", team2);
		map.put("레드벨벳", team3);
		
		HashSet<String> names = map.get("마마무");
		for (String name : names) {
			System.err.println(name);
		}
	}
}

'자바 > collection' 카테고리의 다른 글

properties, queue, stack  (0) 2019.06.10
sort  (0) 2019.06.10
set  (0) 2019.06.10
list  (0) 2019.06.10
package demo.list;

import java.util.ArrayList;

public class ArrayListDemo1 {

	public static void main(String[] args) {
		
		ArrayList<String> names = new ArrayList<String>();
		
		// 객체 저장하기
		// boolean add(E e)
		names.add("홍길동");     //names[0] = "홍길동";
		names.add("김유신");     //names[1] = "김유신";
		names.add("강감찬");     //names[2] = "강감찬";
		names.add("이순신");     //names[3] = "이순신";
		names.add("유관순");     //names[4] = "유관순";
		names.add("유관순");     //names[4] = "유관순";
		names.add("유관순");     //names[4] = "유관순";

		System.out.println(names);
		
		// 지정된 위치의 객체 삭제
		// E remove(int index)
		names.remove(2); // 강감찬 삭제
		
		// 지정된 객체를 삭제
		// boolean remove(Object o)
		names.remove("유관순"); 	// 유관순 삭제
		
		// 지정된 인덱스에 저장된 객체 꺼내기
		// E get(int index)
		String value1 = names.get(0);
		System.out.println("0번째 저장된 객체: " + value1);
		
		
		
		
		// ArrayList에 저장된 모든 객체 꺼내기
		// enhanced for문 사용
		for (String x : names) {
			System.out.println(x);
		}

		// int size()
		// 저장된 객체의 개수를 반환
		int len = names.size();
		System.out.println("저장된 객체의 개수: " + len);
		
		// void clear()
		// 저장된 객체 전체 삭제
		names.clear();
		
		// boolean isEmpty()
		boolean empty = names.isEmpty();
		System.out.println("비어있는가? " + empty);	
	}	
}
package demo.list;

import java.util.ArrayList;

public class ArrayListDemo2 {

	public static void main(String[] args) {
		
		// 기본 자료형을 저장하는 ArrayList
		// 기본 자료형을 저장할 때는 제네릭 타입을 Wrapper 클래스 타입으로 지정한다.
		// int --> Integer, double --> Double, long --> Long, ...
		
		ArrayList<Integer> numbers = new ArrayList<Integer>();
		numbers.add(new Integer(20));
		numbers.add(40); 		// 오토 박싱 --> numbers.add(new Integer(40))
		
		for (Integer x : numbers) {
			System.out.println(x);
		}
		
		for (int x : numbers) {
			System.out.println(x);
		}
		
		ArrayList<Double> scores = new ArrayList<Double>();
	}
}
package demo.list;

import java.util.ArrayList;

public class ArrayListDemo3 {

	public static void main(String[] args) {
		
		// 객체를 저장하는 ArrayList
		ArrayList<Book> books = new ArrayList<Book>();
		
		Book book1 = new Book(100, "이것이 자바다", "신용권", "한빛미디어", 30000);
		Book book2 = new Book(200, "데이터베이스 개론", "홍길동", "한빛미디어", 29000);
		Book book3 = new Book(300, "자바 디자인 패턴", "켄트벡", "인사이트", 32000);
		Book book4 = new Book(400, "인사이트 자바스크립트", "김유신", "한빛미디어", 27000);
		
		books.add(book1);
		books.add(book2);
		books.add(book3);
		books.add(book4);
		books.add(new Book(500, "스프링 3.0", "이일민", "에이콘출판사", 75000));
		
		System.out.println(books);

		int total = 0;
		for (Book book : books) {
			System.out.println("제목 : " + book.getTitle());
			System.out.println("가격 : " + book.getPrice());
		
			total += book.getPrice();
		}
		System.out.println("전체 가격 : " + total);
	}
}
package demo.list;

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListDemo4 {

	public static void main(String[] args) {
		
		ArrayList<String> names = new ArrayList<String>();
		names.add("홍길동");
		names.add("김유신");
		names.add("강감찬");
		names.add("이순신");
		names.add("이성계");
		names.add("김구");
		names.add("김좌진");
		
		for (String name : names) {
			System.out.println(name);			
		}
        
		System.out.println();
		
		for (int i=0; i<names.size(); i++) {
			String name = names.get(i);
			System.out.println(name);
		}
		System.out.println();
		
		/*
		 * Iterator<E>
		 *        콜렉션(List 및 Set)의 요소를 추출, 삭제할 때 사용되는 객체다.
		 *        주요 메소드
		 *        boolean hasNext()     - 추출할 객체가 남아있으면 true를 반환한다.
		 *        E          next()     - 객체를 하나 꺼내서 반환한다.
		 *        void     remove()     - 객체를 삭제한다.
		 */
		
		Iterator<String> it = names.iterator();
		while (it.hasNext()) {
			String name = it.next();
			System.out.println(name);
			if (name.startsWith("김")) {
				it.remove();
			}
		}
		System.out.println(names);
	}
}
package demo.list;

public class Book {
		
	private int no;
	private String title;
	private String author;
	private String publisher;
	private int price;

	public Book() {}

	public Book(int no, String title, String author, String publisher, int price) {
		super();
		this.no = no;
		this.title = title;
		this.author = author;
		this.publisher = publisher;
		this.price = price;
	}

	public int getNo() {
		return no;
	}

	public void setNo(int no) {
		this.no = no;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return "Book [no=" + no + ", title=" + title + ", author=" + author + ", publisher=" + publisher + ", price="
				+ price + "]";
	}		
}

 

'자바 > collection' 카테고리의 다른 글

properties, queue, stack  (0) 2019.06.10
sort  (0) 2019.06.10
set  (0) 2019.06.10
map  (0) 2019.06.10

+ Recent posts