SORU
21 Mart 2012, ÇARŞAMBA


Liste<Göster<Dize, Dize>> vs Listesi<? Göster<Dize, Dize> uzanır;>

Arasında herhangi bir fark yoktur

List<Map<String, String>>

ve

List<? extends Map<String, String>>

?

Eğer fark varsa hayır ? extends kullanmanın yararı nedir?

CEVAP
21 Mart 2012, ÇARŞAMBA


Fark, örneğin, bir

List<HashMap<String,String>>

bir

List<? extends Map<String,String>>

ama bir değil

List<Map<String,String>>

Yani:

void withWilds( List<? extends Map<String,String>> foo ){}
void noWilds( List<Map<String,String>> foo ){}

void main( String[] args ){
    List<HashMap<String,String>> myMap;

    withWilds( myMap ); // Works
    noWilds( myMap ); // Compiler error
}

HashMap s ListMap s Listolması gerektiğini düşünürdüm, ama değil neden iyi bir nedeni var:

Sen farz et:

List<HashMap<String,String>> hashMaps = new ArrayList<HashMap<String,String>>();

List<Map<String,String>> maps = hashMaps; // Won't compile,
                                          // but imagine that it could

Map<String,String> aMap = Collections.singletonMap("foo","bar"); // Not a HashMap

maps.add( aMap ); // Perfectly legal (adding a Map to a List of Maps)

// But maps and hashMaps are the same object, so this should be the same as

hashMaps.add( aMap ); // Should be illegal (aMap is not a HashMap)

Bu HashMap s ListMap s Listyaramaması.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Bigapplemagic

    Bigapplemagi

    22 EYLÜL 2011
  • MrChiCity3

    MrChiCity3

    14 NİSAN 2008
  • The Verge

    The Verge

    8 AĞUSTOS 2006