16

i was having working code with earlier version of java 8 which i was using to get unique values from list but since i upgraded to JDK 66 its giving me an error

Type mismatch: cannot convert from List<Object> to List<String>

List<String> instList = new ArrayList<String>();

while (res.next()) {
    instList.add(res.getString("INST").toString());
}           

List<String> instListF = instList.stream().distinct().collect(Collectors.toList());

Where res is resultset i am getting from database, not sure what is wrong any idea?


How to handle Recyclerview list item click event

I am working with recyclerview but I am trying to intent to next activity but there is not setonlistitem method can any one tell how to redirect to next activity on click of list item,following is my snippet code can any one help me with that

public class SubcategoryActivity extends AppCompatActivity{

    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.subcatgory_listviews);


        mToolbar = (Toolbar) findViewById(R.id.toolbarsubcategory_list);
        setSupportActionBar(mToolbar);
        setTitle(getString(R.string.app_name));
        mToolbar.setTitleTextColor(getResources().getColor(android.R.color.white));

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerViewsubcategory);


        ItemData itemsData[] = { new ItemData("Help",R.drawable.logo),
                new ItemData("Delete",R.drawable.logo),
                new ItemData("Cloud",R.drawable.logo),
                new ItemData("Favorite",R.drawable.logo),
                new ItemData("Like",R.drawable.logo),
                new ItemData("Rating",R.drawable.logo)};


        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        MyAdapter mAdapter = new MyAdapter(itemsData);
        recyclerView.setAdapter(mAdapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());




    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }
}
4

4 回答 4

15

好吧,我最近也遇到了类似的错误Type mismatch: cannot convert from Set<Object> to Set<String>。下面是代码片段-:

public static void main(String[] args) {
    String[] arr = new String[]{"i", "came", "i", "saw", "i", "left"};
    
    Set<String> set = Arrays.asList(arr).stream().collect(Collectors.toSet());
    
    System.out.println(set.size() + "  distinct words: " + set);
}

这是供参考的屏幕截图-: 在此处输入图像描述

现在让我解释一下为什么会出现这个错误?在我的情况下,代码显示编译时错误,因为项目属性中的编译器版本不匹配。我选择了1.7但它应该是1.8因为这个功能已经添加到1.8中。

在此处输入图像描述

所以请注意以下几点:

  1. 在Java Build Path中选择了适当的 JDK 。在这种情况下,例如JDK 1.8 。
  2. 必须在项目属性中的Java Compiler如上图所示)下选择适当的编译器版本。例如1.8

我希望这会对你有所帮助。

于 2017-01-31T06:27:05.223 回答
4

我检查了以下完整示例:

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.stream.Collectors;
import java.util.List;
import java.util.ArrayList;

public class Test { 
    public List<String> test(ResultSet res) throws SQLException {
        List<String> instList = new ArrayList<String>();

        while (res.next()) {
            instList.add(res.getString("INST").toString());
        }           

        List<String> instListF = instList.stream().distinct().collect(Collectors.toList());
        return instListF;
    }
}

它与 javac 8u25、8u40、8u60、8u71 完美编译(注意 8u71 是 8u66 的安全更新,因此基本相同)。尝试清理您的项目并从头开始重建。

于 2016-01-25T10:47:41.687 回答
1

在检查了我的编译器级别(根据上面的 Ashish)之后,我意识到我在 List 或 Set 上都没有数据类型。一旦我添加它就可以了。

List<Integer> number = Arrays.asList(2, 3, 4, 5, 3);
Set<Integer> square = number.stream()
     .map(x -> x * x)
     .collect(Collectors.toSet());
于 2019-09-19T19:40:54.720 回答
0

请注意,如果您收到此错误并且由于某种原因您被 Java <1.8 卡住,您可以编写以下内容:

... .collect(Collectors.<String>toList());
于 2022-01-14T11:52:50.407 回答