简体   繁体   English

Java集合和内存使用

[英]Java collections and memory use

I have a question on Java memory use. 我对Java内存使用有疑问。 It's for my edification and anyone else who searches and finds this later! 这是为了我的教育和以后搜索并找到它的其他人! For the purpose of the question, please assume, this is a single method and nothing goes out of scope... during my question ;-) 出于问题的目的,请假设,这是一个单一方法,没有超出范围...在我的问题中;-)

I have created 5 new objects with a single property called 'name' of type String. 我创建了5个新对象,它们的单个属性名为String类型的“名称”。 I create an ArrayList and add the 5 objects to the ArrayList. 我创建一个ArrayList并将5个对象添加到ArrayList中。 I then create a HashMap and iterate through the previously created ArrayList, adding the objects to the HashMap. 然后,我创建一个HashMap并遍历先前创建的ArrayList,将对象添加到HashMap中。

Q1. Q1。 When I add the objects from the ArrayList, to the HashMap, I assume I am just creating another collection of 'pointers', since I'm not using the 'new' keyword. 当我将对象从ArrayList添加到HashMap时,我假设我只是在创建另一个“指针”集合,因为我没有使用“ new”关键字。 Therefore no new memory is consumed, except for the HashMap itself (the objects are not duplicated). 因此,除了HashMap本身(不重复对象)之外,没有消耗任何新的内存。

Q2. Q2。 If I change the value of 'name', in an object in the HashMap, would the same change be seen, if I were to iterate over the ArrayList, after making the change. 如果我更改了HashMap中的对象中的“名称”的值,则在进行更改之后,如果要遍历ArrayList,是否会看到相同的更改。

I appreciate a 'sanity check' on my understanding. 我对我的理解表示赞赏。

Q1: The HashMap is created and the references to the objects are created. 问题1:创建HashMap,并创建对对象的引用。 So memory is consumed, but references aren't terribly big, but can make a difference if the number of references is huge. 这样就消耗了内存,但是引用并不是很大,但是如果引用数量很大,则可以有所作为。

Q2: Edit: Yes, the name field would change. Q2:编辑:是的,名称字段将更改。 Better still, write a small program to check it out. 更好的是,编写一个小程序来检查它。

A1 : Yes, other than the references and HashMap, nothing new will be created. A1:是的,除了引用和HashMap外,不会创建任何新内容。 (Assuming you are not creating a new set of keys for for the HashMap) (假设您没有为HashMap创建一组新的密钥)

A2 : Yes, the change will reflect on the ArrayList. A2:是的,更改将反映在ArrayList上。

To answer your questions. 回答您的问题。

1.) When you add objects to a HashMap the objects are not duplicated. 1.)将对象添加到HashMap ,对象不会重复。 Internally though the map will create new objects to maintain its inner structure. 在内部,地图会创建新对象来维护其内部结构。 The inner structure of a map consists of HashMap.Entry objects that contain a linked list with all values that map to the same hash code. 映射的内部结构由HashMap.Entry对象组成,这些对象包含一个链接列表,该链接列表的所有值都映射到相同的哈希码。 Thus whenever you add objects to a map one or more internal objects are created. 因此,无论何时将对象添加到地图,都会创建一个或多个内部对象。

2.) I assume you stored the objects in the HashMap using their name as key. 2.)我假设您使用对象的名称作为键将对象存储在HashMap In this case chaning the name of an object will update the object (no matter whether it's being accessed through the list or the map, it's always the same object) but not the mapping in the map. 在这种情况下,更改对象名称将更新该对象(无论是通过列表访问还是通过地图访问,它始终是同一对象),而不是地图中的映射。 In the map the object will still be store under its old name! 在地图中,该对象仍将以其旧名称存储!

 Map map = new HashMap();
 Foo f = new Foo();
 f.setName("A");
 map.put(f.getName(),f);
 f.getName(); // => "A"
 map.get("A"); // => f
 f.setName("B");
 f.getName(); // => "B"
 map.get("B"); // => null
 map.get("A"); // => f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM