Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36756
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: How to parse an array? |
| Date | 2017-06-28 19:09 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <oj0uti$4q3$1@dont-email.me> (permalink) |
| References | (5 earlier) <642c0caf-de21-4add-8f61-8b68e94b1081@googlegroups.com> <oiudpv$imf$1@dont-email.me> <dc58b7e1-186b-472f-a088-f9b848481404@googlegroups.com> <oiumi8$6om$1@dont-email.me> <47fa7e7a-7757-46b0-a6bb-5084529a50e2@googlegroups.com> |
On Wed, 28 Jun 2017 05:10:16 -0700, Eric Douglas wrote:
> On Tuesday, June 27, 2017 at 6:38:53 PM UTC-4, Martin Gregorie wrote:
>> On Tue, 27 Jun 2017 14:01:20 -0700, Eric Douglas wrote:
>>
>> > Yes my table model extends AbstractTableModel. I have each cell
>> > value in a custom object that handles getting and setting values by
>> > String or numeric for different cell styles. The model data is in an
>> > ArrayList. Each element of the ArrayList is an array [] of that cell
>> > class.
>> >
>> Sounds good, though as the row is a fixed size I'm curious why it isn't
>> a class instead, since that, given a getter/setter for each data
>> element would seem to be functionally very similar.
>>
>>
> So the long part is the DefaultRowSorter, trying to build an array of
> cells from scratch every time you change the sort order of columns or
> the filter clause. So maybe I just need another level. My table cell
> class is Comparable. Now if I put them in a table row class that is
> Comparable then it can ask what order the columns go in and just sort
> themselves.
>
> Now, the searchable table holds an ArrayList, indices are view row
> numbers, values are model row numbers. This is where my question came
> in. Even if the rows can sort themselves, isn't it more efficient to
> make a set of rows that match the filter (view) before I sort them, and
> how best to grab those row elements from the table model array.
>
What you need is to change the ordering without reconstructing everything
and especially without moving any items stored in an array or ArrayList.
I hope the following clarifies one way of doing that.
If your rows are held in an ArayList (and it doesn't matter whether a row
is an object[] array or a single RowContent object) then, as you say, you
have to sort it when its loaded and:
- if you change the key in any row, you can either:
- find the row containing the original key
- remove it from the row ArrayList leaving a gap
- work out where to put it back in the arraylist
- move part of the array list up or down to fill the gap and leave
a gap where the new key should go
- put the modified row into the gap
OR
- change the key in situ
- re-sort the row list
OR (if you store all rows, regardless of order in an immutable
ArrayList array and use a TreeMap as the sorting mechanism)
- rowNo = TreeMap.get(oldKey);
- TreeMap.remove(oldKey);
- row = rowList.get(rowNo); row.setKeyVal(newKey);
- TreeMap.put(newKey, rowNo)
which will almost certainly be the fastest of the three, even if you
have to map an Iterator onto it for the JTable to use when refreshing
the display, because no matter what the key value changes are, they
only affect a very small part of the red-black tree used to implement
a TreeMap.
- if you switch ordering to another row in the table, you can either
- re-sort the entire table if your rows are in an ArrayList whose
order determines the display sequence
OR (if you store all rows, regardless of order in an immutable
ArrayList array and use a TreeMap as the sorting mechanism)
-treemap = new TreeMap();
- for all rows in the immutable array
- row = rowList.get(i);
- TreeMap.add(row.getKey(), i)
which will probably be faster than sorting an ArrayList since it will
probably do fewer key comparisons than a comparable sort and will not
have to re-order any ArrayLists.
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
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