简体   繁体   English

Java:匿名集<t>在 function 调用中</t>

[英]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.正如在这个问题中发现的匿名 arrays in Java我尝试了相同的程序java.utils.Set ,但它不起作用。

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?将匿名 Set 传递给 Java 中的 function 的正确方法是什么?

Try this:尝试这个:

When first encountered it will add the HashSet and add the value.首次遇到时,它将添加HashSet并添加值。 The next times it is encountered it will add the value to the hashSet.下次遇到它时,它会将值添加到 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 .如果您希望您的新集合是non-modifiable并且没有 null 值,请使用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-如果您尝试添加匿名 Set,那么在这种情况下,您还需要拥有 Set 的完整实现 -

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我不建议以匿名方式创建集合,而不是您可以使用一些实用方法,例如Collections.singleton用于java-89版本代码,您可以使用 Java 及以上版本代码。

an immutable set containing only the specified object.一个不可变的集合,只包含指定的 object。

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

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

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