2

I have a generic function that returns a List<?>. If it works correctly, it will return a List<LightSensor>, but I cannot directly convert the result of the function to a variable of type List<LightSensor>. How can I check if the class in the List is a LightSensor correctly? (e.g. check if List<?> = List<LightSensor>) Also, What is the best way to cast it correctly once I've checked?
BTW the class LightSensor extends Object and a version of Serializable


How to select duplicates to get proper results?

Could you please help me to write working sql query?

I need to select all duplicate link_rewrite. It's duplicate only if the id_shop, id_lang are the same.

See full code: https://github.com/Ha99y/prestashopCleanURLs/blob/master/PS15/cleanurls.php#L49

-------------------------------------------------------
| id_product  |  id_shop  |  id_lang  |  link_rewrite |
-------------------------------------------------------
|     1       |    1      |     1     |   ipod-nano   |
|     1       |    1      |     2     |   ipod-nano   |
|     2       |    1      |     1     |   ipod-nano   |
|     2       |    1      |     2     |   ipod-nano   |
|     8       |    2      |     1     |   ipod-nano   |
|     8       |    2      |     2     |   ipod-nano   |
-------------------------------------------------------

SQL:

SELECT * FROM `ps_product_lang`
WHERE `link_rewrite`
IN (SELECT `link_rewrite` FROM `ps_product_lang`
GROUP BY `link_rewrite`, `id_lang`
HAVING count(`link_rewrite`) > 1)

.

Any help appreciated

4

2 に答える 2

1

メソッドからジェネリック コレクションを返すイディオムは次のようなものです。

public <T> List<T> getTheList(Class<T> cls){
  // you might need the cls in here to create the list.
  ...
}

を使用してリストを生成する方法は、明らかにClass<T>アプリケーション固有です。ほとんどの場合、これはいくつかのクラス階層のコンテキストで意味があり、その場合、Tいくつかの基本クラスまたはインターフェースを拡張します。

public <T extends MyBaseClassOrInterface> List<T> getTheList(Class<T> cls){
  ...
}

ここでも、リストの作成方法は完全にアプリケーションに依存します。アプリケーション全体を強く型付けしたままにしようとすると、 の型パラメーターを把握するために何らかの黒魔術を行うことを回避できる場合がありますList<?>。そのようなコードになってしまうと、それは強いコード臭です (明らかにレガシー コードを扱っている場合を除きます)。さらに、リストが空の場合は、まったく絶望的です。

于 2014-06-25T19:04:25.287 に答える