/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package std; import java.io.Serializable; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; /** * * @author (C)Y.D.Zakovryashin, 05.12.2022 */ public class DemoCollection { public class User implements Serializable { private String login; private String password; public User(String login, String password) { this.login = login; this.password = password; } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "login=" + login + ", password=" + password + '}'; } } public class Info implements Serializable { String info; public Info(String info) { this.info = info; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } @Override public String toString() { return "Info{" + "info=" + info + '}'; } } public static void main(String[] args) { DemoCollection demo = new DemoCollection(); // demo.print(demo.init(5)); Map m = demo.init(); demo.printMap(m); } private Set init(int size) { int[] ai = {123, 345, 4564, 345, 123}; HashSet hs = new HashSet<>(); for (int i : ai) { System.out.println("Add " + i + " is " + hs.add(new Integer(i))); } return hs; } private Map init() { HashMap hm = new HashMap<>(); hm.put(new User("first", "12344"), new Info("first message")); hm.put(new User("second", "283479"), new Info("secondt message")); return hm; } private void printIterator(Collection c) { if (c == null || c.isEmpty()) { System.out.println("Collection is null or empty"); return; } Iterator i = c.iterator(); while (i.hasNext()) { T t = i.next(); System.out.println(t); } } private void print(Collection c) { if (c == null || c.isEmpty()) { System.out.println("Collection is null or empty"); return; } for (T t : c) { System.out.println(t); } } private void printMap(Map m) { // 1. Map.Entry for (Map.Entry tm : m.entrySet()) { System.out.println( tm.getKey() + " --> " + tm.getValue()); } System.out.println("------------------------------"); // 2. keySet -> get(...) for (User u : m.keySet()) { Info i = m.get(u); System.out.println(u + " --> " + i); } } }