You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
3.4 KiB
135 lines
3.4 KiB
/* |
|
* 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<User, Info> m = demo.init(); |
|
demo.printMap(m); |
|
} |
|
|
|
private Set<Integer> init(int size) { |
|
int[] ai = {123, 345, 4564, 345, 123}; |
|
HashSet<Integer> hs = new HashSet<>(); |
|
for (int i : ai) { |
|
System.out.println("Add " + i + " is " |
|
+ hs.add(new Integer(i))); |
|
} |
|
return hs; |
|
} |
|
|
|
private Map<User, Info> init() { |
|
HashMap<User, Info> 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 <T> void printIterator(Collection<T> c) { |
|
if (c == null || c.isEmpty()) { |
|
System.out.println("Collection is null or empty"); |
|
return; |
|
} |
|
Iterator<T> i = c.iterator(); |
|
while (i.hasNext()) { |
|
T t = i.next(); |
|
System.out.println(t); |
|
} |
|
} |
|
|
|
private <T> void print(Collection<T> 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<User, Info> m) { |
|
// 1. Map.Entry |
|
for (Map.Entry<User, Info> 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); |
|
} |
|
} |
|
}
|
|
|