2

I have an array list of a array list and I want to make a separate view of it that will filter certain things from it and store it somewhere for later usage. I am planing on using an array list of array list and was wondering if array list has a way to be connected together where the change in one reflects another ? Any thoughts ? Its like an excel sheet and I would like to filter out certain numbers and I will display another view of the same sheet without really changing the original sheet. please help.

4
  • What you're asking can certainly be done, but if you give more context as to what you're trying to do, I bet a better solution can be found. Any time you find yourself wanting a "collection of collections" you probably need a more well thought out approach. Commented Sep 9, 2011 at 5:19
  • okay so I have a table that contains a last name first name and age. And in that table I have ages 1 to 90 but I want only ages 8 below so this new "view" has to reflect that. Commented Sep 9, 2011 at 5:25
  • Can you give some text example? Commented Sep 9, 2011 at 5:27
  • okay so I have a set of just ages 1 through 20 and I want only 1 through 10. I say filter ages <= 10 and it will give me ages 10 less and I just need to store it somewhere where the changes I make to the original row will be reflected onto the filtered one. Commented Sep 9, 2011 at 5:30

2 Answers 2

1

Here is a complete, executable, demo for you. You should be able to compile and run as is. I didn't add comments or error checking, but it should do what you ask. Whether a lists of lists is the best thing for your current situation I don't know. The fact that your rows have both names AND ages makes me think that you don't want a list of lists, but rather a list of row objects of a particular class. (Alternatively you can use List<?> or List<Object> everywhere....)

Nevertheless, the following may be of interest to you. You might be able to use it, or it might scare you away from a list of lists approach. :)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * A ragged 2-D table of strings that supports "views" (arbitrary rows).
 *
 * WARNING: no error checking.  This is just a sketch.
 */
public class Table {

    private List<List<String>> data;
    private List<Integer> activeRows = new ArrayList<Integer>();

    public Table(List<List<String>> data) {
        this.data = data;
    }

    public void setActiveRows(List<Integer> activeRows) {
        this.activeRows = activeRows;
    }

    public void update(int row, int column, String value) {
        data.get(row).set(column, value);
    }

    public void show() {
        for (Integer row: activeRows) {
            System.out.println(data.get(row));
        }
    }

    /**
     * DEMO.  Normally we don't put main methods inside a class.  This is just a sketch.
     */
    public static void main(String[] args) {
        List<List<String>> a = Arrays.asList(
            Arrays.asList("abc", "def", "ghi"),
            Arrays.asList("ABC", "DEF", "GHI"),
            Arrays.asList("zzz", "yyy", "xxx"),
            Arrays.asList("dog", "cat", "rat")
        );

        // Make two tables that share the same data
        Table t1 = new Table(a);
        Table t2 = new Table(a);

        // t1 will have all four rows; t2 just two of them.
        t1.setActiveRows(Arrays.asList(0, 1, 2, 3));
        t2.setActiveRows(Arrays.asList(1, 3));

        // Show them
        t1.show();
        System.out.println();
        t2.show();
        System.out.println();

        // Now change part of t1 and show that this is reflected in t2.
        t1.update(1, 2, "NEWVALUE");

        // And show them again
        t1.show();
        System.out.println();
        t2.show();
        System.out.println();
    }
}

Output is:

[abc, def, ghi]
[ABC, DEF, GHI]
[zzz, yyy, xxx]
[dog, cat, rat]

[ABC, DEF, GHI]
[dog, cat, rat]

[abc, def, ghi]
[ABC, DEF, NEWVALUE]
[zzz, yyy, xxx]
[dog, cat, rat]

[ABC, DEF, NEWVALUE]
[dog, cat, rat]
Sign up to request clarification or add additional context in comments.

Comments

0

based on your explanation to the question, you can keep a sorted list.. and then do a sub set (explore navigable set and see how its implemented).. wrap you sub-set into a non-modifiable list or a new list whenever your are exposing it. You can also a sorted/ navigable map if you expect to encounter duplicates.. so the value in the map can be number of duplicates. Now when you query this map for its head or tail to return the view, you can use the number of duplicates to achieve the desired list.

    NavigableSet<Integer> orderedSet = new ConcurrentSkipListSet<Integer>(Arrays.asList(new Integer[] {11, 20, 32, 14,
            5})); // or tree set if thread safety is not a concern.
    orderedSet.headSet(20); // will give you integers upto 20 i.e. [5, 11, 14]

4 Comments

List<String> sub = list.subList(1, 4); something like that ?
yes, but here 1 and 4 are merely indexes so unless the list is sorted on the basis of age it wouldn't work. Also you should depending on your use case have a look at the behavior of the returned list; if you expose it to a user, how do you want it to behave if say the user adds some data to it. Wrap it into an unmodifiable list if you wish to expose only the read only view.
I dont understand what ur saying sorry.
edited answer, so if the ages are 11, 20, 32, 14 and 5 and you want to find everyone till the age of 20 you do a headSet(20) and you get that view of the set. Instead of Integers these numbers could be your real world object with a corresponding Comparable implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.