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.
-
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.corsiKa– corsiKa2011-09-09 05:19:27 +00:00Commented 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.Ashley– Ashley2011-09-09 05:25:30 +00:00Commented Sep 9, 2011 at 5:25
-
Can you give some text example?Pankaj Kumar– Pankaj Kumar2011-09-09 05:27:46 +00:00Commented 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.Ashley– Ashley2011-09-09 05:30:23 +00:00Commented Sep 9, 2011 at 5:30
2 Answers
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]
Comments
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]