정보보안공부

Java2_day08 본문

Language/Java2

Java2_day08

Steady_sp 2017. 3. 21. 00:59

*** n01.tree - TreeMapEx1

 

 

-> 앞에서 했던 HashMap의 예제에 TreeMap메소드를 추가했다.

-> int first = map.firstKey();

-> int last = map.lastKey(); 를통해 첫번째 key값과 마지막 key값을 알수있다.

-> System.out.println("첫번째 키값 : " + first + " 마지막 키값 : " + last );

 

*** n02.comparable - ComparableEx

 

 

-> treeSet.add(new Person("Hongkd", 45));
    treeSet.add(new Person("kimbs", 25));
    treeSet.add(new Person("Parkjw", 31));

    //tree는 순서를 알아야한다. 작은값 왼쪽 큰값 오른쪽

 

-> Iterator<Person> iterator = treeSet.iterator();
    while(iterator.hasNext()) {
    Person person = iterator.next();
    System.out.println(person.name + ":" + person.age);
    }

    여기까진 Hashset과 똑같다

 

-> NavigableSet<Person> dset = treeSet.descendingSet();
    //descending : 내림차순정렬
    //Navigable : 내림차순이나 오름차순쓸때 사용
    dset.forEach(x -> System.out.println());

    TreeSet사용할때 선언한다. Person클래스를 사용했으므로

    Person클래스에 implements comparable<Person>선언후

    public int compareTo(Person o) 선언
    

*** n02.comparable - Person

 

 

-> public class Person implements comparable<Person> {

    Person 클래스에 implements comparable<Person> 선언

 

-> public Person(String name, int age) { this.name = name; this.age = age; }

 

-> public int compareTo(Person o) { if ( age<o.age) return -1;

    else if (age==o.age) return 0; else return 1; }

    compareTo메소드 선언

 

*** n03.comparator - ComparatorEx

 

 

-> //TreeSet<Fruit> treeSet = new TreeSet<Fruit>();

    fruit이 Comparable을 구현하지 않았기 때문에 예외발생

 

*** n03.comparator - Fruit

 

 

-> public class Fruit { public String name; public int price;

    public Fruit(String name, int price) { this.name = name; this.price = price; }

 

*** n03.comparator - DescendingFruit

 

 

-> public DescendingFruit implements Comparator<Fruit> {

    public int compare(Fruit o1, Fruit o2) {

    if(o1.price < o2.price) return 1; else if (o1.price == o2.price) return 0;

    else return -1;

 

*** n04.funtionalInterface - AFunctionalInterface

 

 

-> public interface AFuntionalInterface { public void method(); } //추상메소드 선언

 

*** n04.funtionalInterface - BFunctionalInterface

 

 

-> public interface BFunctionalInterface { public void method(int x); } //매개변수만

 

*** n04.funtionalInterface - CFunctionalInterface

 

 

-> public class CFunctionalInterface { public int method(int x, int y); }//매개변수2개

 

*** n04.funtionalInterface - OperatorExample

 

 

-> public static int maxOrMin(IntBinaryOperator operator) { 
    int result = scores[0];
    for(int score : scores) { result = operator.applyAsInt(result, score); }
    return result; }

 

-> public static void main(String[] args) { //최대값 얻기
    int max = maxOrMin( //함수 인터페이스인 경우 가능
    (a,b) -> { if(a>=b) return a;     else return b;    }   );
    System.out.println("최대값 : " + max);
  
    //최소값 얻기
    int min = maxOrMin(

    (a,b) -> { if(a<=b) return a;     else return b;    }   );
    System.out.println("최소값 : " + min);
    }//main end
    }

 

*** n04.funtionalInterface - SupplierEx

 

 

-

> public class SupplierEx {
    public static void main(String[] args) {
    IntSupplier intSupplier = () -> {
    int num = (int) (Math.random() * 6) + 1;
    return num; };
  
    int num = intSupplier.getAsInt();
    System.out.println("눈의 수 : " + num); } }

 

*** n04.funtionalInterface - ConsumerEx

 

 

-> Consumer<String> consumer = t -> System.out.println(t + "8");
    consumer.accept("java");
  
-> BiConsumer<String,String> bigConsumer = (t, u) -> System.out.println(t + u);
    bigConsumer.accept("java", "8");
  
-> DoubleConsumer doubleConsumer = d -> System.out.println("Java" + d);
    doubleConsumer.accept(8.0);
  
-> ObjIntConsumer<String> objIntConsumer = (t, i) -> System.out.println(t + i);
    objIntConsumer.accept("Java", 8);

 

-> 각형마다 람다식이 다르다

 

*** n04.funtionalInterface - FunctionalInterface

 

 

-> public class FunctionalInterfaceEx {
    public static void main(String[] args) {

-> AFuntionalInterface fi;
  
    fi = () -> {   String str = "method call1";   System.out.println(str);  };
    fi.method();
  
    fi = () -> {   System.out.println("method call2");  };
    fi.method();
  
    fi = () -> System.out.println("method call3");

    fi.method();
  

-> BFunctionalInterface bfi;
  
    bfi = (x) -> {   int result = x * 5;   System.out.println(result);  };
    bfi.method(2);
  
    bfi = (x) -> {   System.out.println(x*5);  };
    bfi.method(2);
  
    bfi = x -> System.out.println(x*5);   bfi.method(2);  
  

-> CFunctionalInterface cfi;
  
    cfi = (x,y) -> {   int result = x*y;   return result;  };
    System.out.println(cfi.method(2,5));
  
    cfi = (x,y) -> {   return x + y;  };
    System.out.println(cfi.method(2,5));
  
    cfi = (x,y) -> x + y;

    System.out.println(cfi.method(2,5));
  
    cfi = (x,y) -> sum(x,y);
    System.out.println(cfi.method(2,5));
    }

 

-> public static int sum(int x, int y) {  return (x + y); }

-> sum메소드 선언

'Language > Java2' 카테고리의 다른 글

Java2_day11  (0) 2017.03.24
Java2_day09  (0) 2017.03.22
Java2_day07  (0) 2017.03.18
Java2_day06  (0) 2017.03.17
Java2_day05  (0) 2017.03.16
Comments