[英]Clear a HashMap after adding to a List, in Java
如果您能帮助我使用以下 Java 代码,我将不胜感激:
import java.util.ArrayList;
import java.util.HashMap;
public class JavaApplication2 {
static ArrayList<HashMap> list_test = new ArrayList<HashMap>();
static HashMap<String,String> test =new HashMap<>();
public static void main(String[] args) {
test.put("name", "jhon");
test.put("lastname", "mckoy");
list_test.add(test);
test.clear();
System.out.println(list_test);
test.put("name", "Tom");
test.put("lastname", "Red");
list_test.add(test);
}
}
我有两个变量,一个叫做 list_test (ArrayList),另一个叫做 test (HashMap <String, String>)。 我的目标是使用以下代码制作 HashMap 的 ArrayList,但在执行后: test.clear ();
然后 list_test 的内容被删除。 为什么? 还有其他方法可以制作 HashMap 列表吗? 哪个?
提前致谢
使用您的代码,您放入列表中的 map object 与您清除的 map ZA8CFDE6331BD59EB66AC96F8911 相同。 相反,将您的 map 的副本添加到您的列表中。
listOfMapsOfNames.add( Map.copyOf( mapOfNames ) ); // Make an unmodifiable copy of map to be added to list.
显然,您希望将 map 的副本添加到列表中。
您的代码将原始 map 放入列表中。 So now you have two references to the same map: (a) the variable test
points to the map, and (b) the first element in the list named list_test
points to the very same map object. 如果您使用任一引用来更改地图的内容,则两个引用都会看到该更改,因为它们都指向相同的 object 。
任何一个:
Map.copyOf
以制作 map 的不可修改副本。 例如: Map.copyOf( oldMap )
Map
实现的构造函数,以将条目复制到新的可修改 map。 例如: new HashMap<>( oldMap )
。 在实用的情况下,您应该考虑养成在参考文献中使用更通用的超类或接口而不是具体的 class 的习惯。 所以List<Map> list_test
而不是ArrayList<HashMap> list_test
。
您应该参数化List
的参数化。 所以List< Map< String, String > >
而不是List< Map >
。
在 Java 标识符上使用更具描述性的名称将使您的代码更易于阅读和调试。 所以像listOfMapsOfNames
而不是list_test
。
这是一些示例代码。
我将static
放在 map 上并列出,因为这无关紧要。
注意两个调用Map.copyOf( mapOfNames )
。
List< Map< String, String > > listOfMapsOfNames = new ArrayList<>() ;
Map< String, String > mapOfNames = new HashMap<>() ;
mapOfNames.put( "name", "John" );
mapOfNames.put( "lastname", "McKoy" );
listOfMapsOfNames.add( Map.copyOf( mapOfNames ) ); // Make an unmodifiable copy of map to be added to list.
System.out.println( "map before clear: " + mapOfNames );
mapOfNames.clear();
System.out.println( "map after clear: " + mapOfNames );
mapOfNames.put( "name", "Tom" );
mapOfNames.put( "lastname" , "Red" );
listOfMapsOfNames.add( Map.copyOf( mapOfNames ) ); // Make an unmodifiable copy of map to be added to list.
System.out.println( "map after 2nd add: " + mapOfNames );
System.out.println( "list at the end: " + listOfMapsOfNames );
map before clear: {name=John, lastname=McKoy}
map after clear: {}
map after 2nd add: {name=Tom, lastname=Red}
list at the end: [{lastname=McKoy, name=John}, {lastname=Red, name=Tom}]
record
与您的问题无关,但为了您的信息,使用 class 可能比使用Map
跟踪名字和姓氏更有意义。 现在我们有了更简单的方法来编写这样的 class: records 。
记录功能是Java 16中的新功能。 记录是编写 class 的简要方法,其主要目的是透明且不可变地传递数据。 编译器隐式创建构造函数、getter、 equals
& hashCode
和toString
。 奖励:记录可以在本地声明,也可以声明为嵌套的或单独的。
请注意,您的代码的以下版本使用record
变得更具表现力(并且更短)。
record Person(String firstName , String lastName) {}
List < Person > people = new ArrayList <>();
people.add( new Person( "John" , "McKoy" ) );
people.add( new Person( "Tom" , "Red" ) );
people.toString(): [Person[firstName=John, lastName=McKoy], Person[firstName=Tom, lastName=Red]]
解决方案:
这是代码:
public class ListOfMapJavaApplication2 {
static ArrayList<HashMap> list_test = new ArrayList<HashMap>();
static HashMap<String,String> test =new HashMap<>();
public static void main(String[] args) {
test.put("name", "jhon");
test.put("lastname", "mckoy");
//list_test.add(new HashMap<String,String>(test));
list_test.add((HashMap<String,String>)test.clone());
test.clear();
System.out.println(list_test);
test.put("name", "Tom");
test.put("lastname", "Red");
//list_test.add(new HashMap<String,String>(test));
list_test.add((HashMap<String,String>)test.clone());
}
}
这是因为引用调用而发生的,您可以使用 new 关键字获取新引用。
import java.util.ArrayList;
import java.util.HashMap;
public class Main
{
static ArrayList<HashMap> list_test = new ArrayList<HashMap>();
static HashMap<String,String> test =new HashMap<>();
public static void main(String[] args) {
test.put("name", "jhon");
test.put("lastname", "mckoy");
list_test.add(test);
test = new HashMap<>();
System.out.println(list_test);
test.put("name", "Tom");
test.put("lastname", "Red");
list_test.add(test);
System.out.println(list_test);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.