-3

When I run this program:-

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");

    }
}

I get 3 errors :-

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

I am trying to use HashMap value as LinkedHashSet, but getting these errors. What shall I do?

Vibhor Verma
  • 161
  • 1
  • 4
  • 13
  • 1
    **1** - If you want to put only `LinkedHashSet` in your `Map`, you should change `Object` to `Set`. **2** - Your `System.out....` line has no sense (you call `add` on the return value which is void, you try to instance a `LinkedHashSet` without new), what do you want to do here ? – Dorian Apr 18 '17 at 06:11
  • You have a basic syntax error at `LinkedHashSet(props.get("videos")).add("yoyo")` but it's not clear what you intended. – Jim Garrison Apr 18 '17 at 06:14
  • @Dorian at some places I need LinkedHashSet and at some place I have to use String – Vibhor Verma Apr 18 '17 at 06:14
  • @Dorian there is an error I (I think a silly one) – Vibhor Verma Apr 18 '17 at 06:17
  • @VibhorVerma I think it's a very bad way. Why not put only `LinkedHashSet` in your map ? You can put only one value in the `Set` if you want, it's ok. So you won't have to check the type of the value and cast it when you call `get` in your `Map`. – Dorian Apr 18 '17 at 06:23
  • @Dorian ok, but I have very strict time complexity issue, does it affect time complexity of the code while accessing and changing value? – Vibhor Verma Apr 18 '17 at 06:29
  • 1
    @VibhorVerma easier for accessing I think (no need to check type and cast), maybe a little bit harder to change value (`props.put("myKey", new LinkedHashSet(Arrays.asList("myValue"))`). If you want to add value, it's easier to : (I suppose myKey exists in map : `props.get("myKey").add("valueToAdd")`) – Dorian Apr 18 '17 at 06:37

3 Answers3

1

You have an error in your syntax, first you need to convert object to LinkedHashSet and then add a string to it

   System.out.println( ((LinkedHashSet<String>)(props.get("videos")) ).add("video_url") );
1

I guess you want to use java.util.LinkedList as the value of Map.

Map<String, List<String>> props = new HashMap<>();
props.putIfAbsent("videos", new LinkedList<String>());
props.get("videos").add("video_url");
System.out.println(props);

But if you want to use a map which iterate as the same order of put order.

Map<String, String> props = new LinkedHashMap<>();
props.putIfAbsent("videos", "video_url");
System.out.println(props);
liuzhengyang
  • 386
  • 1
  • 11
  • you are right but I need to update this by adding more video_url, and at some place I need HashMap value as String, so value as Object looks good – Vibhor Verma Apr 18 '17 at 06:20
  • 1
    @VibhorVerma You can get the first element of the `List` to get `String` value (`props.get("key").stream().first().orElse("no value in list");`), it's a lot better than store `Object`, sometimes `Set`, sometimes `String` and then check the type and cast to it. – Dorian Apr 18 '17 at 06:27
  • @Dorian , thank you this gives an approach, how shall I tackle the problem :) – Vibhor Verma Apr 18 '17 at 06:46
  • @liuzhengyang , – Vibhor Verma Apr 18 '17 at 06:46
-2

The correct code looks like:

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"));
    }
Jay Smith
  • 2,331
  • 3
  • 16
  • 27