Path: csiph.com!feeder.erje.net!1.eu.feeder.erje.net!news.albasani.net!.POSTED!not-for-mail From: Sebastian Newsgroups: comp.lang.java.programmer Subject: Re: What's the best way to make a cross string set? Date: Mon, 31 Jul 2017 18:13:32 +0200 Organization: albasani.net Lines: 34 Message-ID: References: <6f9ff338-1944-4b64-83b8-4a921ce57a89@googlegroups.com> <1c3dafe0-c2ed-4ea1-9c06-b069f20c16ea@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net 3t8w3VfJkr9+16vdnWU3KEb4OpQx1R0mm4qMQCq65EHurTggYhcmzWTHoYGprgJkOmxkEr4N0KX1fgjPzhMc5Q== NNTP-Posting-Date: Mon, 31 Jul 2017 16:13:32 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="py3F6q7CEfJitfU+wLqFcR+pJAjnBluPaROKddZfVmOS0j2AaY5eZ2p3a0u8+36mDBaT25HthCZaTgpQSR5RG4KhEVSW7O+XJqPwVikJOvMPYwV2w+QvPU4ZfVnGuQXD"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 In-Reply-To: <1c3dafe0-c2ed-4ea1-9c06-b069f20c16ea@googlegroups.com> Cancel-Lock: sha1:6+3y6V9iWDGWopcD8drRanghDQw= Xref: csiph.com comp.lang.java.programmer:37035 Am 31.07.2017 um 14:24 schrieb Eric Douglas: > On Friday, July 28, 2017 at 5:21:09 PM UTC-4, Eric Sosman wrote: >> ArrayList list1 = new ArrayList() >> should have been something like >> List> list1 = new ArrayList<>(); >> or >> List> list1 = new ArrayList<>(); >> or even >> List> list1 = new ArrayList<>(); >> and >> ArrayList list2 = new ArrayList() >> should have been >> List list2 = new ArrayList<>(); >> or maybe >> Collection list2 = new ArrayList<>(); >> ... and, of course, other variations are possible. >> > > You're saying declaring a variable as a List is more efficient even if you know it's going to be instantiated as ArrayList? I've always tried to avoid that because it's one more line in the import section, and I never use * imports because that just sounds confusing at best messy at worst when you get into the same class names in multiple packages. > I think the point Eric made was not about efficiency, but about better type-safety and readability. That's what you can accomplish with generics: it's kind of nice when both you and the compiler know up front you're dealing with a list of lists of strings. Efficiency does not enter here. Whether it is better to program against ArrayList or List is more of an API consideration. It's not important in this short code snippet, I think. It would depend on whether you want the consumers of your API to know they can rely on properties of ArrayList that List implementations in general do not have, such as constant time random access. But that is a bit beside the point here, it would be an entirely different topic. -- Sebastian