3.24作业

该博客展示了如何使用Java进行Map的遍历,包括遍历key、value和entry。同时,还演示了List的元素添加、打乱顺序、查找最大值和最小值、排序以及交换元素的方法。此外,还探讨了字符串重复拼接导致的内存溢出问题。

1.

public static void main(String[] args) {
    Map<String, String> map = new HashMap<>();
    map.put("aaa", "111");
    map.put("bbb", "111");
    map.put("ccc", "111");
    map.put("ddd", "222");
    System.out.println(map);
    //通过遍历所有key 遍历Map
    Set<String> keys = map.keySet();
    Iterator<String> itkey = keys.iterator();
    while (itkey.hasNext()){
        String key = itkey.next();
        String value = map.get(key);
        System.out.println(key + ": " + value);
    }
    //通过遍历所有的value 遍历Map
    Collection<String> values = map.values();
    Iterator<String> itvalue = values.iterator();
    while (itvalue.hasNext()){
        String value = itvalue.next();
        System.out.println(value);
    }
    //通过遍历所有的entry 遍历map
    Set<Map.Entry<String , String>> entries = map.entrySet();
    Iterator<Map.Entry<String , String>> itentry = entries.iterator();
    while (itentry.hasNext()){
        Map.Entry<String , String> entry = itentry.next();
        String key = entry.getKey();
        String value = entry.getValue();
        System.out.println(key + ": " + value);
    }
}

2.

public static void main(String[] args) {
    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(3);
    list.add(4);
    list.add(5);
    list.add(6);
    list.add(7);
    list.add(8);
    list.add(9);
    System.out.println(list);
    Collections.shuffle(list); //打乱顺序
    System.out.println(list);
    System.out.println(Collections.max(list));
    System.out.println(Collections.min(list));
    Collections.sort(list);
    System.out.println(list);
    Collections.swap(list, 0 , 8);
    System.out.println(list);

    Set set = new HashSet();
    set.addAll(list);
    System.out.println(set);


}

3.

 public static void main(String[] args) {
//        int a = 10;
//        int b = 0;
//        int c = a/b;
//        System.out.println(c);
//        int[] d = {0,1,2,3,4,5,6,7,8,9};
//        System.out.println(d[10]);
//        String str = null;
//        if (str.equals(1)){
//            System.out.println(str);
//        }
//        Object o = new String("str");
//        System.out.println((Integer) (o));
//        String numstr = "345.00";
//        int num = Integer.valueOf(numstr);
//        System.out.println(numstr);
        String str1 = "a";
        for (int i = 0; i < 1000000000; i++) {
            str1 = str1 + str1;//OutOfMemoryError 内存溢出严重
        }
        System.out.println(str1);

    }

4.

public static void main(String[] args) {
    String st = "If you want to change your fate I think you must come to the school to learn java";
    String[] str = st.split(" ");//将字符串以空格分隔,分别装入String数组
    HashMap<String , Integer> map = new HashMap<>();
    for (String string : str){//遍历数组,每次将元素装入map,相同则在原有数目加一,否则设置初值一
        map.put(string , map.containsKey(string) ? map.get(string) + 1 : 1);
    }
    //遍历HashMap
    for (Map.Entry<String , Integer> entry : map.entrySet()){
        System.out.println(entry.getKey() + "=" + entry.getValue());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值