简体   繁体   中英

How to add elements from a generic type to a list <long> in JAVA?

I'm new at posting questions here, but have been looking since yesterday for a solution for this and couldn't find. Would really appreciate the help.

I have two lines, at which I'm trying to put generic elements inside a list and get a type mismatch for sending Long elements to List.

List<Long> returnedPages = m_algo.getElement(Arrays.asList(pageIds));
List<Long> pagesNotInRam = new ArrayList<>();
List<Long> pageIdsToHandle = m_algo.putElement(pagesNotInRam, pagesNotInRam);

m_algo is defined

private IAlgoCache<Long, Long> m_algo;

and IAlgoCache works with generic types

public interface IAlgoCache <K, V> {
public V getElement(K key);
public V putElement(K key, V value);}

Can I make this work? Should I work in some sort of loop?

There are several problems with your code. For example

public V getElement(K key); // takes an object as an argument (Long in your case)
m_algo.getElement(Arrays.asList(pageIds));  // you pass a list of objects (I suppose longs)

And even if you fix that issue and pass a proper key later you are trying to assign a Long object to a List. That's why it complains - assigning and object to a collection cannot be done because they are basically different types.

List<Long> returnedPages = m_algo.getElement( properKey ); // Problem

Try with:

 List<Long> returnedPages=new ArrayList();
 returnedPages.add(m_algo.getElement( properKey));

The problem with putElement is the same. You just need to distinguish between an object and an array or collection of objects. Even if they have the same type they are not interchangeable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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