LinkedList of LinkedList with recursion - loop issue - java

I have a List of Lists structure and a recursive function called tree. In the following code it NEVER reaches the current == null statement so it will run forever.
If I cannot use null, what is the solution?
private void tree(LinkedList<LinkedList<String>> partitions, LinkedList<String> part)
{
LinkedList<String> current = findBiggerPartitionContained(partitions, part);
if (current == null) {
return;
}
tree(partitions, current);
}
private LinkedList<String> findBiggerPartitionContained(LinkedList<LinkedList<String>> partitions, LinkedList<String> part)
{
LinkedList<String> max = new LinkedList<>();
boolean flag = false;
for (LinkedList<String> item : partitions) {
if (item.size() > max.size() && part.containsAll(max)) {
max = item;
flag = true;
}
}
if (!flag)
return null;
flag = false;
return max;
}

Most of the time flag will be true because your condition tests item.size() > max.size(), and max is initialized with an empty list. When max is empty, the expression part.containsAll(max) will be true as well, which leads to unexpected results.
In order to fix this, you can use this in findBiggerPartitionContained:
if (item.size() > max.size() && item.containsAll(part)) {
max = item;
flag = true;
}
And this in tree:
if (current.equals(part)) {
return;
} else {
tree(partitions, current);
}
If I have understood correctly, you're looking for the biggest list in partitions which contains part. Maybe the following is less error prone and more readable:
List<String> result = partitions.stream().filter(list -> list.containsAll(part))
.max(Comparator.comparingInt(List::size))
.orElse(null);
You can test it with this MCVE:
List<String> p0 = new LinkedList<>(Arrays.asList("a", "b", "c"));
List<String> p1 = new LinkedList<>(Arrays.asList("a", "b"));
List<String> p2 = new LinkedList<>(Arrays.asList("a", "b", "c", "d"));
List<String> p3 = new LinkedList<>(Arrays.asList("a", "b", "e", "d"));
List<List<String>> partitions = Arrays.asList(p0, p1, p2, p3);
List<String> part = new LinkedList<>(Arrays.asList("a", "b", "e"));
List<String> result = partitions.stream().filter(list -> list.containsAll(part))
.max(Comparator.comparingInt(List::size))
.orElse(null);
System.out.println(result);
Bear in mind that this may returns null to handle absent Optionals.

Related

How efficiently sort a list by groups?

I need to group a given sort list by some given "blocks" or "groups" of elements. For example:
Given a list:
[A, B, C, D, E, F, G, H, I, J]
And groups
[A, C, D]
[F, E]
[J, H, I]
the result should be
[A, C, D, B, F, E, G, J, H, I]
The blocks of elements can not be mixed with non-group elements. The blocks should have the same order. The other elements of the list should mantain their order.
I have already found a solution. But it's not the most efficient code as you will see.
I'm using java 6 also...
public static List<CategoryProduct> sortProductsByBlocks(List<CategoryProduct> products, CategoryBlocks categoryBlocks) {
if (!validateCategoryBlocks(categoryBlocks)) {
return products;
}
Map<String, BlockView> mapProductByBlock = mapBlocksByPartnumber(categoryBlocks);
Map<String, BlockView> mapFirstProductByBlock = mapFirstProductByBlock(categoryBlocks);
Map<Integer, Block> blocksById = blocksById(categoryBlocks);
List<CategoryProduct> sortedProduct = Lists.newArrayList();
Map<String, CategoryProduct> productsMapByPartNumber = ProductHelper.getProductsMapByPartNumber(products);
List<CategoryProduct> processedProducts = Lists.newArrayList();
int j = 0;
for (int i = 0; i < products.size(); i++) {
CategoryProduct product = products.get(i);
if (blocksById.isEmpty() && !processedProducts.contains(product)) {
sortedProduct.add(j++, product);
processedProducts.add(product);
}
if (!processedProducts.contains(product) && (mapFirstProductByBlock.get(product.getPartNumber()) != null
|| mapProductByBlock.get(product.getPartNumber()) == null)) {
BlockView blockView = mapProductByBlock.get(product.getPartNumber());
if (blockView != null) {
Block block = blocksById.get(blockView.getBlockId());
if (block == null) {
sortedProduct.add(j++, product);
continue;
}
for (BlockProduct blockProduct : block.getProducts()) {
CategoryProduct categoryProduct = productsMapByPartNumber.get(blockProduct.getPartnumber());
sortedProduct.add(j++, categoryProduct);
processedProducts.add(categoryProduct);
}
blocksById.remove(blockView.getBlockId());
} else {
sortedProduct.add(j++, product);
processedProducts.add(product);
}
}
}
return sortedProduct;
}
Any advice to improve and make it faster will be welcome.
(edit with the improved code)
public static List<CategoryProduct> sortProductsByBlocks2(List<CategoryProduct> products,
CategoryBlocks categoryBlocks) {
if (!validateCategoryBlocks(categoryBlocks)) {
return products;
}
Map<String, Integer> blocksIdByFirstPartnumber = Maps.newHashMap();
List<String> partnumbersInBlocks = Lists.newArrayList();
for (int k = 0; k < categoryBlocks.getBlocks().size(); k++) {
Block block = categoryBlocks.getBlocks().get(k);
if (block != null && block.getProducts() != null) {
for (int i = 0; i < block.getProducts().size(); i++) {
BlockProduct blockProduct = block.getProducts().get(i);
if (i == 0) {
blocksIdByFirstPartnumber.put(blockProduct.getPartnumber(), k);
} else {
partnumbersInBlocks.add(blockProduct.getPartnumber());
}
}
}
}
CategoryProduct[] result = new CategoryProduct[products.size()];
Map<String, Integer> productsIndex = Maps.newHashMap();
Map<String, CategoryProduct> categoryProductByPartnumber = Maps.newHashMap();
int indexResult = 0;
for (CategoryProduct categoryProduct : products) {
String partNumber = categoryProduct.getPartNumber();
if (!partnumbersInBlocks.contains(partNumber)) {
if (blocksIdByFirstPartnumber.get(partNumber) != null) {
Block categoryProductBlock = categoryBlocks.getBlocks()
.get(blocksIdByFirstPartnumber.get(partNumber));
result[indexResult] = categoryProduct;
indexResult++;
for (int i = 1; i < categoryProductBlock.getProducts().size(); i++) {
BlockProduct blockProduct = categoryProductBlock.getProducts().get(i);
if (categoryProductByPartnumber.get(blockProduct.getPartnumber()) != null) {
result[indexResult] = categoryProductByPartnumber.get(blockProduct.getPartnumber());
} else {
productsIndex.put(blockProduct.getPartnumber(), indexResult);
result[indexResult] = null;
}
indexResult++;
}
} else {
result[indexResult] = categoryProduct;
indexResult++;
}
} else {
if (productsIndex.get(partNumber) != null) {
result[productsIndex.get(partNumber)] = categoryProduct;
} else {
categoryProductByPartnumber.put(partNumber, categoryProduct);
}
}
}
return Lists.newArrayList(Arrays.asList(result));
}
Performance:
Elements New algorithm Old algorithm
1200 0.002s 0.129s
12000 0.021s 14.673s
Form the code you submitted, I cannot figure out how your algorithm is fully working.
I can write another algorithm that will do the task.
Mark the first element for each group
[A,C,D] -> A
Remove from list(to_be_sorted) all elements from groups that are not marked
[A,C,D] -> remove [C,D]
perform sort on list
result ([A,B,F,G,J])
place removed element based on Mark
Initial Sorted List [A,B,F,G,J]
A->add [C,D]
List is [A,C,D,B,F,G,J]
B->as it is
F->add [E]
List is [A,C,D,B,F,E,G,J]
G->as it is
J->add [H,I]
Final Sorted List [A,C,D,B,F,E,G,J,H,I]
Time complexity is the same as sorting algorithm
By your definition it isn't entirely clear what the conditions are to merge the results from your given list and 'groups' ( arrays ). However, here is a solution based on your requirements using the assertion
"You want the first element of the list not contained in any of the groups inserted between the groups... "
public class MergeArrays {
private static final List<String> FIRST = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"));
private static final List<String> SECOND = new ArrayList<>(Arrays.asList("A", "C", "D"));
private static final List<String> THIRD = new ArrayList<>(Arrays.asList("F", "E"));
private static final List<String> FOURTH = new ArrayList<>(Arrays.asList("J", "H", "I"));
public static List<String> merge(List<String> source, List<String>... lists) {
List<String> result = new ArrayList<>();
for (List<String> list : lists) {
for (String value : list) {
source.remove(value);
}
}
for (List<String> list : lists) {
String value = null;
if (source.size() > 0) {
value = source.get(0);
source.remove(0);
}
result.addAll(merge(value, list));
}
return result;
}
public static List<String> merge(String value, List<String> list) {
List<String> result = new ArrayList<>(list);
if (value != null) {
result.add(value);
}
return result;
}
public static void main(String[] args) {
List<String> result = merge(FIRST, SECOND, THIRD, FOURTH);
System.out.println(result);
}
}
//Results
[A, C, D, B, F, E, G, J, H, I]

Java 8 - Merge All Subsets Containing Common Elements

Starting with a set of sets "groups":
Set<Set<String>> groups = new HashSet<>();
I want to create a new list of sets by merging all subsets with common elements:
i.e. Starting with the sets below:
A = {a, b, c}
B = {c, d, e, f}
C = {f, g, h, i, j}
D = {k, l, m}
E = {m, n, o}
F = {p, q, r}
The final result would be:
Set 1 = {a, b, c, d, e, f, g, h, i, j}
Set 2 = {k, l, m, n, o}
Set 3 = {p, q, r}
Any advice on how to accomplish this would be appreciated.
EDIT: In case of uneven sets it would perform the same. So if it were a method, it pseudo would look like this:
public void doStuff(){
Set<Set<String>> groups = {{a,b,c}, {c,d,e,f}, {m, n, o}}
Set<Set<String>> newGroups = mergeSubsets(groups);
System.out.println(newGroups);
}
public Set<Set<String>> mergeSubsets(Set<Set<String>> groups){
//some operations
}
Console out:
New Groups: {{a,b,c,d,e,f}, {m, n, o}}
You can just implement the algorithm as you describe it in your problem statement -- find intersecting sets and merge them until there is nothing to merge. Standard library has a method Collections.disjoint that helps by determining if two collections have any elements in common:
// this implementation sacrifices efficiency for clarity
public Set<Set<String>> mergeSubsets(Set<Set<String>> groups) {
Set<Set<String>> result = new HashSet<>();
for (Set<String> set : groups) {
// try to find a set in result that intersects this set
// if one is found, merge the two. otherwise, add this set to result
result.stream()
.filter(x -> !Collections.disjoint(x, set))
.findAny()
.ifPresentOrElse( // this method was added in java 9
x -> x.addAll(set),
() -> result.add(new HashSet<>(set))
);
}
// if nothing got merged we are done; otherwise, recurse and try again
return result.size() == groups.size() ? result : mergeSubsets(result);
}
Here is the imperative way based on #NiksVij solution. Obviously the solution of #NiksVij is not correct and this answer aims to fix this and extend a bit more:
public class MergeSet {
public static void main(String... args) {
List<Set<String>> list = new ArrayList<>();
String[] A = {"a", "c", "e", "g"};
String[] B = {"b", "d", "f", "h"};
String[] C = {"c", "e", "f"};
String[] D = {"b"};
list.add(new HashSet<>(Arrays.asList(A)));
list.add(new HashSet<>(Arrays.asList(C)));
list.add(new HashSet<>(Arrays.asList(B)));
list.add(new HashSet<>(Arrays.asList(D)));
List<Set<String>> newGroups = merge(list);
System.out.println(newGroups);
}
#SuppressWarnings("empty-statement")
private static <T> List<Set<T>> merge(List<Set<T>> list) {
if (list == null || list.isEmpty()) {
return list;
}
List<Set<T>> merged = new ArrayList<>();
do {
merged.add(list.get(0));
list.remove(0);
while (mergeStep(merged.get(merged.size() - 1), list));
} while (!list.isEmpty());
return merged;
}
private static <T> boolean mergeStep(Set<T> setToCheck, List<Set<T>> remainingList) {
boolean atLeastOnceMerged = false;
Iterator<Set<T>> iterator = remainingList.iterator();
while (iterator.hasNext()) {
Set<T> elements = iterator.next();
boolean doMerge = !Collections.disjoint(elements, setToCheck);
if (doMerge) {
atLeastOnceMerged |= doMerge;
setToCheck.addAll(elements);
iterator.remove();
}
}
return atLeastOnceMerged;
}
import java.util.*;
public class MergeSet {
public static void main(String... args) {
List<Set<String>> groups = new ArrayList<>();
String[] A = {"a", "b", "c"};
String[] B = {"c", "d", "e", "f"};
String[] C = {"f", "g", "h", "i", "j"};
String[] D = {"k", "l", "m"};
String[] E = {"m", "n", "o"};
String[] F = {"p", "q", "r"};
groups.add(new HashSet<>(Arrays.asList(A)));
groups.add(new HashSet<>(Arrays.asList(B)));
groups.add(new HashSet<>(Arrays.asList(C)));
groups.add(new HashSet<>(Arrays.asList(D)));
groups.add(new HashSet<>(Arrays.asList(E)));
groups.add(new HashSet<>(Arrays.asList(F)));
Set<Set<String>> newGroups = mergeSubsets(groups);
System.out.println(newGroups);
}
private static Set<Set<String>> mergeSubsets(List<Set<String>> groups) {
List<Set<String>> newGroups = new ArrayList<>();
Set<String> init = groups.get(0);
groups.remove(0);
newGroups.add(init);
while (!groups.isEmpty()) {
removeMergedElementFromGroupAndUpdateNewGroup(newGroups.get(newGroups.size() - 1), groups);
if(!groups.isEmpty()) {
init = groups.get(0);
groups.remove(0);
newGroups.add(init);
}
}
return new HashSet<>(newGroups);
}
private static void removeMergedElementFromGroupAndUpdateNewGroup(Set<String> master2, List<Set<String>> masterList) {
Iterator<Set<String>> iterator = masterList.iterator();
while (iterator.hasNext()) {
Set<String> strings = iterator.next();
boolean merge = strings.stream().anyMatch(string -> master2.contains(string));
if (merge) {
master2.addAll(strings);
iterator.remove();
}
}
}
}
Hope this helps instead of Set<Set<String>> groups I have used List<Set<String>> groups for the ease of using lists if you have a constraint of using Set only , you can generate List from Set(say yourSet) by passing it into the constructor of Lists implementation , for eg.
groups = new ArrayList<>(yourSet);

How to filter a collection of sets by intersection?

I need to union a collection of sets by intersection of sets and write a function with such signature
Collection<Set<Integer>> filter(Collection<Set<Integer>> collection);
Here is a simple example of sets
1) {1,2,3}
2) {4}
3) {1,5}
4) {4,7}
5) {3,5}
In this example we can see that sets 1, 3, and 5 intersect. We can rewrite it as a new set {1,2,3,5}. Also we have two sets that have intersections as well. They're 2 and 4, and we can create a new set {4,7}. The output result will be a collection of two sets: {1,2,3,5} and {4,7}.
I don't know from which point to start solving this task.
This should solve your use-case. It may be implemented in a more efficient way, but I guess this should give you an idea to start with:
private static Collection<Set<Integer>> mergeIntersections(Collection<Set<Integer>> collection) {
Collection<Set<Integer>> processedCollection = mergeIntersectionsInternal(collection);
while (!isMergedSuccessfully(processedCollection)) {
processedCollection = mergeIntersectionsInternal(processedCollection);
}
return processedCollection;
}
private static boolean isMergedSuccessfully(Collection<Set<Integer>> processedCollection) {
if (processedCollection.size() <= 1) {
return true;
}
final Set<Integer> mergedNumbers = new HashSet<>();
int totalNumbers = 0;
for (Set<Integer> set : processedCollection) {
totalNumbers += set.size();
mergedNumbers.addAll(set);
}
if (totalNumbers > mergedNumbers.size()) {
return false;
}
return true;
}
private static Collection<Set<Integer>> mergeIntersectionsInternal(Collection<Set<Integer>> collection) {
final Collection<Set<Integer>> processedCollection = new ArrayList<>();
// ITERATE OVER ALL SETS
for (final Set<Integer> numberSet : collection) {
for (final Integer number : numberSet) {
boolean matched = false;
// ITERATE OVER ALL PROCESSED SETS COLLECTION
for (final Set<Integer> processedSet : processedCollection) {
// CHECK OF THERE IS A MATCH
if (processedSet.contains(number)) {
matched = true;
// MATCH FOUND, MERGE THE SETS
processedSet.addAll(numberSet);
// BREAK OUT OF PROCESSED COLLECTION LOOP
break;
}
}
// IF NOT MATCHED THEN ADD AS A COLLECTION ITEM
if (!matched) {
processedCollection.add(new HashSet<>(numberSet));
}
}
}
return processedCollection;
}
This is how it executed it:
public static void main(String[] args) {
final Collection<Set<Integer>> collection = new ArrayList<>();
final Set<Integer> set1 = new HashSet<>();
set1.add(1);
set1.add(2);
set1.add(3);
collection.add(set1);
final Set<Integer> set2 = new HashSet<>();
set2.add(4);
collection.add(set2);
final Set<Integer> set3 = new HashSet<>();
set3.add(1);
set3.add(5);
collection.add(set3);
final Set<Integer> set4 = new HashSet<>();
set4.add(4);
set4.add(7);
collection.add(set4);
final Set<Integer> set5 = new HashSet<>();
set5.add(3);
set5.add(5);
collection.add(set5);
System.out.println(mergeIntersections(collection));
}
Here’s my go. It deletes all sets from the input collection, this could be easily fixed by making a copy first. It does not modify each set in the input collection. With my implementation Ajay’s main method prints [[1, 2, 3, 5], [4, 7]].
Collection<Set<Integer>> filter(Collection<Set<Integer>> collection) {
Collection<Set<Integer>> mergedSets = new ArrayList<>(collection.size());
// for each set at a time, merge it with all sets that intersect it
while (! collection.isEmpty()) {
// take out the first set; make a copy as not to mutate original sets
Set<Integer> currentSet = new HashSet<>(removeOneElement(collection));
// find all intersecting sets and merge them into currentSet
// the trick is to continue merging until we find no intersecting
boolean mergedAny;
do {
mergedAny = false;
Iterator<Set<Integer>> it = collection.iterator();
while (it.hasNext()) {
Set<Integer> candidate = it.next();
if (intersect(currentSet, candidate)) {
it.remove();
currentSet.addAll(candidate);
mergedAny = true;
}
}
} while (mergedAny);
mergedSets.add(currentSet);
}
return mergedSets;
}
private static Set<Integer> removeOneElement(Collection<Set<Integer>> collection) {
Iterator<Set<Integer>> it = collection.iterator();
Set<Integer> element = it.next();
it.remove();
return element;
}
/** #return true if the sets have at least one element in common */
private static boolean intersect(Set<Integer> leftSet, Set<Integer> rightSet) {
// don’t mutate, take a copy
Set<Integer> copy = new HashSet<>(leftSet);
copy.retainAll(rightSet);
return ! copy.isEmpty();
}
An elegant way to solve this problem is using Undirected Graphs, where you connect an element from an input set with at least one other element from the same set, and then look for the Connected Components.
So the graph representation of your example is:
And from that we can easily infer the Connected Components: {1, 2, 3, 5} and {4, 7}.
Here is my code:
Collection<Set<Integer>> filter(Collection<Set<Integer>> collection) {
// Build the Undirected Graph represented as an adjacency list
Map<Integer, Set<Integer>> adjacents = new HashMap<>();
for (Set<Integer> integerSet : collection) {
if (!integerSet.isEmpty()) {
Iterator<Integer> it = integerSet.iterator();
int node1 = it.next();
while (it.hasNext()) {
int node2 = it.next();
if (!adjacents.containsKey(node1)) {
adjacents.put(node1, new HashSet<>());
}
if (!adjacents.containsKey(node2)) {
adjacents.put(node2, new HashSet<>());
}
adjacents.get(node1).add(node2);
adjacents.get(node2).add(node1);
}
}
}
// Run DFS on each node to collect the Connected Components
Collection<Set<Integer>> result = new ArrayList<>();
Set<Integer> visited = new HashSet<>();
for (int start : adjacents.keySet()) {
if (!visited.contains(start)) {
Set<Integer> resultSet = new HashSet<>();
Deque<Integer> stack = new ArrayDeque<>();
stack.push(start);
while (!stack.isEmpty()) {
int node1 = stack.pop();
visited.add(node1);
resultSet.add(node1);
for (int node2 : adjacents.get(node1)) {
if (!visited.contains(node2)) {
stack.push(node2);
}
}
}
result.add(resultSet);
}
}
return result;
}
IMHO the best solution is Union-Find algorithm
An implemtation:
public class UnionFind {
Set<Integer> all = new HashSet<>();
Set<Integer> representants = new HashSet<>();
Map<Integer, Integer> parents = new HashMap<>();
public void union(int p0, int p1) {
int cp0 = find(p0);
int cp1 = find(p1);
if (cp0 != cp1) {
int size0 = parents.get(cp0);
int size1 = parents.get(cp1);
if (size1 < size0) {
int swap = cp0;
cp0 = cp1;
cp1 = swap;
}
parents.put(cp0, size0 + size1);
parents.put(cp1, cp0);
representants.remove(cp1);
}
}
public int find(int p) {
Integer result = parents.get(p);
if (result == null) {
all.add(p);
parents.put(p, -1);
representants.add(p);
result = p;
} else if (result < 0) {
result = p;
} else {
result = find(result);
parents.put(p, result);
}
return result;
}
public Collection<Set<Integer>> getGroups() {
Map<Integer, Set<Integer>> result = new HashMap<>();
for (Integer representant : representants) {
result.put(representant, new HashSet<>(-parents.get(representant)));
}
for (Integer value : all) {
result.get(find(value)).add(value);
}
return result.values();
}
public static Collection<Set<Integer>> filter(Collection<Set<Integer>> collection) {
UnionFind groups = new UnionFind();
for (Set<Integer> set : collection) {
if (!set.isEmpty()) {
Iterator<Integer> it = set.iterator();
int first = groups.find(it.next());
while (it.hasNext()) {
groups.union(first, it.next());
}
}
}
return groups.getGroups();
}
}

Java smallest partition of common elements (in the same order) of two or more lists

I'm looking for a quick and smart way to find up until where two lists are equal. In other words I need to find the smallest partition containing common elements in the same order of two or more lists.
It might sound a bit confusing but here is an example of what I want to achieve:
List 1: A, B, C, L, M Z
List 2: A, B, C, K, F
Output -> List 3: A, B, C
I need to use this in a recursive method which should be called with large inputs and all the solutions I've come up with are a bit too slow.
Thanks for your answers in advance
EDIT:
Please excuse me for being unclear. This is my first question and english is not my first language.
Let me explain the problem in a better way. I need to find the intersection of two or more lists starting from the first element of the lists. Please note that elements must be in the same order so it's not exactly an intersection but more like a partition.
the "recursive" thing was just to say that I need to include this in a recursive method which will run many times so I would like the solution to be as fast as possibile as to not lose a lot of time.
Working on an answer that appears to have been deleted I came up with my own solution:
List<String> list1 = new ArrayList<>(Arrays.asList("ciao", "come"));
List<String> list2 = new ArrayList<>(Arrays.asList("ciao", "come", "va"));
List<String> list3 = new ArrayList<>(Arrays.asList("ciao", "come", "va", "?", "tutto", "ok"));
List<List<String>> allLists = new ArrayList<>();
allLists.addAll(Arrays.asList(list1, list2, list3));
int min = Integer.MAX_VALUE;
int listIndex = 0;
for(List<String> list : allLists){
if(min > list.size()){
min = list.size();
listIndex = allLists.indexOf(list);
}
}
int index = 0;
boolean same = true;
while(index<min && same == true) {
String element = allLists.get(listIndex).get(index);
for(List<String> list : allLists){
if(!list.get(index).equals(element)){
same = false;
break;
}
element = allLists.get(listIndex).get(index);
}
if(same == true) ++index;
}
System.out.println("OUTPUT:" + allLists.get(listIndex).subList(0, index));
----> Output: ciao, come
EDIT2:
And also garnful's solution works like a charm and I find it way clearer than mine. Thanks everybody
This should do the work, and hopefully be quite okay regarding the performance:
public List<String> getEqualsPart(List<String>[] listsToCheck) {
if (listsToCheck.length == 0) {
return Collections.emptyList();
}
int minLength = getShortesListLength(listsToCheck);
if (minLength == 0) {
return Collections.emptyList();
}
return getEqualPartsForIndex(listsToCheck, 0, minLength, new ArrayList<String>());
}
private int getShortesListLength(List<String>[] listsToCheck) {
int min = Integer.MAX_VALUE;
for (List<String> currentList : listsToCheck) {
min = Math.min(min, currentList.size());
}
return min;
}
private List<String> getEqualPartsForIndex(List<String>[] listsToCheck, int index, int minLength,
List<String> result) {
if (index == minLength) {
return result;
}
Set<String> setForIndex = new HashSet<>();
Arrays.stream(listsToCheck).forEach(list -> setForIndex.add(list.get(index)));
if (setForIndex.size() > 1) {
return result;
} else {
result.add(setForIndex.iterator().next());
return getEqualPartsForIndex(listsToCheck, index + 1, minLength, result);
}
}`
Try:
public List<E> equalUntil(List<E> l1, List<E> l2) {
return equalUntilRec(l1.iterator(), l2.iterator(), new ArrayList<E>());
}
private int equalUntilRec(Iterator<E> it1, Iterator<E> it2, List<E> acc) {
if(!it1.hasNext() || !it2.hasNext()) {
return acc;
} else {
E e1 = it1.next();
E e2 = it2.next();
if(!e1.equals(e2)) {
return acc;
}
acc.add(e1);
return equalUntilRec(it1, it2, acc);
}
}

ArrayList's containsAll returning wrong value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Let's say:
a = ["s", "i", "n", "e", "d"];
b = ["s", "e", "n", "d"];
(a and b are of type List<String>)
How can I determine if all the letters in b are contained in a? -- not necessarily in order (In this case, it's true since [s,e,n,d] is in a and b)
Using a.containsAll(b) does not always work!
Another example:
a=["b", "a", "z", "z", "z"]
b=["a", "a", "b", "b"]
Here I want the result to be false since [a,a,b,b] does not appear in any sort of order in a, but using a.containsAll(b) will return true!
Try this:
private boolean containsAll(List<?> a, List<?> b) {
// List doesn't support remove(), use ArrayList instead
ArrayList<Object> x = new ArrayList<Object>();
ArrayList<Object> y = new ArrayList<Object>();
x.addAll(a);
y.addAll(b);
for (Object o : y) {
if (!x.remove(o)) // an element in B is not in A!
return false;
}
return true; // all elements in B are also in A
}
The idea is to remove each letter in b from a. When you try to remove a letter that is not in a, then it's confirmed that a doesn't contain all letters in b.
(remove() will return true if the element exists, otherwise false)
Simply add your whole String list to new String variable by using for each
And find the one String variable value contains the other String or not, by using .contains()
List<String> a = ["b","a","n"];
List<String> b = ["b","a","n","a","n","a"];
String newA = null;
String newB = null;
for(String strA : a) {
newA += strA;
}
for(String strB : b) {
newB += strB;
}
if(newA.contains(newB))
return True;
else
return False;
Reference for String .contains()
Remove from the bigger list all the elements that not appear in the smaller list and equal them. If they are equal then the smaller list is contained in the bigger:
static List<String> list1 = new ArrayList<String>(){{
add("b");
add("a");
add("n");
add("z");
add("z");
add("z");
}};
static List<String> list2 = new ArrayList<String>(){{
add("b");
add("a");
add("n");
add("a");
add("n");
add("a");
}};
public static void main(String[] args) {
if(deepContains(list1, list2))
System.out.println("List2 is contained in List1");
}
public static boolean deepContains(List<String> one, List<String> two){
if (one == null && two == null){
return true;
}
if((one == null && two != null)
|| one != null && two == null){
return false;
}
//to avoid messing the order and elements of the lists we will use a copy
one = new ArrayList<String>(one);
two = new ArrayList<String>(two);
//This removes from one all the elements not contained in two
one.retainAll(two);
int a = one.size();
int b = two.size();
//one has lesser elements than two, for sure two is not contained in one
if(a < b) return false;
//one has the same number of elements of two, check if they are the same
if(a == b){
Collections.sort(one);
Collections.sort(two);
return one.equals(two);
}
//one has more elements than two. Remove duplicate elements
//and check for equality
Set<String> set1 = new HashSet<String>(one);
Set<String> set2 = new HashSet<String>(two);
if(set1.size() == set2.size()){
one = new ArrayList<String>(set1);
two = new ArrayList<String>(set2);
Collections.sort(one);
Collections.sort(two);
return one.equals(two);
}
return false;
}
Here is a version that works for any collection of any type:
private <E> boolean containsAllIncludingDuplicates(Collection<E> container,
Collection<E> items) {
Set<E> checkedItems = new HashSet<>();
for (E item : items) {
if (checkedItems.add(item)
&& Collections.frequency(container, item) < Collections
.frequency(items, item)) {
return false;
}
}
return true;
}
The use of the Set ensures that the frequency check is not repeated multiple times when there are duplicates in items.

Categories