{"id":10,"date":"2005-10-02T15:12:19","date_gmt":"2005-10-02T20:12:19","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/?p=10"},"modified":"2005-10-02T15:12:19","modified_gmt":"2005-10-02T20:12:19","slug":"generic-java","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2005\/10\/02\/generic-java\/","title":{"rendered":"Generic Java"},"content":{"rendered":"<p>I have been using Java 1.5, or as Sun calls it Java 5.0, with Eclipse 3.1.0.  One of the kewl new features of Java 1.5 is generics (which C++ has had for over ten years).  If you use generics with a list you don&#8217;t have to keep casting to an expected object.  When you cast you pretty much are guessing about the types contained in a collection.  If you use generics you can guarantee the type of in a list or hash map, in fact it is used by the compiler for type checking.  To get started lets create a list using generics:<\/p>\n<pre>\nList&lt;String&gt; myList = new ArrayList&lt;String&gt;();\n\/\/ fill my list with string objects\nfor(int i=0; i &lt; myList.size(); i++)  {\n\tString item = myList.get(i);\n\tSystem.out.println(item);\n}\n<\/pre>\n<p>See.  No cast required.  Here is the same snippet using the new for\/in loop to mix things up a bit.<\/p>\n<pre>\nList&lt;String&gt; myList = new ArrayList&lt;String&gt;();\n\/\/ fill my list with strings objects...\nfor(String item : myList)  {\n\tSystem.out.println(item);\n}\n<\/pre>\n<p>To illustrate Java generics with another example, here is a snippet of code that uses a map instead of a list.<\/p>\n<pre>\nMap&lt;String, String&gt; myMap = new HashMap&lt;String, String&gt;();\n\/\/ fill my map with string key and value objects...\nIterator&lt;String&gt; i = myMap.keySet().iterator()\nwhile(i.hasNext)\n\tString key = i.next();\n<\/pre>\n<p>Again, the benefits of using Java generics is compile type checking so that you won&#8217;t be surprised with a ClassCastException at runtime during an important demo.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been using Java 1.5, or as Sun calls it Java 5.0, with Eclipse 3.1.0. One of the kewl new features of Java 1.5 is generics (which C++ has had for over ten years). If you use generics with a list you don&#8217;t have to keep casting to an expected object. When you cast [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[15,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-a","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/10"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}