Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #37006 > unrolled thread

What's the best way to make a cross string set?

Started byEric Douglas <e.d.programmer@gmail.com>
First post2017-07-27 12:12 -0700
Last post2017-07-29 23:56 +0200
Articles 20 on this page of 41 — 9 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-27 12:12 -0700
    Re: What's the best way to make a cross string set? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-07-27 14:38 -0500
      Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-27 12:49 -0700
    Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-27 15:49 -0400
      Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-27 12:55 -0700
      Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-27 13:22 -0700
        Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-27 13:30 -0700
        Re: What's the best way to make a cross string set? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-07-27 15:34 -0500
        Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-27 18:27 -0400
          Re: What's the best way to make a cross string set? lewbloch@gmail.com - 2017-07-28 18:51 -0700
      Re: What's the best way to make a cross string set? v_borchert@despammed.com (Volker Borchert) - 2017-07-29 06:03 +0000
    Re: What's the best way to make a cross string set? v_borchert@despammed.com (Volker Borchert) - 2017-07-28 18:19 +0000
      Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-28 13:28 -0700
        Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-28 17:20 -0400
          Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-31 05:24 -0700
            Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-31 12:01 -0400
              Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-31 10:58 -0700
            Re: What's the best way to make a cross string set? Sebastian <nowhere@nirvana.org> - 2017-07-31 18:13 +0200
            Re: What's the best way to make a cross string set? lewbloch@gmail.com - 2017-07-31 09:24 -0700
            Re: What's the best way to make a cross string set? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-31 19:24 -0400
          Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-01 06:36 -0700
            Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-01 10:58 -0400
              Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-01 08:09 -0700
                Re: What's the best way to make a cross string set? lewbloch@gmail.com - 2017-08-01 09:39 -0700
                  Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-01 10:18 -0700
                    Re: What's the best way to make a cross string set? Andreas Leitgeb <avl@logic.at> - 2017-08-02 09:22 +0000
                      Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-02 05:13 -0700
                        Re: What's the best way to make a cross string set? Andreas Leitgeb <avl@logic.at> - 2017-08-03 14:33 +0000
                          Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-03 08:51 -0700
                            Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-03 12:20 -0400
                              Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-03 10:15 -0700
                                Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-03 13:42 -0400
                                  Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-03 11:17 -0700
                                    Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-05 16:08 -0400
                                      Re: What's the best way to make a cross string set? Martin Gregorie <martin@address-in-sig.invalid> - 2017-08-05 20:31 +0000
                                        Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-08-05 17:25 -0400
                                          Re: What's the best way to make a cross string set? Martin Gregorie <martin@address-in-sig.invalid> - 2017-08-05 22:11 +0000
                                            Re: What's the best way to make a cross string set? Sebastian <nowhere@nirvana.org> - 2017-08-06 18:13 +0200
                  Re: What's the best way to make a cross string set? Eric Douglas <e.d.programmer@gmail.com> - 2017-08-02 12:56 -0700
    Re: What's the best way to make a cross string set? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-29 16:55 -0400
    Re: What's the best way to make a cross string set? Sebastian <nowhere@nirvana.org> - 2017-07-29 23:56 +0200

Page 1 of 3  [1] 2 3  Next page →


#37006 — What's the best way to make a cross string set?

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-07-27 12:12 -0700
SubjectWhat's the best way to make a cross string set?
Message-ID<6f9ff338-1944-4b64-83b8-4a921ce57a89@googlegroups.com>
I have a variable number of lists of values, as in
ArrayList list1 = new ArrayList()
ArrayList list2 = new ArrayList()
list2.add("a")
list2.add("b")
list2.add("c")
list1.add(list2)
ArrayList list3 = new ArrayList()
list3.add("1")
list3.add("2")
list3.add("3")
list1.add(list3)
ArrayList list4 = new ArrayList()
list4.add("fg")
list4.add("hj")
list4.add("kl")
list1.add(list4)

Now I want to print only the complete set, as in
a1fg
b1fg
c1fg
a2fg
b2fg
c2fg
a3fg
b3fg
c3fg
a1hj
b1hj
...

[toc] | [next] | [standalone]


#37007

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2017-07-27 14:38 -0500
Message-ID<oldf7j$t6b$1@dont-email.me>
In reply to#37006
On 7/27/2017 14:12, Eric Douglas wrote:
> I have a variable number of lists of values, as in
> ArrayList list1 = new ArrayList()
> ArrayList list2 = new ArrayList()
> list2.add("a")
> list2.add("b")
> list2.add("c")
> list1.add(list2)
> ArrayList list3 = new ArrayList()
> list3.add("1")
> list3.add("2")
> list3.add("3")
> list1.add(list3)
> ArrayList list4 = new ArrayList()
> list4.add("fg")
> list4.add("hj")
> list4.add("kl")
> list1.add(list4)
> 
> Now I want to print only the complete set, as in
> a1fg
> b1fg
> c1fg
> a2fg
> b2fg
> c2fg
> a3fg
> b3fg
> c3fg
> a1hj
> b1hj
> ...
> 

import java.util.*;

public class test0 {
     public static void main(String... args) {
         List<String> list1 = Arrays.asList("a","b","c");
         List<String> list2 = Arrays.asList("1","2","3");
         List<String> list3 = Arrays.asList("fg","hj","kl");

         for (String str3 : list3) {
             for (String str2 : list2) {
                 for (String str1 : list1) {
                     System.out.println(str1.concat(str2.concat(str3)));
                 }
             }
         }
     }
}

C:\Users\Knute Johnson>java test0
a1fg
b1fg
c1fg
a2fg
b2fg
c2fg
a3fg
b3fg
c3fg
a1hj
b1hj
c1hj
a2hj
b2hj
c2hj
a3hj
b3hj
c3hj
a1kl
b1kl
c1kl
a2kl
b2kl
c2kl
a3kl
b3kl
c3kl



-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#37008

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-07-27 12:49 -0700
Message-ID<79c4db8d-4a5f-4ae4-9c5f-405a6d2106fa@googlegroups.com>
In reply to#37007
On Thursday, July 27, 2017 at 3:38:32 PM UTC-4, Knute Johnson wrote:
> import java.util.*;
> 
> public class test0 {
>      public static void main(String... args) {
>          List<String> list1 = Arrays.asList("a","b","c");
>          List<String> list2 = Arrays.asList("1","2","3");
>          List<String> list3 = Arrays.asList("fg","hj","kl");
> 
>          for (String str3 : list3) {
>              for (String str2 : list2) {
>                  for (String str1 : list1) {
>                      System.out.println(str1.concat(str2.concat(str3)));
>                  }
>              }
>          }
>      }
> }

That is cheating, to hard code list3, 2, 1.  I need to work with the first container.  Perhaps it wasn't clear when I mentioned that list1 in my sample contains a variable number of lists.

[toc] | [prev] | [next] | [standalone]


#37009

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2017-07-27 15:49 -0400
Message-ID<oldftj$vac$1@dont-email.me>
In reply to#37006
On 7/27/2017 3:12 PM, Eric Douglas wrote:
> I have a variable number of lists of values, as in
> ArrayList list1 = new ArrayList()
> ArrayList list2 = new ArrayList()
> list2.add("a")
> list2.add("b")
> list2.add("c")
> list1.add(list2)
> ArrayList list3 = new ArrayList()
> list3.add("1")
> list3.add("2")
> list3.add("3")
> list1.add(list3)
> ArrayList list4 = new ArrayList()
> list4.add("fg")
> list4.add("hj")
> list4.add("kl")
> list1.add(list4)
> 
> Now I want to print only the complete set, as in
> a1fg
> b1fg
> c1fg
> a2fg
> b2fg
> c2fg
> a3fg
> b3fg
> c3fg
> a1hj
> b1hj
> ...

     If by "complete set" you mean "the Cartesian product of all the
lists contained in list1," where list1 might hold N embedded lists
(more generally, Collections) instead of just three, I'd suggest a
recursive solution along the lines of:

	void tuples(List bigList, int which, String soFar) {
	    if (which < bigList.size()) {
	       for (Object item : bigList.get(which)) {
	           tuples(bigList, which + 1, soFar + item);
	       }
	    } else {
	        System.out.println(soFar);
	    }
	}
	...
	tuples(list1, 0, "");

     If that's not what you want, please explain "complete set"
more fully.

-- 
esosman@comcast-dot-net.invalid
Twelve hundred seventy-three days to go.

[toc] | [prev] | [next] | [standalone]


#37010

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-07-27 12:55 -0700
Message-ID<e887e39e-d5b3-4226-86c7-82d2cccf031d@googlegroups.com>
In reply to#37009
On Thursday, July 27, 2017 at 3:50:08 PM UTC-4, Eric Sosman wrote:
>      If by "complete set" you mean "the Cartesian product of all the
> lists contained in list1," where list1 might hold N embedded lists
> (more generally, Collections) instead of just three, I'd suggest a
> recursive solution along the lines of:
> 

Exactly.  Complete meaning I don't need the product of list1 + list2 when there are 3 lists, just all possible strings generated from combining all values from all lists.  Yes, 3 was just a sample showing what the end result could look like, but the goal is a variable number of lists each containing a variable number of Strings.

[toc] | [prev] | [next] | [standalone]


#37011

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-07-27 13:22 -0700
Message-ID<95687d57-46ef-47d8-84de-505e79d67784@googlegroups.com>
In reply to#37009
On Thursday, July 27, 2017 at 3:50:08 PM UTC-4, Eric Sosman wrote:
> recursive solution along the lines of:
> 
> 	void tuples(List bigList, int which, String soFar) {
> 	    if (which < bigList.size()) {
> 	       for (Object item : bigList.get(which)) {
> 	           tuples(bigList, which + 1, soFar + item);
> 	       }
> 	    } else {
> 	        System.out.println(soFar);
> 	    }
> 	}
> 	...
> 	tuples(list1, 0, "");
> 

That doesn't make sense.  As is it doesn't compile, just says bigList.get(which) needs to return an Iterable.  I've tried modifying it but I'm not sure how it's supposed to work.

[toc] | [prev] | [next] | [standalone]


#37012

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-07-27 13:30 -0700
Message-ID<76caff5e-7431-4c99-86ee-6eb25efee42c@googlegroups.com>
In reply to#37011
On Thursday, July 27, 2017 at 4:22:51 PM UTC-4, Eric Douglas wrote:
> On Thursday, July 27, 2017 at 3:50:08 PM UTC-4, Eric Sosman wrote:
> > recursive solution along the lines of:

Nevermind I think that one works, once I update the qualifiers.

          List<List<String>> list1 = new ArrayList();
          ArrayList list2 = new ArrayList();
          list2.add("a");
          list2.add("b");
          list2.add("c");
          list1.add(list2);
          ArrayList list3 = new ArrayList();
          list3.add("1");
          list3.add("2");
          list3.add("3");
          list1.add(list3);
          ArrayList list4 = new ArrayList();
          list4.add("fg");
          list4.add("hj");
          list4.add("kl");
          list1.add(list4);
          tuples(list1, 0, "");

     public static void tuples(List<List<String>> bigList, int which, String soFar) {
          if (which < bigList.size()) {
               for (String item : bigList.get(which)) {
                    tuples(bigList, which + 1, soFar + item);
               }
          } else {
               System.out.println(soFar);
          }
     }

Output:
a1fg
a1hj
a1kl
a2fg
a2hj
a2kl
a3fg
a3hj
a3kl
b1fg
b1hj
b1kl
b2fg
b2hj
b2kl
b3fg
b3hj
b3kl
c1fg
c1hj
c1kl
c2fg
c2hj
c2kl
c3fg
c3hj
c3kl

[toc] | [prev] | [next] | [standalone]


#37013

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2017-07-27 15:34 -0500
Message-ID<oldihc$874$1@dont-email.me>
In reply to#37011
On 7/27/2017 15:22, Eric Douglas wrote:
> On Thursday, July 27, 2017 at 3:50:08 PM UTC-4, Eric Sosman wrote:
>> recursive solution along the lines of:
>>
>> 	void tuples(List bigList, int which, String soFar) {
>> 	    if (which < bigList.size()) {
>> 	       for (Object item : bigList.get(which)) {
>> 	           tuples(bigList, which + 1, soFar + item);
>> 	       }
>> 	    } else {
>> 	        System.out.println(soFar);
>> 	    }
>> 	}
>> 	...
>> 	tuples(list1, 0, "");
>>
> 
> That doesn't make sense.  As is it doesn't compile, just says bigList.get(which) needs to return an Iterable.  I've tried modifying it but I'm not sure how it's supposed to work.
> 

import java.util.*;

public class test0 {
     public static void main(String... args) {
         List<String> list1 = Arrays.asList("a","b","c","d");
         List<String> list2 = Arrays.asList("1","2","3");
         List<String> list3 = Arrays.asList("fg","hj","kl");
         List<String> list4 = Arrays.asList("x","y");
         List<List<String>> list0 = new ArrayList<>();
         list0.add(list1);
         list0.add(list2);
         list0.add(list3);
         list0.add(list4);

         ArrayList<String> result = new ArrayList<>();
         GeneratePermutations(list0,result,0,"");

         System.out.println(result);
     }

     /* with a nod to StackOverflow */
     static void GeneratePermutations(List<List<String>>list,
      List<String> result,int depth, String current) {
         if (depth == list.size()) {
             result.add(current);
             return;
         }
         for (int i=0; i<list.get(depth).size(); i++) {
             GeneratePermutations(list,result,depth + 1,
              current + list.get(depth).get(i));
         }
     }
}



-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#37014

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2017-07-27 18:27 -0400
Message-ID<oldp5k$viq$1@dont-email.me>
In reply to#37011
On 7/27/2017 4:22 PM, Eric Douglas wrote:
> On Thursday, July 27, 2017 at 3:50:08 PM UTC-4, Eric Sosman wrote:
>> recursive solution along the lines of:
>>
>> 	void tuples(List bigList, int which, String soFar) {
>> 	    if (which < bigList.size()) {
>> 	       for (Object item : bigList.get(which)) {
>> 	           tuples(bigList, which + 1, soFar + item);
>> 	       }
>> 	    } else {
>> 	        System.out.println(soFar);
>> 	    }
>> 	}
>> 	...
>> 	tuples(list1, 0, "");
>>
> 
> That doesn't make sense.  As is it doesn't compile, just says bigList.get(which) needs to return an Iterable.  I've tried modifying it but I'm not sure how it's supposed to work.

     Sorry about that.

	for (Object item : (Iterable) bigList.get(which)) {
	...

(If you'd used generics, this would never have happened.)

-- 
esosman@comcast-dot-net.invalid
Twelve hundred seventy-three days to go.

[toc] | [prev] | [next] | [standalone]


#37020

Fromlewbloch@gmail.com
Date2017-07-28 18:51 -0700
Message-ID<8e0dcabc-5929-4a68-a9df-6996d439c10a@googlegroups.com>
In reply to#37014
Never use raw types.

[toc] | [prev] | [next] | [standalone]


#37021

Fromv_borchert@despammed.com (Volker Borchert)
Date2017-07-29 06:03 +0000
Message-ID<olh8eo$iom$1@Gaia.teknon.de>
In reply to#37009
Eric Sosman wrote:
>      If by "complete set" you mean "the Cartesian product of all the
> lists contained in list1," where list1 might hold N embedded lists
> (more generally, Collections) instead of just three, I'd suggest a
> recursive solution along the lines of:
> 
>         void tuples(List bigList, int which, String soFar) {
>             if (which < bigList.size()) {
>                for (Object item : bigList.get(which)) {
>                    tuples(bigList, which + 1, soFar + item);
>                }
>             } else {
>                 System.out.println(soFar);
>             }
>         }
>         ...
>         tuples(list1, 0, "");

I'd reuse a single StringBuilder.

-- 

"I'm a doctor, not a mechanic." Dr Leonard McCoy <mccoy@ncc1701.starfleet.fed>
"I'm a mechanic, not a doctor." Volker Borchert  <v_borchert@despammed.com>

[toc] | [prev] | [next] | [standalone]


#37016

Fromv_borchert@despammed.com (Volker Borchert)
Date2017-07-28 18:19 +0000
Message-ID<olfv7c$8l3$3@Gaia.teknon.de>
In reply to#37006
Eric Douglas wrote:
> I have a variable number of lists of values, as in
> ArrayList list1 = new ArrayList()
> ArrayList list2 = new ArrayList()
> list2.add("a")
> list2.add("b")
> list2.add("c")
> list1.add(list2)
> ArrayList list3 = new ArrayList()
> list3.add("1")
> list3.add("2")
> list3.add("3")
> list1.add(list3)
> ArrayList list4 = new ArrayList()
> list4.add("fg")
> list4.add("hj")
> list4.add("kl")
> list1.add(list4)
> 
> Now I want to print only the complete set, as in
> a1fg
> b1fg
> c1fg
> a2fg
> b2fg
> c2fg
> a3fg
> b3fg
> c3fg
> a1hj
> b1hj
> ....

This sounds suspiciously like homework. Please do it yourself.

-- 

"I'm a doctor, not a mechanic." Dr Leonard McCoy <mccoy@ncc1701.starfleet.fed>
"I'm a mechanic, not a doctor." Volker Borchert  <v_borchert@despammed.com>

[toc] | [prev] | [next] | [standalone]


#37017

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-07-28 13:28 -0700
Message-ID<d4c8c823-5603-4b80-be33-629d8d307faf@googlegroups.com>
In reply to#37016
On Friday, July 28, 2017 at 3:09:01 PM UTC-4, Volker Borchert wrote:
> 
> This sounds suspiciously like homework. Please do it yourself.
> 

More like work work and I did spend a few hours trying to think of more efficient ways.  I did come up with other ways to arrive at the result, just trying to clean it up.  You don't want to see how the original code did it..

[toc] | [prev] | [next] | [standalone]


#37018

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2017-07-28 17:20 -0400
Message-ID<olg9jv$8ct$1@dont-email.me>
In reply to#37017
On 7/28/2017 4:28 PM, Eric Douglas wrote:
> On Friday, July 28, 2017 at 3:09:01 PM UTC-4, Volker Borchert wrote:
>>
>> This sounds suspiciously like homework. Please do it yourself.
>>
> 
> More like work work and I did spend a few hours trying to think of more efficient ways.  I did come up with other ways to arrive at the result, just trying to clean it up.  You don't want to see how the original code did it..

     If it's work work, you *really* should be using generics.
Raw types are still supported in Java, yes -- for compatibility
with existing code from more than thirteen years ago.  New code,
even ten-year-old code, should not be using them except in dire
dire dire circumstances.  So:

	ArrayList list1 = new ArrayList()
should have been something like
	List<List<String>> list1 = new ArrayList<>();
or
	List<Collection<String>> list1 = new ArrayList<>();
or even
	List<Iterable<String>> list1 = new ArrayList<>();
and
	ArrayList list2 = new ArrayList()
should have been
	List<String> list2 = new ArrayList<>();
or maybe
	Collection<String> list2 = new ArrayList<>();
... and, of course, other variations are possible.

-- 
esosman@comcast-dot-net.invalid
Twelve hundred seventy-two days to go.

[toc] | [prev] | [next] | [standalone]


#37033

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-07-31 05:24 -0700
Message-ID<1c3dafe0-c2ed-4ea1-9c06-b069f20c16ea@googlegroups.com>
In reply to#37018
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<List<String>> list1 = new ArrayList<>();
> or
> 	List<Collection<String>> list1 = new ArrayList<>();
> or even
> 	List<Iterable<String>> list1 = new ArrayList<>();
> and
> 	ArrayList list2 = new ArrayList()
> should have been
> 	List<String> list2 = new ArrayList<>();
> or maybe
> 	Collection<String> 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.

[toc] | [prev] | [next] | [standalone]


#37034

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2017-07-31 12:01 -0400
Message-ID<olnk1i$sc7$1@dont-email.me>
In reply to#37033
On 7/31/2017 8:24 AM, Eric Douglas wrote:
> 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<List<String>> list1 = new ArrayList<>();
>> or
>> 	List<Collection<String>> list1 = new ArrayList<>();
>> or even
>> 	List<Iterable<String>> list1 = new ArrayList<>();
>> and
>> 	ArrayList list2 = new ArrayList()
>> should have been
>> 	List<String> list2 = new ArrayList<>();
>> or maybe
>> 	Collection<String> 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?

     No, I did not say that, nor anything remotely like it.  I won't
re-hash the pros and cons of declaring interface types vs. concrete
types, as they are probably covered in the instructional materials
you skimmed too hastily.  (I've mentioned before that you exhibit a
strange combination of sophistication and ignorance, and I surmise
that your extensive experience with other programming languages may
have led you to short-change your Java studies and jump right in --
and find yourself in over your head.)

     What I *did* say is that it is *better* (not "more efficient") to
declare List<String> (or ArrayList<String>, if you prefer) than raw
ArrayList.  If you do not know why, go back to the books.

> 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.

     The pros and cons of interface vs. concrete declaration are, as
I said, well-known to anyone who has given Java even a moderate
degree of attention.  Go thou, and so do.

-- 
esosman@comcast-dot-net.invalid
Twelve hundred sixty-nine days to go.

[toc] | [prev] | [next] | [standalone]


#37037

FromEric Douglas <e.d.programmer@gmail.com>
Date2017-07-31 10:58 -0700
Message-ID<cbfee87f-801a-428b-8682-1d1065be472a@googlegroups.com>
In reply to#37034
On Monday, July 31, 2017 at 12:02:02 PM UTC-4, Eric Sosman wrote:
> >> 	ArrayList list1 = new ArrayList()
> >> should have been something like
> >> 	List<List<String>> list1 = new ArrayList<>();

Oh, right then.  I was confused by this ^^ as you were saying to add the <List<String>> where I thought you were saying should be declaring list1 as List instead of ArrayList when we know it will only ever be an ArrayList.

When I pasted in your code as is, Eclipse didn't mention that it was missing the <List<String>> because that is just a warning (which I have turned off).  It just flagged the bigList.get(which) because if you don't add the type parameters it marks that as an error. (must return iterable, because it does, but it didn't know that)

Of course I normally add the type parameters.  I don't recall if I turned off the warning because I actually have code written in raw that doesn't work if you add the type parameters, or just because I have a lot of old code that needs updated if I leave that warning on.

Half of what I write is in another language, built on top of Java, so it accepts Java classes and has it's own objects with Java-like syntax which doesn't have a type parameter.
https://drive.google.com/file/d/0ByERNUtEl6-fM2ExZmRkMWMtYjU4OC00NGVjLWI0NTYtMWI1NzkxNDIyYjI4/edit

[toc] | [prev] | [next] | [standalone]


#37035

FromSebastian <nowhere@nirvana.org>
Date2017-07-31 18:13 +0200
Message-ID<olnkvc$kls$1@news.albasani.net>
In reply to#37033
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<List<String>> list1 = new ArrayList<>();
>> or
>> 	List<Collection<String>> list1 = new ArrayList<>();
>> or even
>> 	List<Iterable<String>> list1 = new ArrayList<>();
>> and
>> 	ArrayList list2 = new ArrayList()
>> should have been
>> 	List<String> list2 = new ArrayList<>();
>> or maybe
>> 	Collection<String> 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

[toc] | [prev] | [next] | [standalone]


#37036

Fromlewbloch@gmail.com
Date2017-07-31 09:24 -0700
Message-ID<89b36f28-bd9e-4130-9485-6cd58377a206@googlegroups.com>
In reply to#37033
On Monday, July 31, 2017 at 5:25:19 AM UTC-7, Eric Douglas wrote:
> 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.

Why do you care about adding another line to the `import` block? If you care about that, just use fully-qualified names (FQNs), but really, there's no engineering reason to avoid imports.

Anyway, "efficiency", whatever you think that means, has nothing to do with it. It's about coding to the interface, a best practice. It's researchable.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#37039

FromArne Vajhøj <arne@vajhoej.dk>
Date2017-07-31 19:24 -0400
Message-ID<oloe7f$qbm$1@gioia.aioe.org>
In reply to#37033
On 7/31/2017 8:24 AM, Eric Douglas wrote:
> 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.

List<X> lst = new ArrayList<X>();

is often considered better than:

ArrayList<X> lst = new ArrayList<X>();

The import cost nothing.

And by using the interface List<> then it is possible to change
the actual type later to a different List<> implementation without
worrying about code using methods only available in ArrayList<>.

Reference:

Effective Java 2nd Ed / Bloch Item 52.

The question comes up frequently.

SO examples:

https://stackoverflow.com/questions/2279030/type-list-vs-type-arraylist-in-java
https://stackoverflow.com/questions/9852831/polymorphism-why-use-list-list-new-arraylist-instead-of-arraylist-list-n
https://stackoverflow.com/questions/18329311/reason-for-list-list-new-arraylist

Arne


[toc] | [prev] | [next] | [standalone]


Page 1 of 3  [1] 2 3  Next page →

Back to top | Article view | comp.lang.java.programmer


csiph-web