Java 7ããªãªã¼ã¹ããã¾ãããããéååãã7ã®è©±é¡ã¯ãã£ããã®ã®ããã£ã¨å»¶æããã¦ããã§åºãäºèªä½ãä¸æè°ãªæ°ããã¾ãããçæ§ããããéããã§ãããããJava 6ã2006å¹´12æã§ãããã»ã¼5å¹´ãçµã£ã¦ãã¾ã£ãã®ã§ããã
Java 7ã®ãªãªã¼ã¹ãã¼ãã¯
ãã¦ãæ¬ç¨¿ã®ã¿ã¼ã²ããã¯ã¸ã§ããªã¯ã¹ã®ãã¤ã¢ã¢ã³ãã»ãªãã¬ã¼ã¿ã§ãã
ããã¯ä»¥ä¸ã®ããã«ã¸ã§ããªãã¯ãªå¤æ°ã«newã§ã¤ã³ã¹ã¿ã³ã¹ãçæãã¦ä»£å ¥ããã¨ããã±ã¼ã¹ã§ã®åé·è¡¨è¨ãç°¡ç´ ååºæ¥ãæ©è½ã§ãã
// Java6ã¾ã§ã®æ¸ãæ¹ List<String> list = new ArrayList<String>(); // Java7ã§ã®ãã¤ã¢ã¢ã³ãã»ãªãã¬ã¼ã¿ List<String> list = new ArrayList<>();
ã¾ãStringãããã ã¨å¯æããã®ã§ãã
List<List<Map<String, WeakReference<B>>>> list = new ArrayList<>();
ã¿ãããªã±ã¼ã¹ã§ã¯å®ã«ã·ã³ãã«ã§å¬ããããããã³ã¬ã¯ã·ã§ã³ãããã¾ã§å¤é層ã«ããããããªãclassãä½ãã¨ãã話ã§ã¯ããã¾ããã
ãã¤ã¢ã¢ã³ãã»ãªãã¬ã¼ã¿ã使ç¨ã§ããç®æ
ãã¤ã¢ã¢ã³ãã»ãªãã¬ã¼ã¿ã¯newæ¼ç®åã¨å¯¾ã«ãªãããã§ããããããnewã§ããç®æãªãã©ãã§ã使ããã¨ãã訳ã§ã¯ããã¾ããã
å®ç¨çãªè©±ãè¨ãã°ãå¤æ°ã¸ã®ä»£å
¥ã§ã®ã¿å©ç¨å¯è½ã¨è¦ãã¦ããã°ããã§ãããã
ä¾ãã°ä»¥ä¸ã®ã±ã¼ã¹ã§ãã
public void hoge() { // å¤æ°å®£è¨ã®åæå¤ã¨ã㦠List<String> list = new ArrayList<>(); // åç´ãªä»£å ¥ã¨ã㦠list = new ArrayList<>(); // å¢çãæã¤å¤æ°ã¸ã®ä»£å ¥ List<? extends String> list2 = new ArrayList<>(); List<? super String> list3 = new ArrayList<>(); List<? extends List<? extends String>> list4 = new ArrayList<>(); // é£éçãªå¤æ°ã®åæå¤ã®ä¾ List<String> list4, list5 = list4 = new ArrayList<>(); } // åå¤æ°ã使ç¨ããã¸ã§ããªãã¯ãªå¤æ°å public <T> void hoge2() { List<T> list = new ArrayList<>(); } // ã¸ã§ããªãã¯ãªé åã®è¦ç´ ã¸ã®ä»£å ¥ public void hoge3(List<String> ... listArray) { listArray[0] = new ArrayList<>(); } // returnå¤ public List<String> hoge4() { return new ArrayList<>(); }
ããã«å¯¾ãããããã使ç¨ã§ããªãä¾ã¯ä»¥ä¸ã®ã±ã¼ã¹ã§ãã
- ã¡ã½ããã®å¼æ°ã«æ¸¡ãå ´å
public static void hoge(List<String> list){} public static void main(String[] args) { hoge(new ArrayList<>()); // NG }
- ä¸é æ¼ç®åãç¨ããå ´å
boolean b = true; List<String> l2 = b ? new ArrayList<>() : new LinkedList<>(); // NG
returnã¯ä¾å¤çã§ããããããå¤æ°ã¸ã®ç´æ¥çãªä»£å ¥ã«éãå©ç¨åºæ¥ãã¨è¦ãã¦ããã°å¤§ä¸å¤«ã§ããããå®ç¨çã«ã¯ã
å¢çããã¤å¤æ°ã¸ä»£å ¥ããå ´å
ã¨ããã§ããã£ãããã£ã¨æ¸ãã¦ã¾ãã
// å¢çãæã¤å¤æ°ã¸ã®ä»£å ¥ List<? extends String> list2 = new ArrayList<>(); List<? super String> list3 = new ArrayList<>(); List<? extends List<? extends String>> list4 = new ArrayList<>();
ãããä¸æè°ã ã¨æãã¾ãããã
ä½ãä¸æè°ãåãããªãï¼ã§ã¯ã以ä¸ãã³ã³ãã¤ã«ãã¦ã¿ã¾ãããã
List<? extends String> list2 = new ArrayList<? extends String>(); List<? super String> list3 = new ArrayList<? super String>(); List<? extends List<? extends String>> list4 = new ArrayList<? extends List<? extends String>>();
ã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã¾ãããï¼
newæ¼ç®åã§ã®åã®ãã¤ã³ãã§ã¯ã¯ã¤ã«ãã«ã¼ãã¯ä½¿ããªããã§ãã詳ããã¯Javaジェネリクス再入門 - プログラマーの脳みそããããè¦ã¦ãããã¨ãã¦ã
ãããã©ããªã£ã¦ããã¨ããã¨ãJavaã®ã¸ã§ããªã¯ã¹ã¯ã¤ã¬ã¤ã¸ã£æ¹å¼ãªã®ã§ãªããé©å½ã«new ArrayList()ãã¡ãã£ã¦ãããã§ããlist2ã¨ãã®å¤æ°ã«ä»£å
¥ããã¦ãã¾ãã°ããã¨ã¯åã®äºæãã§ãã¯ã¯ã§ãã¦ãã¾ãã¾ããããã
æ®æ®µãåãã¯list4ã®å®£è¨å List<? extends List<? extends String>>ã«å¯¾ãã¦newããã¨ãã®åã¯ã©ãããã°ããã®ï¼ã£ã¦æ©ãã ãããããã§ãããã³ã³ãã¤ã©ã®å®è£
å´ã¯ãããªæ¨è«ã¯å®ã¯å
¨ãããå¿
è¦ããªãã£ãã¨ãã話ã
ã¡ãªã¿ã«ãã¤ã¢ã¢ã³ãã使ããã«list4ã®ã³ã³ãã¤ã«ãéãããå ´åã¯list4 = new ArrayList<List<? extends String>>();ã¨ãã§ããã
2011.8.1ä¿®æ£ : Java 7以åãã¡ã½ããã¹ã³ã¼ãã®åå¤æ°ã®å ´åãåæ¨è«ãè¡ããã¦ããã®ã§ããããã®åæ¨è«ãå©ç¨ããã¦ããããã§ãã詳細ã¯å¥éã¨ã³ããªã«ã¾ã¨ãã¾ãããã®ä»¶ã«é¢ããtwitterä¸ã®è°è«ã¯ãã¡ãã®ã¾ã¨ããåè Java7 のダイアモンドオペレータと型推論について。 - Togetter
åæ¨è«ã«å¤±æããä¾
ãã®ãã¤ã¢ã¢ã³ããæ¨è«å¯è½ãªå ´æãã¤ã¾ãåè¿°ã®å¤æ°ã¸ã®ä»£å ¥ã¨ããã·ãã¥ã¨ã¼ã·ã§ã³ã§ã¯ããªãå¼·åºãªã®ã§ãããã²ã¨ã¤å¤±æããä¾ãè¦ã¤ãã¾ããã
public class Sample<T> { class X {} class Y<Z extends X> {} void hoge() { Y<X> y = new Y<>(); // OK } static void hoge2() { Sample<String> s = new Sample<>(); Sample<String>.Y<Sample<String>.X> y1 = s.new Y<>(); // NG Sample<String>.Y<Sample<String>.X> y2 = s.new Y<Sample<String>.X>(); // OK } }
ã¸ã§ããªãã¯ãªå¤é¨ã¯ã©ã¹ã®å
é¨ã¯ã©ã¹ãç¨ããå ´åã§ãå¤é¨ããinstance.new ã§åæåããã±ã¼ã¹ã§ãã
ã³ã¡ã³ãã§NGã¨ãã¦ããè¡ã¯ããã®ä¸ã®è¡ã®ããã«æ¨è«ããããã¨ãæå¾
ããããã§ãã
ã¨ã©ã¼: Sample.Y<>ã®åå¼æ°ãæ¨å®ã§ãã¾ããã
Sample.Y .X> y1 = s.new Y<>(); // NG
çç±: åå¤æ°Zã®ã¤ã³ã¹ã¿ã³ã¹ãåå¨ããªãã®ã§ãSample.Y ã¯Sample .Y .X>ã«é©åãã¾ãã
Z,Tãåå¤æ°ã®å ´å:
ã¯ã©ã¹ Sample.Yã§å®£è¨ããã¦ããZ extends Sample.X
ã¯ã©ã¹ Sampleã§å®£è¨ããã¦ããT extends Object
ã¨ã©ã¼1å
ã¨ãã£ãã¨ã©ã¼ãåºã¾ããã³ã³ãã¤ã«ç°å¢ã¯Oracleç´æ£JDKã§ãã¼ã¸ã§ã³1.7.0ã§ããã¡ãªã¿ã«NetBeansã®IDEä¸ã§ã¯ããã¯ã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã¾ãããã³ã³ãã¤ã©ã®ãã°ã®çããå¼·ãã§ããã
ãªãªã¼ã¹ãã¼ãã®è§£èª¬
ã®ä¸ã®æ¹ã®ãµã³ãã«ãåããã«ããã¨æãã®ã§è§£èª¬ãã¦ããã¾ãã
class MyClass<X> { <T> MyClass(T t) { // ... } }
ã¨ããã¯ã©ã¹ããã£ãå ´åã®è©±ã§ããããã®MyClassã®ã³ã³ã¹ãã©ã¯ã¿ã«ã¯<T>ã¨ããã³ã³ã¹ãã©ã¯ã¿ã¹ã³ã¼ãã®åå¤æ°ãæã£ã¦ãã¾ãã
ãã®ã³ã³ã¹ãã©ã¯ã¿ã¹ã³ã¼ãã®åå¤æ°ã¯ãã¡ã½ããã¹ã³ã¼ãã®åå¤æ°ã®å ´åã¨åçã«åæ¨è«ãå¹ãã¾ããããã¯Java7以åããããæ©è½ã§ããå端æã¯ããã¾ããã©ãã
ãã§ããã®ã³ã³ã¹ãã©ã¯ã¿ã¹ã³ã¼ãã®åå¤æ°ãçç¥ãã¦Tãåæ¨è«ãããå ´åã
MyClass<Integer> myObject = new MyClass<>("");
ã§ãã£ã¦ãæ£æ¸æ³ã§çç¥ããã«æ¸ããå ´åã
MyClass<Integer> myObject = new <String> MyClass<>("");
ã¨ããããã§ããã³ã³ã¹ãã©ã¯ã¿ã¹ã³ã¼ãã®åå¤æ°ã®åæ¨è«ã¯ä»ã¾ã§ãåºæ¥ããã©ããã¤ã¢ã¢ã³ãã ã¨Xã®æ¹ãåæ¨è«ã§ããããã«ãªã£ããï¼ã¨ãã話ã
ã¨ããã§ããã®ä¸ã®ã³ã¼ããªãã§ãããã³ã³ãã¤ã«ãããã¨ããã¨
ã¨ã©ã¼: MyClassã®åå¼æ°ãæ¨å®ã§ãã¾ããã
MyClassmyObject = new MyClass<>("");
çç±: ã³ã³ã¹ãã©ã¯ã¿ã®æ示çãªåãã©ã¡ã¼ã¿ã§ã¯'<>'ã使ç¨ã§ãã¾ãã
ã¨æããã¾ããä¸ã®ã³ã¼ãã ã¨éãã®ã«â¦ãå¾®å¦ãªãªããã¤ãã¦ãã¾ãã¾ããã
éå®ç¨çãªè©±
ãå¾ ãããã¾ãããã¹ã¼ãã¼å¤æ ã¿ã¤ã ã§ããããããã¾ãã¹ã¼ãã¼ãããªãã§ããã©ãã
å®ã¯å¤æ°ã¸ã®ä»£å ¥ãããªãã¦ããã¤ã¢ã¢ã³ãã»ãªãã¬ã¼ã¿ãã³ã³ãã¤ã«éããã¨ã¯ã§ãããã§ãããã
// newããã ã new ArrayList<>(); // newããã¤ã³ã¹ã¿ã³ã¹ãç´æ¥ä½¿ã new ArrayList<>().add("hoge"); // newããã¤ã³ã¹ã¿ã³ã¹ãå¼ä¸ã§ä½¿ã if (new ArrayList<>() == null) {} // instanceofæ¼ç®åã«ç´æ¥ãã¦ã if (new ArrayList<>() instanceof List){} // Objectåã¸ã®ä»£å ¥ Object o = new ArrayList<>(); // rawåã¸ã®ä»£å ¥ List l = new ArrayList<>(); // Objectåã®é åã®åæåå Object[] oarray = {new ArrayList<>()}; // rawåã®é åã®åæåå List[] larray = {new ArrayList<>()}; // 3é æ¼ç®å Objectåã®å ´å boolean b = true; Object o = b ? new ArrayList<>() : new ArrayList<>(); // 3é æ¼ç®å rawåã®å ´å List l2 = b ? new ArrayList<>() : new ArrayList<>();
ãã ã¾ãããããã®éãå®ç¨æ§ã¯ãªãã®ã§å¿ãã¦ããã§ããããã¤ã¢ã¢ã³ããªãã¬ã¼ã¿ã¯ä»£å ¥ã§ã®newã§ä½¿ãããã¨ããã·ã³ãã«ãªã«ã¼ã«ã§è¦ãã¦ããã»ããæ··ä¹±ããªãã¦ããã
ä¸è¨ã®ä¾ã¯éã¸ã§ããªãã¯ãªå¤æ°ã«ä»£å ¥ããã±ã¼ã¹ã§ãããã®ä»ãã¡ã½ããã®å¼æ°ã«æ¸¡ãå ´åã§ãéã¸ã§ããªãã¯ã§ããã°å©ç¨å¯è½ã§ããå®ç¨ä¸ããã¾ãæå³ã¯ãªãã§ããã
public void hoge0(Object o) {} public void hoge1(List l) {} public void hoge2(List<String> l) {} public void hoge() { hoge0(new ArrayList<>()); // OK hoge1(new ArrayList<>()); // OK hoge2(new ArrayList<>()); // NG }
ããã§ã¯çããã楽ããJava7ã©ã¤ããï¼