Redis with java


public void verProduto(){
    //System.out.println("Digite o codigo do produto : - ");
    //produt.setCodigo(scan.nextInt());
    List<String> list =  (List<String>) jeproduto.keys("*"); 
for(int i = 0; i<list.size(); i++) { 
    System.out.println("List of stored keys:: "+list.get(i)); 
}  
}

此代码返回错误:

java.util.hashset不能被施放到java.util.list

有人可以帮忙吗?

您没有提供很多细节,但您可以尝试:

List<String> list =  new ArrayList<String>(jeproduto.keys("*")); 

假设您正在使用jedis,并且jeproduto对象是Jedis实例,根据Javadocs,keys方法返回类型Set<String>

您有两个选择:

将收集类型更改为集合而不是列表:

Set<String> keySet = jeproduto.keys("*");
for (String key : keySet) {
    System.out.println("List of stored keys: " + key); 
}

如果您确实需要一个列表,请执行提到的罐头罐头并利用允许您通过另一个集合的Arraylist构造函数。

List<String> list = new ArrayList<>(jeproduto.keys("*"));
Since your code is not fully completed.
My Assumption is
   jeproduto = new HashSet<String>

then
List<String> list =  (List<String>) jeproduto.keys("*"); line should be
// Creating a List of HashSet elements
 List<String> list = new ArrayList<String>(hset);

 if you want to convert Hashset to List, please find the sample code below.
 public static void main(String[] args) {
      // Create a HashSet
      HashSet<String> hset = new HashSet<String>();
      //add elements to HashSet
      hset.add("A");
      hset.add("B");
      hset.add("C");
      hset.add("D");
      hset.add("E");
      // Displaying HashSet elements
      System.out.println("HashSet contains: "+ hset);
      // Creating a List of HashSet elements
      List<String> list = new ArrayList<String>(hset);
      // Displaying ArrayList elements
      System.out.println("ArrayList contains: "+ list);
  }

最新更新