[精讚] [會員登入]
654

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

你可能感興趣的文章

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

Spring Boot Maven wrappers maven, mvnw, build command

Thread java thread

spring boot 使用jdbc連接mariadb spring boot, mariadb, jdbc, rowmapper

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

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

隨機好文

vue.js component 在parent與child 傳值 component 在parent與child 傳值

臺中市校務雲端系統與Windows AD帳號整合(1) active directory,ldaps,雲端校務系統

臺中市雲端校務系統與Windows AD帳號整合(6) 修改Windows AD 密碼原則

scratch3 計算得分排名 scratch3 得分排名

題庫批次匯入google表單 請先建一新試算表, 將題目轉成格式如下 並將網址列記下來, 後續的題目就是從此試算表讀出 題目 答案 選項一 選項二 選