Zdrojový kód k 11. prednáške

package sk.upjs.paz.prednaska11;

import java.util.*;
import java.util.Map.Entry;

import sk.upjs.jpaz2.*;

public class Launcher {

        public static void main(String[] args) {
                //------------------------------------
                // Zoznam Stringov a ukazka pridavania + vypis
                // typ premennej je interface list
                List<String> zoznam = new ArrayList<String>();
                zoznam.add("dobry");
                zoznam.add("den");
                System.out.println(zoznam);

                for (int i = 0; i < zoznam.size(); i++) {
                        System.out.println(zoznam.get(i));
                }

                //-----------------------------------
                // zoznam cisel Double je wrappovacia trieda pre double
                List<Double> cisla = new ArrayList<Double>();
                for (int i = 0; i < 10; i++) {
                        // Double cislo = Math.random();
                        // cisla.add(cislo);
                        cisla.add(Math.random());
                }
                // Double je referencny typ premennej - moze byt null
                cisla.set(2, null);
                System.out.println(cisla);

                //------------------------------------
                // volanie metody z list handleru na vypocet suctu
                List<Integer> postupka = new ArrayList<>();
                for (int i = 0; i < 6; i++) {
                        postupka.add(i);
                }
                postupka.add(null);
                System.out.println(postupka);
                ListHandler handler = new ListHandler();
                int result = handler.sum(postupka);
                System.out.println(result);

                //------------------------------------
                // mnozina - nemozu byt duplikovane prvky a poradie nie je urcene
                // String a Integer implementuju equals a hashcode - teda set funguje
                Set<String> mnozina = new HashSet<>();
                mnozina.add("dobry");
                mnozina.add("ABC");
                mnozina.add("den");
                mnozina.add("ABC");
                System.out.println(mnozina);

                Set<Integer> mnozinaCisel = new HashSet<>();
                mnozinaCisel.add(new Integer(2021));
                mnozinaCisel.add(new Integer(2021));
                System.out.println(mnozinaCisel.size());

                //------------------------------------
                // mnozina bodov - bez equals a hashcode to nefunguje. Bez hashcode je problem pri HashSet-e
                Set<Bod> body = new HashSet<>();
                body.add(new Bod(100, 100));
                body.add(new Bod(100, 100));
                body.add(new Bod(70, -10));
                body.add(new Bod(10, -10));
                body.add(new Bod(70, -100));
                System.out.println(body);
                Bod a = new Bod(100, 100);
                Bod b = new Bod(100, 100);
                // chcem false
                System.out.println(a == b);
                // chcem true
                System.out.println(a.equals(b));
                System.out.println(a.equals("hello"));

                //------------------------------------
                // iterovanie mnozinou cez for-each
                for (Bod bod : body) {
                        System.out.println(bod);
                        // taketo vymazavanie by vyhodilo vynimku
                        // if (bod.getY() < 0) body.remove(bod);
                }
                // iterovanie mnozinou cez iterator - vygenerovane s for cyklom
                // for (int i = 0; i < 10; i++)
                for (Iterator iterator = body.iterator(); iterator.hasNext();) {
                        Bod bod = (Bod) iterator.next();
                }
                // iterator pomocou while
                Iterator<Bod> iterator = body.iterator();
                while (iterator.hasNext()) {
                        Bod next = iterator.next();
                        System.out.println(next);
                        // vymazavanie umoznene pomocout iteratora
                        // if (next.getY() < 0) iterator.remove();
                }

                //------------------------------------
                // for each cyklus funguje aj pre pole
                int[] arr = { 1, 2, 3, 4, 5 };
                int res = 0;
                for (int num : arr) {
                        res += num;
                }

                //------------------------------------
                // kolekcie - konstruktory - arraylist vyrobeny podla inej kolekcie. Teda zo setu mame list.
                List<Bod> bodyVZozname = new ArrayList<>(body);
                System.out.println(bodyVZozname);

                //------------------------------------
                // mapa
                Map<String, Integer> tabulka = new HashMap<>();
                tabulka.put("java", 8);
                tabulka.put("abc", 70);
                tabulka.put("java", 9);
                System.out.println(tabulka);
                System.out.println(tabulka.get("java"));
                tabulka.containsKey("java");
                Set<Entry<String, Integer>> entrySet = tabulka.entrySet();

                //------------------------------------
                // naznacene riesenie ulohy kde uchovavam pre kluc String - nejake slovo hodnotu list - zoznam stran kde sa slovo vyskytuje
                Map<String, List<Integer>> register = new HashMap<>();
                // ... prehladavam knihu a mam slovo a stranu
                String slovo = "java";
                int strana = 1050;
                if (!register.containsKey(slovo)) {
                        // take slovo tam nemam, vyrobim prazdny zoznam
                        register.put(slovo, new ArrayList<>());
                }
                // pridam vyskyt slova do listu
                List<Integer> z = register.get(slovo);
                z.add(strana);

                //------------------------------------
                // algoritmy v triede Collections
                System.out.println(bodyVZozname);
                Collections.shuffle(bodyVZozname);
                System.out.println(bodyVZozname);
                Collections.reverse(bodyVZozname);
                System.out.println(bodyVZozname);
        }
}
package sk.upjs.paz.prednaska11;

import java.util.*;

public class ListHandler {

        /**
         * Spocita sucet cisel v liste, null vynechava.
         * Parameter je Interface List - lebo metoda by vyzerala rovnako pre Arraylist, LinkedList a ine.
         */

        public int sum(List<Integer> list) {
                int result = 0;
                for (int i = 0; i < list.size(); i++) {
                        Integer number = list.get(i);
                        if (number != null) {
                                // na tomto mieste sa implicitne vola .intValue()
                                result += number;
                        }
                }
                return result;
        }

        // ta ista metoda ale s pouzitim ForEach cyklu
        public int sumForEach(List<Integer> list) {
                int result = 0;
                // FOR-EACH cyklus
                for (Integer number : list) {
                        if (number != null) {
                                result += number;
                        }
                }
                return result;
        }
}
 
package sk.upjs.paz.prednaska11;

// Trieda bod - 2 premenne, zvysok kodu je vygenerovany

public class Bod {

        private int x;
        private int y;

        public Bod(int x, int y) {
                super();
                this.x = x;
                this.y = y;
        }

        public int getX() {
                return x;
        }

        public int getY() {
                return y;
        }

        @Override
        public String toString() {
                return "Bod [x=" + x + ", y=" + y + "]";
        }

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

        // Bod this .... Object obj
        @Override
        public boolean equals(Object obj) {
                // ak ten isty objekt
                if (this == obj)
                        return true;
                // mam vobec ten druhy object?
                if (obj == null)
                        // this a null
                        return false;
                // su rozne triedy
                if (getClass() != obj.getClass())
                        // ak porovnavam napr bod a string, jablka s hruskami
                        return false;

                // viem ze porovnavam dva body ... this a Object obj
                Bod other = (Bod) obj;
                // this vs. other .. obe su Bod
                if (x != other.x)
                        return false;
                if (y != other.y)
                        return false;
                return true;
        }



}