【问题标题】:Replace google-collect Lists.newArrayList() with other List用其他列表替换 google-collect Lists.newArrayList()
【发布时间】:2015-12-10 10:04:18
【问题描述】:

使用 Eclipse Luna 我正在尝试运行导入已弃用的 google-collections 的 java 代码,该代码在使用最新 Guava 版本编译时会引发异常。

public static void run(String consumerKey, String consumerSecret, String token, String secret) throws InterruptedException
{

    BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);
    StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
    endpoint.trackTerms(Lists.newArrayList("twitterapi", "#AAPSweep"));
    Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);

    Client client = new ClientBuilder()
    .hosts(Constants.STREAM_HOST)
    .endpoint(endpoint)
    .authentication(auth)
    .processor(new StringDelimitedProcessor(queue))
    .build();

    client.connect();

我试图从插件中删除 com.google.guava_15.0.0.v201403281430 文件,并尝试按照here in a comment 的说明粘贴 Guava 旧版本,但我无法安装(指向)旧 guava 版本。 这个问题可能还有另一种解决方案here,但我是 java 新手,不知道如何用其他列表替换该列表。

请问有没有人可以使用其他 List 方法运行该代码? 要么 告诉我如何在 Eclipse 中添加旧版本的番石榴(我不确定这会解决这个问题,只是从一个线程中读取它) 要么 告诉我其他解决方案。谢谢

【问题讨论】:

    标签: java twitter arraylist guava twitter-hbc


    【解决方案1】:

    Guava Lists.newArrayList 只是创建标准 JDK ArrayList 并用预定义值填充它的简写。您可以使用 Arrays.asList 以更长的方式完成此操作,而无需任何第三方库:

    endpoint.trackTerms(new ArrayList<>(Arrays.asList("twitterapi", "#AAPSweep")));
    

    如果您不需要在创建的列表中进行结构更改,甚至更简单:

    endpoint.trackTerms(Arrays.asList("twitterapi", "#AAPSweep"));
    

    【讨论】:

      猜你喜欢
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-02
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多