Java学习第十五章 之 Map、可变参数、Collections

摘要:
=目标。getClass())194returnfalse;195学生=(学生)obj;196if(age!=other.age)197returnfalse;198if(name==null){199if(other.name!=null)200returnfalse;201}elseif(!
  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 */

免责声明:文章转载自《Java学习第十五章 之 Map、可变参数、Collections》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇angluar 表单的验证 动态数据项表单验证ASP.NET WEBAPI 的身份验证和授权下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Innosetup 脚本写注册表实现自定义协议(Url Protocol)

[Registry] Root: HKCR; SubKey: NGIE; ValueData: "NGIE"; ValueType: string; Flags: CreateValueIfDoesntExist UninsDeleteKey; Root: HKCR; SubKey: NGIE; ValueName: "URL Protocol";Val...

各种数据库与.NET Framework类型对照

本文记录各种数据库与.NET类型的对照,包括Oracle,SQL Server,MySQL,SQLite 首先是Oracle的 序号 Oracle数据类型 .NET类型 1 BFILE byte[] 2 BLOB byte[] 3 CHAR string 4 CLOB string 5 DATE DateTime 6 FLO...

Java 设计模式六原则及23中常用设计模式

一、设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。 结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。 行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访...

xStream完美转换XML、JSON(转)

xStream框架 xStream可以轻易的将Java对象和xml文档相互转换,而且可以修改某个特定的属性和节点名称,而且也支持json的转换; 前面有介绍过json-lib这个框架,在线博文:http://www.cnblogs.com/hoojo/archive/2011/04/21/2023805.html 以及Jackson这个框架,在线博文:ht...

CORS跨域实现思路及相关解决方案

本篇包括以下内容: CORS 定义 CORS 对比 JSONP CORS,BROWSER支持情况 主要用途 Ajax请求跨域资源的异常 CORS 实现思路 安全说明 CORS 几种解决方案 自定义CORSFilter Nginx 配置支持Ajax跨域 支持多域名配置的CORS Filter keyword:cors,跨域,ajax,403,fi...

golang查找端口号占用的进程号

golang官方包: https://studygolang.com/pkgdoc os 支持获取当前进程pid并kill、但是仅仅限于获取当前进程pid FindProcess().Kill() os.Getpid() 基础用法;但是却没有提供依据端口号获取对应的pid,所以还是执行shell指令对结果集进行过滤获取pid // 获取8299端口对应进程...