开发者

Connect multiple arraylist to one single array list

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 开发者_StackOverflow中文版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.


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]


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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜