what is an efficient way to get arraylist of arraylist? - java

I have this string:
string1="A.1,B.2,C.4"
I want to get the following arraylist of arraylist:
<<"A","1">,<"B","2">,<"c","4">>
is there any way other than using for loop?
suppose that arraylists are unique, so I would have
Set<String> set= new HashSet<>(Arrays.asList(string1.split(",")));
now I want to split each element in the above set on ., without using for loop.

I was thinking of first splitting based on ,. Then, split each of those on ., put both values in a list, and then put that list in another list. Something pseudo-code-y that should work. :)
string1="A.1,B.2,C.4"
stringsWithDots[] = string1.split(",");
List<List<String>> result = new ArrayList<List<String>>();
for(String stringWithDots: stringsWithDots) {
finalSplit[] = stringWithDots.split(".");
List<String> list1 = Arrays.asList(finalSplit);
result.add(list1);
}
"A.1,B.2,C.4" would look like [[A,1],[B,2],[C,4]]
Edit.
[asList source]
[split] which is used to split a string [has a loop].
ArrayList is backed by an array. The asList function simply sets a reference to that array [source].
So, that thing you say about not using a loop. Well, be it using stream or some internal function, loops happen; just that you might not be seeing it in the immediate code that you write.

Related

how do i print an element from a 2 dimension array-list?

The last command should print "printcat" but it does not. Why is that?
ArrayList<String[]> outerArr = new ArrayList<String[]>();
String[] myString1= {"hey","hey","hey","hey"};
outerArr .add(myString1);
String[] myString2= {"you","printcat","you","you"};
outerArr .add(myString2);
System.out.println(Arrays.toString(outerArr.get(1)));
System.out.println(outerArr.get(1).get(1));
I don't think it is a two dimensional Array-List. It's Just an Array-List which contains arrays of Strings as data.
You can use System.out.println(outerArr.get(1)[1]); to get the result.
outerArr.get(1) will return String[], which is {"you","printcat","you","you"}, and then you can use outerArr.get(1)[1], which will return the "printcat" element at the 1st index.
List, ArrayList and array ([]) are all different things (though the first two are related, and they all are kind of similar things). if you want something in 2D, it means you use something 2 times. for example a 2D List would look like List<List<String>> and a 2D array would look like String[][]. what you have is a list of arrays, which is not a 2D array, nor a 2D list. what you most likely want is List<List<String>> outerArr = new ArrayList<List<String>>();. The rest you should figure out yourself.
I finally figured it out! Because the Arraylist exists of Array you must handle the Array as a normal Array. So this works fine.
System.out.println(outerArr.get(1)[1]);

What would be the most efficient and fastest way to remove an element from a nested ArrayList?

I have an ArrayList of ArrayLists implemented as:
ArrayList<ArrayList<Integer>> sampleList = new ArrayList<ArrayList<Integer>>();
Supposing that my list, after some operations, contains the following elements:
[[1,2,3],[2,1,3,4],[3,1,2],[4,2]]
I want to remove all occurrences of a particular element, say 4, from this collection, i.e., I want to obtain the following output after the removal of 4:
[[1,2,3],[2,1,3],[3,1,2]]
I know I can use a for loop, but that would be too tedious and inefficient in cases of a really large set.
So is there a better method to do this? (I'm new to programming)
Given your stated requirements, there's not going to be a better solution. You'll need to be a bit careful to avoid removing the value 4 instead of the element at position 4:
Set<String> toRemove = Collections.singleton(4);
for (List<Integer> list : sampleList) {
list.removeAll(toRemove);
}

How to make subsets for an item in a list elements combining the other items from the list?

Assume I have an ArrayList of strings like [a, b, c, d, ....]. Can anybody help me with a sample code that how can I come out with a result that contains all possible power subsets form this list which are including a particular string from that list(except the single and empty subset)?
For example: if I like to get all the power subsets including a from the example list then the output will be:
[a,b], [a,c], [a,d], [a,b,c], [a,b,d], [a,c,d] without the empty and single subset([a])
Similarly if I want for b then the output will be:
[b,a], [b,c], [b,d], [b,a,c], [b,a,d], [b,c,d] without the empty and single subset([b])
As all of the items in the example list are string then their might be a memory problem when the subsets will be too rich. Because I need to keep this subsets in memory for a single string at a time. Like when making subsets for a then I need those subset for some further processing and then delete them, then for b and so on. So I also need help about what would be the optimized solution for this scenario?
I need the help in Java. As I am not that much good at Java please pardon me if I made any mistake!
If there's always only one specific element, I'd suggest something like removing the target element from the source set, using Guava's Sets.powerSet on the remaining set, and then adding the target element to the returned set. Something like...
Set<String> elems = Sets.newHashSet(set);
elems.remove(target);
Set<Set<String>> powerSet = Sets.powerSet(elems);
Collection<Set<String>> subsetsWithTarget = Collections2.transform(
powerSet, new Function<Set<String>, Set<String>>() {
public Set<String> apply(Set<String> setWithoutTarget) {
return Sets.union(setWithoutTarget, Collections.singleton(target));
}
});
(Disclosure: I contribute to Guava.)

Splitting string[] to ArrayList and Returning Values

I'm having an issue splitting a string array into a List/ArrayList. I realise this is basic stuff but I've looked at so many examples that I've now completely confused myself at what's happening (or not happening).
Some code snippets:
private String[] stringTempList;
private ArrayList<List<String>> arrayImageList = new ArrayList<List<String>>();
My list is read from a webpage and is formatted in plain text like: ['One','Two','Three', etc ...]
So, some lines to strip out the stuff I want/don't want (note - I've separated these out to help me follow the process through):
stringExtractedList = stringText.substring(stringText.indexOf("['") + 2,
stringText.lastIndexOf("']"));
stringTempList = stringExtractedList.split("','");
From what I can see the above works as expected (creating an array (stringTempList), splitting out each item where it sees ','.
Where it's going wrong:
arrayImageList.add((List<String>) Arrays.asList(stringTempList));
I expect this line to take my array (stringTempList) and move the items into an ArrayList. I was hoping to use code similar to arrayImageList.get(i); to access individual elements.
However, the code above seems to add all items to the first index in arrayImageList (list size is always 1). Running some debug tests eg Log.d("Test", arrayImageList.get(0)); returns the following:
[One,Two,Three,Four, etc...]
I'd be grateful if someone could point me in the right direction. I think I've confused two different ideas here.
Change arrayImageList to a list of strings:
private ArrayList<String> arrayImageList = new ArrayList<String>();
and add them using the addAll method:
arrayImageList.addAll((List<String>) Arrays.asList(stringTempList));
I think you want to use addAll() instead of add()
You'll also want to declare
private List<String> arrayImageList = new ArrayList<String>();
You want to use addAll, not add.
I'm not sure i got this "wrong the right way", and I'm not that familiar with java, but here are my thoughts:
Your "arrayImageList" object is an ArrayList of List's. When youre adding your stringTempList-object, that object is correctly stored at the first available index of your 'List-of-lists'. If you want to access the individual elements (strings) from your "arrayImageList", you either have to adress the second dimension (the list of the list) like this:
arrayImageList.get(0).get(i) <- not entirely sure about java-syntax here, but you get the picture.
If on the other hand you dont need the extra dimension, just use ArrayList instead og ArrayList>.

How to get values of an array in the alphabetical order?

I have an array of strings plus one additional string. I want to use this string and values of array to get a set of string. Then I want to order the set of string alphabetically and extract the string which is the first in the list. What is the easiest way to do it in Java?
ADDED:
I wanted to do it this way:
List<String> playersList = Arrays.asList(players);
playersList.add(userName); // <---------- HERE IS A PROBLEM
Collections.sort(playersList);
I do not get any errors during the compilation. But during the execution I get a "UnsopportedOperationException". And it happens in the second line.
Arrays.asList wraps the array with an unmodifiable List, so when you try to add to it it throws UnsupportedOperationException. What you could do it create a new a ArrayList and add your elements to it, then you're free to modify it afterwards.
List<String> list = new ArrayList<String>(Arrays.asList(players));
list.add(userName);
If you just want to get the minimum of an array of String with an additional external element, then you don't have to sort and extract first (which would be O(N log N)). You can do it in O(N).
String minPlayer = Collections.min(Arrays.asList(players));
minPlayer = Collections.min(Arrays.asList(minPlayer, extraPlayer));
Either append the value to the array and sort it with Arrays.sort or create a List of the items and sort them using Collections.sort. The natural ordering of the strings will be alphabetical.

Categories

Resources