[精讚] [會員登入]
387

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

你可能感興趣的文章

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

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

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

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

停止多執行緒 利用主程式呼叫多執行緒時, 要怎麼停止正在執行的多執行緒

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

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

download a file from spring boot controllers ownload a file from spring boot controllers

臺中市雲端校務系統與Windows AD帳號整合(2) 安裝Active Directory網域服務

臺中市雲端校務系統與Windows AD帳號整合(5) 安裝java環境

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

利用google apps script 將Google sheet 中兩個工作表中的資料對應 如何整理google測驗收回來的學生資料