[精讚] [會員登入]
371

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

你可能感興趣的文章

Thread java thread

Arrays.asList 後想再add出現 UnsupportedOperationException 這個問題真是搞死我了 List<String> fruits = Arrays.asList("a

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

java.time 時間 instant java.time

Spring Boot Maven wrappers maven, mvnw, build command

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

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

java-身份証字號驗証 FormatCheck.java public class FormatCheck { private volatile

2018 hoc 掃地機器人 掃地機器人只能打掃沒有障礙物(桌椅、牆壁)的範圍,請寫程式控制機器人打掃餐廳的所有走道, 並在清掃完畢後回到充電器。

讀取特定資料夾下的xls檔 讀取特定資料夾下的xls檔

ArrayList 與 HashMap 範例 public static void main(String[] args) { String titleIds = &

word題目轉google測驗 word題目轉google測驗