如何将 HashMap 用作 LinkedHashSet
How to use HashMap as LinkedHashSet
当我运行这个程序时:-
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
public class try1 {
public static void main(String args[]) {
Map<String, Object> props = new HashMap<String, Object>();
props.putIfAbsent("videos", new LinkedHashSet<String>());
System.out.println(LinkedHashSet<String>(props.get("videos")).add("video_url");
}
}
我收到 3 个错误:-
try1.java:14: error: cannot find symbol
System.out.println(LinkedHashSet<String>(props.get("videos")).add("yoyo"));//.add("video_url");
^
symbol: variable LinkedHashSet
location: class try1
try1.java:14: error: cannot find symbol
System.out.println(LinkedHashSet<String>(props.get("videos")).add("yoyo"));//.add("video_url");
^
symbol: variable String
location: class try1
try1.java:14: error: cannot find symbol
System.out.println(LinkedHashSet<String>(props.get("videos")).add("yoyo"));//.add("video_url");
^
symbol: method add(String)
location: class Object
3 errors
我正在尝试将 HashMap 值用作 LinkedHashSet,但遇到了这些错误。我该怎么办?
正确的代码如下:
public static void main(String[] args) {
Map<String, Object> props = new HashMap<String, Object>();
props.putIfAbsent("videos", new LinkedHashSet<String>());
System.out.println(((LinkedHashSet)(props.get("videos"))).add("video_url"));
}
您的语法有误,首先您需要将对象转换为LinkedHashSet,然后向其中添加一个字符串
System.out.println( ((LinkedHashSet<String>)(props.get("videos")) ).add("video_url") );
我猜你想使用 java.util.LinkedList 作为 Map 的值。
Map<String, List<String>> props = new HashMap<>();
props.putIfAbsent("videos", new LinkedList<String>());
props.get("videos").add("video_url");
System.out.println(props);
但是如果你想使用一个映射,它迭代为相同的订单订单。
Map<String, String> props = new LinkedHashMap<>();
props.putIfAbsent("videos", "video_url");
System.out.println(props);
当我运行这个程序时:-
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
public class try1 {
public static void main(String args[]) {
Map<String, Object> props = new HashMap<String, Object>();
props.putIfAbsent("videos", new LinkedHashSet<String>());
System.out.println(LinkedHashSet<String>(props.get("videos")).add("video_url");
}
}
我收到 3 个错误:-
try1.java:14: error: cannot find symbol
System.out.println(LinkedHashSet<String>(props.get("videos")).add("yoyo"));//.add("video_url");
^
symbol: variable LinkedHashSet
location: class try1
try1.java:14: error: cannot find symbol
System.out.println(LinkedHashSet<String>(props.get("videos")).add("yoyo"));//.add("video_url");
^
symbol: variable String
location: class try1
try1.java:14: error: cannot find symbol
System.out.println(LinkedHashSet<String>(props.get("videos")).add("yoyo"));//.add("video_url");
^
symbol: method add(String)
location: class Object
3 errors
我正在尝试将 HashMap 值用作 LinkedHashSet,但遇到了这些错误。我该怎么办?
正确的代码如下:
public static void main(String[] args) {
Map<String, Object> props = new HashMap<String, Object>();
props.putIfAbsent("videos", new LinkedHashSet<String>());
System.out.println(((LinkedHashSet)(props.get("videos"))).add("video_url"));
}
您的语法有误,首先您需要将对象转换为LinkedHashSet,然后向其中添加一个字符串
System.out.println( ((LinkedHashSet<String>)(props.get("videos")) ).add("video_url") );
我猜你想使用 java.util.LinkedList 作为 Map 的值。
Map<String, List<String>> props = new HashMap<>();
props.putIfAbsent("videos", new LinkedList<String>());
props.get("videos").add("video_url");
System.out.println(props);
但是如果你想使用一个映射,它迭代为相同的订单订单。
Map<String, String> props = new LinkedHashMap<>();
props.putIfAbsent("videos", "video_url");
System.out.println(props);