简体   繁体   English

我可以将一个变量用于不同的容器(列表,集合,映射)吗?

[英]Can I use one variable for different containers (List, Set, Map)?

It is necessary to change a container type: 有必要更改容器类型:

import java.util.*;

public class MyContainers {

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("title1");
        list.add("title2");
        System.out.println(list.indexOf("title1"));

        // change container type
        Set<String> set = new HashSet<String>(list);
    }
}

But it will be better to use only one variable. 但是最好只使用一个变量。 I wrote such code but there was a restriction (see comment): 我编写了这样的代码,但是有一个限制(请参阅注释):

import java.util.*;


public class MyContainers {

    public static void main(String[] args) {
        Collection<String> list = new ArrayList<String>();
        list.add("title1");
        list.add("title1");

        System.out.println(list);

        // Can't call indexOf because there are no such method in inteface Collection
        //System.out.println(list.indexOf("title1")); 

        // change container type
        list = new HashSet<String>(list);
        System.out.println(list);   

    }
}

Please, help me with such questions: 请帮我解决以下问题:

  1. Is it possible to use one variable for different containers and use full set of containers methods? 是否可以将一个变量用于不同的容器并使用全套容器方法?
  2. Is it possible to convert List to Map ? 是否可以将List转换为Map Please, show a code examples. 请显示一个代码示例。

If some method doesn't exist in one interface , it doesn't exist for a reason. 如果某个方法在一个接口中不存在,则由于某种原因它不存在。 Don't try to have a workaround in order to have it, that'll usually lead to troubles. 不要为了拥有它而尝试解决方法,这通常会带来麻烦。 For example, it doesn't make any sense to have indexOf method for the Set interface . 例如,对于Set 接口具有indexOf方法没有任何意义。

However, it can be useful sometimes to construct a new object of different type from an existing one, for example, if you have an ArrayList and you don't want to have duplicates, it does make sense to convert it to HashSet . 但是,有时候构造一个与现有对象不同类型的新对象可能很有用,例如,如果您有ArrayList并且不想重复,则将其转换为HashSet确实有意义。

You should pick the best interface that suits your needs, if you don't find any, you can always implement your own class. 您应该选择适合自己需求的最佳接口 ,如果找不到任何接口 ,则可以随时实现自己的类。

I found the solution, is it good? 我找到了解决方案,好吗?

import java.util.*;

public class MyContainers {

    static Collection<String> collection = new ArrayList<String>();

    public static void main(String[] args) {
        collection.add("title1");
        collection.add("title1");

        System.out.println(collection); // [title1, title1]
        useIndexOf();
        System.out.println(collection); // [title1, title1]
        deleteDublication();
        System.out.println(collection); // [title1]
    }

    public static void useIndexOf()
    {
        List<String> list = new ArrayList<String>(collection);
        System.out.println(list.indexOf("title1")); // 0
        // Change container type back to universal
        collection = list;
    }

    public static void deleteDublication() {
        Set<String> set = new HashSet<String>(collection);
        collection = set;
    }

}

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

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