zoukankan      html  css  js  c++  java
  • hashmap笔记

    hashmap用法笔记

    View Code
     1 package com.app;
    2
    3 import java.util.HashMap;
    4 import java.util.Iterator;
    5
    6 public class app {
    7
    8 /**
    9 * @param args
    10 */
    11 public static void main(String[] args) {
    12 // TODO Auto-generated method stub
    13 HashMap<String, Double> map = new HashMap<String, Double> ();
    14
    15 map.put("hello", 1.0);
    16 map.put("fine", 2.0);
    17 map.put("suck", 3.0);
    18
    19 Iterator<String> it = map.keySet().iterator();
    20
    21 if(map.containsKey("why")){
    22 System.out.println("Key why exist");
    23 }
    24
    25 if(map.containsValue(2.0)){
    26 System.out.println("Value 2 exist");
    27 }
    28
    29 /***Hash的遍历***/
    30 if(!map.isEmpty()){
    31 while(it.hasNext()){
    32 String tempKey = it.next();
    33 if(tempKey.equals("fine")){
    34 Double tempValue = map.get(tempKey) + 10;
    35 map.put(tempKey, tempValue);
    36
    37 System.out.println("fine is update to " + tempValue);
    38 }else{
    39 System.out.println(map.get(tempKey));
    40 }
    41 }
    42 }else{
    43 System.out.println("map is empty");
    44 }
    45
    46 map.clear();
    47
    48 if(map.isEmpty()){
    49 System.out.println("map is empty");
    50 }
    51 }
    52 }

    hashmap的排序:

    View Code
     1 package com.app;
    2
    3 import java.util.ArrayList;
    4 import java.util.Collections;
    5 import java.util.Comparator;
    6 import java.util.HashMap;
    7 import java.util.List;
    8 import java.util.Map;
    9
    10 public class app {
    11
    12 /**
    13 * @param args
    14 */
    15 public static void main(String[] args) {
    16 // TODO Auto-generated method stub
    17 Map<String, Integer> map = new HashMap<String, Integer>();
    18 map.put("hello", 1);
    19 map.put("fine", 3);
    20 map.put("abandon", 2);
    21
    22 List<Map.Entry<String, Integer>> list =
    23 new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
    24
    25 for(int i = 0; i < list.size(); i++){
    26 String id = list.get(i).getKey();
    27 System.out.println(id);
    28 }
    29
    30 Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
    31 public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2){
    32 return (o1.getKey()).toString().compareTo(o2.getKey());
    33 }//String类的compareTo方法实现字典排序
    34 });
    35
    36 for(int i = 0; i < list.size(); i++){
    37 String id = list.get(i).getKey();
    38 System.out.println(id);
    39 }
    40 }
    41 }
  • 相关阅读:
    oracle保留小数,例如0.00
    线程池读取List<T>实例
    Linq XML
    C# 反射 表达式树 模糊搜索
    MVC权限模块
    .NET-提取字符串实践总结
    2014,码农梦想,先从态度开始!
    Biztalk开发系列之二. (AS2配置)
    测试2
    Biztalk Http 适配器使用.开发系列(一)
  • 原文地址:https://www.cnblogs.com/fredric/p/2358474.html
Copyright © 2011-2022 走看看