C++容器与Java容器的区别

本文详细探讨了Java中TreeMap、HashMap、HashSet等集合类的特性与行为差异,特别是重复Key值处理方式上的区别,以及Java与C++在这一方面的对比。通过具体代码示例,分析了集合遍历、浅复制的机制,并揭示了String类型在列表复制中的特殊表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.
Java: TreeMap HashMap
C++ : map     unordered_map   //结论:与c++不一样 [经过验证]
// java 相同Key值时,使用新的Value
// c++  相同Key值时,使用旧的Value   private void testTreeMapOrder3() {
        TreeMap<Student, String> treeMap = new TreeMap<>();
        treeMap.put(new Student(25, "name1"), "test1");
        treeMap.put(new Student(26, "name3"), "same0");
        treeMap.put(new Student(26, "name3"), "same1");
        treeMap.put(new Student(27, "name4"), "test4");


        for (Iterator itr = treeMap.entrySet().iterator(); itr.hasNext(); ) {
            Map.Entry entry = (Map.Entry) itr.next();
            Student key = (Student) entry.getKey();
            String value = (String) entry.getValue();
            System.out.println(String.format("key:%s value:%s", key, value));

        }

//        是否在与自己比较:true     相同:age:25 name:name1    //原因:源码中,插入第一个元素时,compare(key, key);
//        是否在与自己比较:false    相同:age:26 name:name3
//        key:age:25   name:name1 value:test1
//        key:age:26   name:name3 value:same1
//        key:age:27   name:name4 value:test4


        //分析源代码
//        if (cmp < 0)
//            t = t.left;
//        else if (cmp > 0)
//            t = t.right;
//        else
//            return t.setValue(value); //更换为最新的值


        //结论:与c++不一样 [经过验证]
        // java 相同Key值时,使用新的Value
        // c++  相同Key值时,使用旧的Value

    }

2. TreeMap的遍历(HashMap遍历一样)

for (Iterator itr = treeMap.entrySet().iterator(); itr.hasNext(); ) {
    Map.Entry entry = (Map.Entry) itr.next();
    Student key = (Student) entry.getKey();
    String value = (String) entry.getValue();
    System.out.println(String.format("key:%s value:%s", key, value));
}

 

3.hashSet遍历 (在遍历时同时删除:一定要使用迭代器 itr.remove())

for (Iterator<Student> itr = hashSet.iterator(); itr.hasNext(); ) {
    Student student = itr.next();
    System.out.println("student:" + student);

    if (student.mAge == 2)
        itr.remove();
}

for (Student student : hashSet) {
    System.out.println("student:" + student);
}

4.new ArrayList<>(otherList) 采用浅复制

    private static void testClone4() {
        List<People> peopleList1 = new ArrayList<>();
        peopleList1.add(new People(1, "ye huang"));
        peopleList1.add(new People(2, "wang yi"));
        peopleList1.add(new People(3, "zhang yue"));
        System.out.println("peopleList1:" + peopleList1);

        List<People> peopleList2 = new ArrayList<>(peopleList1);

        for (People people : peopleList2) {
            people.age = 66;
            people.name = "maintop";
        }

        System.out.println("peopleList1:" + peopleList1);
        System.out.println("peopleList2:" + peopleList2);

//        对象个数:1
//        对象个数:2
//        对象个数:3
//        peopleList1:[age:1   name:ye huang, age:2   name:wang yi, age:3    name:zhang yue]
//        peopleList1:[age:66  name:maintop, age:66   name:maintop, age:66   name:maintop]
//        peopleList2:[age:66  name:maintop, age:66   name:maintop, age:66   name:maintop]
        //浅复制

    }

不过String的行为不一样

    private static void testClone() {
        List<String> list1 = new ArrayList<>();
        list1.add("A");
        list1.add("B");
        list1.add("C");

        List<String> list2 = new ArrayList<>(list1);
        list2.set(0, "I");
        System.out.println("list1:" + list1);
        System.out.println("list2:" + list2);

//        list1:[A, B, C]
//        list2:[I, B, C]
        //结论: [可能是String的缘故] [我猜测可能与Qt中String类似,当被修改后,才会再创建一个对象]
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值