728x90

 

 

 

 

 

package controller;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;

import model.vo.pModelVo02;


public class pController02 {

	public void firstR() {
		
		// HashSet
		HashSet<pModelVo02> hset =new HashSet<>();
		
		hset.add(new pModelVo02("힘내",1));
		hset.add(new pModelVo02("포기 ㄴ",2));
		System.out.println(hset);
		
		pModelVo02 pmv = new pModelVo02("a",0);
		hset.add(pmv);
		hset.add(new pModelVo02("추가",3));
		System.out.println(hset);
		
		HashSet<String> strSet = new HashSet<String>();
		strSet.add("안녕");
		strSet.add(new String("안녕"));
		System.out.println(hset);
		System.out.println(strSet);
		strSet.remove(new String("안녕"));
		System.out.println(strSet);
	
	}
	public void second() {	

		// LinkedHashSet
		// 순서 유지
		LinkedHashSet<pModelVo02> lhSet = new LinkedHashSet();
		
		lhSet.add(new pModelVo02("a",1));
		lhSet.add(new pModelVo02("b",2));
		lhSet.add(new pModelVo02("c",3));
		System.out.println(lhSet);
		
		// TreeSet
		// 정렬 가능
		// 정렬 기능 + 기준 필요
		// 기준이 되는 오버라이딩은 String class 제네릭으로 대체
		TreeSet<String> tSet = new TreeSet<>();
//		tSet.add(new String("A",1));
//		tSet.add(new String("B",2));
		System.out.println(tSet);
		
		tSet.add("A");
		tSet.add("C");
		tSet.add("B"); 			  // 추가 순서는 ACB
		System.out.println(tSet); // 결과는        ABC (정렬 적용 확인)
		
		
		
	}
	
}

 

package model.vo;

public class pModelVo02 {

	private String name;
	private double weight;
	
	public pModelVo02() {}
	public pModelVo02(String name, double weight) {
		this.name = name;
		this.weight = weight;
	}


	// getter & setter
	public void setName(String name) {
		this.name = name;
	}
	public void setWeight(double weight) {
		this.weight = weight;
	}
	public String getName() {
		return name;
	}
	public double getWeight() {
		return weight;
	}
	
	//toString
	@Override
	public String toString() {
		return name+"("+weight+"kg)";
	}

}

 

package run;
import controller.pController02;

public class pRun02 {
	public static void main(String[] args) {
		
		pController02 p = new pController02();
//		p.firstR();
		p.second();

	}
	
}

 

728x90
반응형

+ Recent posts