Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36686
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: How to parse an array? |
| Date | 2017-06-20 16:23 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <oic01n$rji$1@dont-email.me> (permalink) |
| References | <b36c14f6-dfe7-492e-99ab-7bb7aeb35ac3@googlegroups.com> |
On 6/20/2017 3:18 PM, Eric Douglas wrote:
> I'm writing a method to take 2 arrays and return the subset of the first array. Is there a more efficient way than creating a new array and iterating?
It would remove some few obstacles to understanding if you
were to stop treating `ArrayList' and `array' as synonyms. In
return, I will forswear using `4' to mean `for'. Deal?
> ArrayList<String> list1 = new ArrayList(); > list1.add("AA");
> list1.add("CC");
> list1.add("DD");
> list1.add("BB");
> list1.add("EE");
> ArrayList<Integer> list2 = new ArrayList();
> list2.add(1);
> list2.add(3);
> ArrayList<String> retval = new ArrayList();
`new ArrayList<>(list2.size())' might be a bit more efficient,
as it uses the minimum memory and avoids any expansions of too-short
intermediate lists.
> for (int i : list2) {
> retval.add(list1.get(i));
Aside: This might throw IndexOutOfBoundsException. If you
"know things" about the length of `list1' and the contents of
`list2', that might not be a concern.
> }
> Collections.sort(retval);
Aside: You could get a NullPointerException during the sort
if `list1' contains any nulls that are selected by the contents
of `list2'. Again, this may not be a concern if you have
sufficient prior knowledge.
> System.out.println(retval);
There are certainly other ways to write this -- as a stream
pipeline, for example. Whether the alternatives would be more or
less efficient is something you'd need to measure for yourself.
My guess (*no* hard evidence, mind you) is that "manual" methods
would win if `list2' is short, streams would win if it's Really
Long (because you could parallelize), and there'd probably be a
rather wide intermediate range of lengths where differences might
be difficult to measure.
And, of course, you could always use arrays instead.
--
esosman@comcast-dot-net.invalid
Thirteen hundred ten days to go.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to parse an array? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-20 12:18 -0700
Re: How to parse an array? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-06-20 14:49 -0500
Re: How to parse an array? Arne Vajhøj <arne@vajhoej.dk> - 2017-06-20 16:15 -0400
Re: How to parse an array? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-06-20 16:23 -0400
Re: How to parse an array? Robert Klemme <shortcutter@googlemail.com> - 2017-06-24 12:02 +0200
Re: How to parse an array? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-26 05:10 -0700
Re: How to parse an array? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-06-26 09:36 -0400
Re: How to parse an array? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-26 07:47 -0700
Re: How to parse an array? Martin Gregorie <martin@address-in-sig.invalid> - 2017-06-26 15:47 +0000
Re: How to parse an array? Arne Vajhøj <arne@vajhoej.dk> - 2017-06-26 20:37 -0400
Re: How to parse an array? lewbloch@gmail.com - 2017-06-27 12:29 -0700
Re: How to parse an array? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-27 12:50 -0700
Re: How to parse an array? Martin Gregorie <martin@address-in-sig.invalid> - 2017-06-27 20:05 +0000
Re: How to parse an array? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-27 14:01 -0700
Re: How to parse an array? Martin Gregorie <martin@address-in-sig.invalid> - 2017-06-27 22:34 +0000
Re: How to parse an array? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-28 05:10 -0700
Re: How to parse an array? Martin Gregorie <martin@address-in-sig.invalid> - 2017-06-28 19:09 +0000
Re: How to parse an array? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-28 13:02 -0700
Re: How to parse an array? Martin Gregorie <martin@address-in-sig.invalid> - 2017-06-28 21:55 +0000
Re: How to parse an array? Eric Douglas <e.d.programmer@gmail.com> - 2017-06-30 06:10 -0700
Re: How to parse an array? Martin Gregorie <martin@address-in-sig.invalid> - 2017-06-30 16:52 +0000
Re: How to parse an array? Jerry Stuckle <jstucklex@attglobal.net> - 2017-06-24 16:34 -0400
Re: How to parse an array? Martin Gregorie <martin@address-in-sig.invalid> - 2017-06-24 22:42 +0000
Re: How to parse an array? Jerry Stuckle <jstucklex@attglobal.net> - 2017-06-24 20:05 -0400
Re: How to parse an array? Robert Klemme <shortcutter@googlemail.com> - 2017-06-25 12:20 +0200
csiph-web