简体   繁体   中英

Java: anonymous Set<T> inside a function call

As found in this question Anonymous arrays in Java I tried the same procedure for a java.utils.Set , but it does not work.

If i have

private HashMap<String, String[]> flags;

I can easily call

this.flags.put("test", new String[]{"value"});

But with

private HashMap<String, Set<String>> flags;

this does not work :

this.flags.put("test", new Set<String>().add("value"));

What is the correct way, to pass an anonymous Set into a function in Java?

Try this:

When first encountered it will add the HashSet and add the value. The next times it is encountered it will add the value to the hashSet.

this.flags.computeIfAbsent("test", v-> new HashSet<>()).add("value");

Set.of

If you want your new set to be non-modifiable , and without null values, use Set.of .

private Map< String , Set< String > > flags = new HashMap<>() ;
…
this.flags.put( "test" , Set.of( "value" ) );

If you try to attempt to add an anonymous Set, then in that case you need to have the complete implementation of you Set as well-

Ex:

        HashMap<String, Set<String>> valueMap = new HashMap<>();


        valueMap.put("test", new Set<String>() {

            @Override
            public boolean add(String e) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean addAll(Collection<? extends String> c) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void clear() {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean contains(Object o) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean containsAll(Collection<?> c) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean isEmpty() {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public Iterator<String> iterator() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public boolean remove(Object o) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean removeAll(Collection<?> c) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean retainAll(Collection<?> c) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public int size() {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public Object[] toArray() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public <T> T[] toArray(T[] a) {
                // TODO Auto-generated method stub
                return null;
            }
        });


A better and simple approach would be

valueMap.put("test", new HashSet<String>(){{
            add("a");
            add("b");
            add("c");
        }});

I would not recommend to create set in anonymous way, rather than you can use some utility methods like Collections.singleton for java-8 version code, and you can use Set.of for java-9 and above

an immutable set containing only the specified object.

this.flags.put("test", Collections.singleton("value"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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