840

ArrayList 與 HashMap 範例

“
public static void main(String[] args) { String titleIds = &
„

分享此文連結 //n.sfs.tw/13298

分享連結 ArrayList 與 HashMap 範例@igogo
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-22 06:47:46 最後編修
2019-05-16 10:56:47 By igogo
 

 

 

職稱是根據一組字串由逗號分隔帶回數字代碼例如 “40,80,60"  

數字代表的是不同的職稱, 考量將來可能又有新的職稱, 所以另外寫一個對照

 

分別用arraylist 及hashmap 來加入職稱範例

public static void main(String[] args) {

 String titleIds = "40,80,100";
        List<String> titles = new ArrayList<>();

        Arrays.asList(titleIds.split(",")).forEach(id->{
            titles.add(getTitle(Integer.valueOf(id)));
        });

        titles.forEach(title -> System.out.printf(title));


        HashMap<Integer,String> hashMap = new HashMap<>();
        Arrays.asList(titleIds.split(",")).forEach(id->hashMap.put(Integer.valueOf(id),getTitle(Integer.valueOf(id))));

        hashMap.forEach((k,v)-> System.out.println(k+":"+v));
        //直接由hashMap的key 取得值
        System.out.println(hashMap.get(40));
        
}

    public String getTitle(Integer id) {
        String title = "";

        if (id == 40 ) {
            title = "資訊組長";
        }
        if (id == 50) {
            title = "校長";
        }
        if (id == 100) {
            title = "工友";
        }
        return title;

    }

 

END

你可能感興趣的文章

java.time 時間 instant java.time

ArrayList 想移除特定值 想移出water, 使用lambda 的方式如下 List<String> fruits = new Arr

[vue.js] 設定 content type 今天在wickt 端怎麼就是收不到vue.js 以post 傳過來的資料 找了好久才發現 application/jso

刪除資料夾內的檔案 lambda 一行文 Arrays.stream(new File("/folder/path").

spring boot jpa 使用多個欄位排序 我有一entity 叫 team 資料欄下如下 @Id @GeneratedValue(strategy = Gener

使用poi 解析 docx 原先是想解析出在docx 中的文字跟圖片, 但是, 有些我們認為是圖片, 其實是用方程式表示, 實在是太麻煩了 就記錄一

隨機好文

vue.js components 多個組件的呈現 vue.js 組件 component

[scratch2] 巢狀迴圈 有兩清單 一數字 一英文 想排出所以可能, 例如1a,1b,1c,2a,2b,2c...3c 利用巢狀迴圈 內圈累加的變

將google試算表當作簡易資料庫,利用Google apps cript 在網頁上操作查詢 將google試算表當作簡易資料庫,利用apps cript 在網頁上操作查詢 若我有一試算表資料 縣市 status

[web]校園食材登錄平臺午餐食材嵌入頁面語法 校園食材登錄平臺午餐食材嵌入頁面語法

[web] 自訂模組-打字機與跑馬燈效果 自訂模組-打字機效果

精讚::