日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java学习(129):hashmap的方法

發布時間:2023/12/10 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java学习(129):hashmap的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.util.Date; public class Car {private String brand;//品牌private Date createDate;private int housepower;//馬力private int speed;//速度public void setBrand(String brand) {this.brand = brand;}public String getBrand() {return brand;}public Date getCreateDate() {return createDate;}public void setCreateDate(Date createDate) {this.createDate = createDate;}public void setHousepower(int housepower) {this.housepower = housepower;}public int getHousepower() {return housepower;}public void setSpeed(int speed) {this.speed = speed;}public int getSpeed() {return speed;} }

測試類

import java.util.*;public class test68 {public static void main(String[] args) {Car c0 = new Car();c0.setBrand("寶馬");c0.setCreateDate(new Date());c0.setHousepower(4);c0.setSpeed(200);Car c1 = new Car();c1.setBrand("創奇");c1.setCreateDate(new Date());c1.setHousepower(5);c1.setSpeed(220);Car c2 = new Car();c2.setBrand("發力");c2.setCreateDate(new Date());c2.setHousepower(4);c2.setSpeed(240);Car c3 = new Car();c3.setBrand("豐田");c3.setCreateDate(new Date());c3.setHousepower(4);c3.setSpeed(230);//Car c4 = c3;// c4.setBrand("大眾");//c4.setCreateDate(new Date());//c4.setHousepower(4);//c4.setSpeed(230);//添加hashmap的對象Map carMap = new HashMap();//使用put添加對象carMap.put("k1", c0);carMap.put("k2", c1);carMap.put("k3", c2);carMap.put("k4", c3);//carMap.put("k4",c4);//使用get返回自己需要的值Object obj = carMap.get("k4");if (obj != null) {Car target = (Car) obj;System.out.println(target.getBrand() + target.getHousepower());} else {System.out.println("未找到");}/*獲取map集合的所有值*/Set keySet = carMap.keySet();Car c = null;for (Object k : keySet) {Car temp = (Car) carMap.get(k);System.out.println(temp.getBrand() + temp.getHousepower() + temp.getSpeed());}/*獲取所有值對象*/Collection coll = carMap.values();for (Object v : coll) {System.out.println(((Car) v).getBrand());}/*判斷是否包含某個給定的key*/boolean boo = carMap.containsKey("k9");System.out.println("是否包含k9" + boo);System.out.println(carMap.containsValue(c3));/*移除鍵值對象*/if (carMap.containsKey("k3")) {Object removeobj = carMap.remove("k3");System.out.println(((Car) removeobj).getBrand());}System.out.println(carMap.size());} }

運行結果

總結

以上是生活随笔為你收集整理的java学习(129):hashmap的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。