1 /*
2
3 Map:存储键值对
4
5 键不能重复存储,值可以重复
6
7 每一个键对应一个值
8
9 方法:
10
11 v put(K key , V value)将指定的键和值添加到集合中
12
13 v get(Object obj) 获取指定的键对应的值
14
15 v remove(Object key)移除指定的键所对应的值
16
17 */
18
19 public class MapDemo{
20
21 public static void main(String[] args){
22
23 //创建map对象
24
25 Map<String , String> map = new HashMap<String , String>();
26
27 //给Map中添加元素,会返回key所对应的value值,若key没有对应的值,则返回null
28
29 map.put("张三" , "上海");
30
31 map.put("李四" , "北京");
32
33 System.out.println(map);
34
35 //根据key获取value值
36
37 String s = map.get("张三");
38
39 System.out.println(s);//上海
40 //移除key,返回key对应的value值
41
42 String st = map.remove("张三");
43
44 System.out.println(st);//上海
45
46 System.out.println(map);//{李四,北京}
47
48 }
49
50 }
51
52 /*
53
54 Map集合键找值的方式 : 通过元素的键,获取键所对应的值
55
56 Set<K> keySet() 遍历键的Set集合,得到每一个键,根据键,获取键对应的值
57
58 */
59
60 public class MapDemo{
61
62 public static void main(String[] args){
63
64 //创建Map对象
65
66 Map<String , Integer> map = new HashMap<String , Integer>();
67
68 map.put("张三" , 20);
69
70 map.put("李四" , 20);
71
72 //获取Map中的key
73
74 Set<String> keySet = map.keySet();
75
76 //遍历存放所有key的Set集合
77
78 Iterator<String> it = keySet.iterator();
79
80 while(it.hasNext()){
81
82 //获取key
83
84 String key = it.next();
85
86 //通过key找到对应的值
87
88 int value = map.get(key);
89
90 System.out.println(key+" "+ value);
91
92 }
93
94 }
95
96 }
97
98
99
100 /*
101
102 Entry嵌套接口 : static interface(Map.Entry(K , V))
103
104 K getKey() 返回与此项对应的键
105
106 V getValue() 返回与此项对应的值
107
108 Set<Map.Entry<K , V>> entrySet() 用于返回Map集合中所有的键值对对象,以Set集合形式返回
109
110 */
111
112 public class MapDemo{
113
114 public static void main(String[] args){
115
116 //创建Map对象
117
118 Map<String , Integer> map = new HashMap<String , Integer>();
119
120 map.put("赵云" , 20);
121
122 map.put("吕小布" , 20);
123
124 //遍历Map中的所有的 key 和 value 的对应关系
125
126 Set<Map.Entry<String , Integer>> entrySet = map.entrySet();
127
128 //遍历Set集合
129
130 for(Map.Entry<String , Integer> entry : entrySet){
131
132 //通过每一对对应关系获取对应的key
133
134 String key = entry.getKey();
135
136 //通过每一对对应关系获取对应的key
137 int value = entry.getValue();
138
139 System.out.println(key +" "+ value);
140
141 }
142
143 }
144
145 }
146
147 /*
148
149 HashMap存储自定义类型
150
151 */
152
153 public class Student {
154
155 private String name;
156 private int age;
157
158 public String getName() {
159 return name;
160 }
161 public void setName(String name) {
162 this.name = name;
163 }
164 public int getAge() {
165 return age;
166 }
167 public void setAge(int age) {
168 this.age = age;
169 }
170 public Student() {
171 super();
172
173 }
174 public Student(String name, int age) {
175 super();
176 this.name = name;
177 this.age = age;
178 }
179 @Override
180 public int hashCode() {
181 final int prime = 31;
182 int result = 1;
183 result = prime * result + age;
184 result = prime * result + ((name == null) ? 0 : name.hashCode());
185 return result;
186 }
187 @Override
188 public boolean equals(Object obj) {
189 if (this == obj)
190 return true;
191 if (obj == null)
192 return false;
193 if (getClass() != obj.getClass())
194 return false;
195 Student other = (Student) obj;
196 if (age != other.age)
197 return false;
198 if (name == null) {
199 if (other.name != null)
200 return false;
201 } else if (!name.equals(other.name))
202 return false;
203 return true;
204 }
205 @Override
206 public String toString() {
207 return "Student [name=" + name + ", age=" + age + "]";
208 }
209
210 public static void main(String[] args) {
211
212 //创建Map对象
213 Map<Student, String> map = new HashMap<Student, String>();
214 map.put(new Student("张三" , 20), "北京");
215 map.put(new Student("李四" , 20), "上海");
216 map.put(new Student("王五" , 20), "广州");
217
218
219 //遍历存放所有key的Set集合
220 Set<Student> keySet = map.keySet();
221
222
223 Iterator<Student> it = keySet.iterator();
224 while(it.hasNext()) {
225
226 //获取key
227 Student key= it.next();
228
229 //通过key找到对应的值
230
231 String value = map.get(key);
232 System.out.println(key +".."+ value);
233 }
234
235 /*
236
237 静态导入:
238
239 在导包的过程中我们可以直接导入静态部分,这样某个类的静态成员就可以直接使用了。在源码中经常会出现静态导入
240
241 格式;import static xxx.yyy;导入后yyy可直接使用
242
243 Set<Map.Entry<String , String>> entrySet = map.entrySet();
244
245 Set<Entry<String , String>> entrySet = map.entrySet();
246
247 */
248
249 /*
250
251 可变参数:如果定义一个方法需要接受多个参数,并且多个参数类型一致,可以简化成如下格式:
252
253 修饰符 返回值类型 方法名(参数类型...形参名);直接传递数据
254
255 等价与
256
257 修饰符 返回值类型 方法名(参数类型[] 形参名);方法调用时必须传递数组
258
259 如果在方法书写时,这个方法拥有多参数,参数中包含可变参数,可变参数一定要写在参数列表的末尾位置
260
261 */
262
263 public class Demo{
264
265 public static void main(String[] args){
266
267 int sum = add(1,2,3);
268
269 System.out.println(sum);
270
271 }
272
273 public static int add(int...arr){
274
275 int sum = 0;
276 for (int i = 0; i < arr.length; i++) {
277 sum += arr[i];
278 }
279
280 return sum;
281
282 }
283
284 }
285
286
287
288 /*
289
290 Collections集合工具类:
291
292 static void shuffle(List<?> list)集合元素存储位置打乱
293
294 static <T extends Comparable<? super T>> void sort(List<?> list) 集合元素排序
295
296 */
297
298 /*
299
300 集合嵌套: Collection集合嵌套、Map集合嵌套、Collection和Map集合嵌套
301 ArrayList< ArrayList<String>> Collection< ArrayList<String>>
302
303 HashMap<String , ArrayList<Person>> ArrayList< Map<String , String>>
304
305 HashMap<String , HashMap<String , String>> HashMap<String , HashMap<Person , Student>>
306
307 */