In the below example, how would I go about comparing the contents of ArrayList al4 to either of the other ArrayLists? In the same manner I compared al1 to al2.
import java.util.ArrayList;
public class Details {
public static void main(String[] args) {
ArrayList<String> al1 = new ArrayList<String>();
al1.add("hi");
al1.add("How are you");
al1.add("Good Morning");
al1.add("bye");
al1.add("Good night");
ArrayList<String> al2 = new ArrayList<String>();
al2.add("Howdy");
al2.add("Good Evening");
al2.add("bye");
al2.add("Good night");
ArrayList<ArrayList<String>> al4 = new ArrayList<ArrayList<String>>();
al4.add(al1);
// Storing the comparison output in ArrayList<String>
ArrayList<String> al3 = new ArrayList<String>();
for (String temp : al1)
al3.add(al2.contains(temp) ? "Yes" : "No");
System.out.println(al3);
}
}
Output for al3
[No, No, No, Yes, Yes]
It depends a little on what you mean. You can't directly compare al4 with al2, because they're of different types: one is an ArrayList<String> and one is an ArrayList<ArrayList<String>>.
When you compare al1 and al2, what you're doing is determining, for each element of al1, whether it occurs anywhere in al2. I presume this is what you want. You are not deciding whether it occurs in the same place in al2.
The first thing you should do is improve this comparison, which is rather inefficient. Since you don't care about ordering of al2, you should rewrite the comparison like this:
ArrayList<String> al3 = new ArrayList<String>();
Set<String> al2set = new HashSet<String>(al2);
for (String temp : al1)
al3.add(al2set.contains(temp) ? "Yes" : "No");
This is a lot more efficient because you don't have to traverse the whole of al2 for each lookup.
Now, if you want to compare your list of lists with a list, I can only assume that you mean you want to tell, for each element of any of the lists in al4, whether it occurs in al2. If so, then you want
ArrayList<String> al3 = new ArrayList<String>();
Set<String> al2set = new HashSet<String>(al2);
for (List<String> tempList : al4)
for (String temp : tempList)
al3.add(al2set.contains(temp) ? "Yes" : "No");
This will give you one flat ArrayList<String>, recording, for each element of the lists of al4, whether that element was in al2 somewhere.
It is possible that you want your result to be an ArrayList<ArrayList<String>>, so that the result mimics the structure of al4, in which case it's only slightly more complicated:
ArrayList<ArrayList<String>> al3list = new ArrayList<ArrayList<String>>();
Set<String> al2set = new HashSet<String>(al2);
for (List<String> tempList : al4) {
ArrayList<String> al3 = new ArrayList<String>();
for (String temp : tempList)
al3.add(al2set.contains(temp) ? "Yes" : "No");
al3list.add(al3);
}
After executing this, a3list will have the same structure as al4, and each element of each list will be either "Yes" or "No", according to whether the corresponding element in al4 was contained somewhere in al2.
You can create a method like this.
public static boolean[] compare(ArrayList<String> al1, ArrayList<String> al2) {
boolean result[] = new boolean[al1.size()]; // result
int i = 0; // index
/*
* Test if an element of al1 occurs in al2
*/
for(String s : al1) {
if(al2.contains(s)) {
result[i] = true;
}
else {
result[i] = false;
}
i++; // increment
}
return result;
}
And after you can call this method and display the result
boolean result[] = compare(al1, al2);
for(int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
Related
I have one List of String Elements reading from a property file and then returning a List of these elements:
public static ArrayList<String> getExpextedTestDataForHeaderList() throws IOException {
ArrayList<String> testDataList = new ArrayList<String>();
testDataList.add(hpExpectedTxtStoiximaBtn());
testDataList.add(hpExpectedTxtLiveStoiximaBtn());
testDataList.add(hpExpectedTxtVirtualBtn());
testDataList.add(hpExpectedTxtCasinoBtn());
testDataList.add(hpExpectedTxtLiveCasinoBtn());
testDataList.add(hpExpectedTxtOtherGamesBtn());
testDataList.add(hpExpectedTxtLogInBtn());
testDataList.add(hpExpectedTxtRegisterBtn());
for (int i = 0; i < testDataList.size(); i++) {
String actualTitle = testDataList.get(i).toString();
//System.out.println("From File: "+actualTitle);
}
return testDataList;
}
Then I have a method to get the elements (getElementsNames) and convert them in to a second list
private static ArrayList<String> methodConvertElementToList(String path) {
ArrayList<String> list = getElementsNames(path);
for (int i = 0; i < list.size(); i++) {
return list;
}
return list;
}
Up to this point when I execute my program I get back 2 list with elements.
Now, how I can search from the second list if contains elements of the first list and then print those elements in screen?
I came up with some code but it is not working. Can anyone help with this?
public static void printToScreen(String path,List<String> expectedDataList) throws IOException {
ArrayList<String> expectedNames = (ArrayList<String>) expectedDataList;
ArrayList<String> actualNames = getElementsNames(path);
//ListIterator<String> expectedNamesListIterator = expectedNames.listIterator();
//ListIterator<String> actualNamesListIterator = actualNames.listIterator();
}
With iterators? Or some other way?
If i correctly understand, you can simply use a listA.containsAll(listB) example code looks like
List<String> listA = new ArrayList<>(
List.of("ala",
"adam",
"bocian",
"hello",
"world"));
List<String> listB = new ArrayList<>(
List.of("ala",
"hello"));
if(listA.containsAll(listB)){
System.out.println("YOUR CODE");
}
It returns true if all element of listB are present in listA, then you can simply print all elements of listB.
Other way if you don't wanna check all elements you can get individual element of listA and check if it present in listB
listA.stream()
.filter(listB::contains)
.forEach(System.out::println);
I want to write a function that checks a 2-d List for duplicate elements. For example: [[key1, key2], [key1, key3]] "key1" is duplicate. I want to remove or replace it e.g with n\a. What I have tried so far.
private static List<List<String>> removeRedundantStrings(List<List<String>> list) {
List<List<String>> outList = new ArrayList<>();
List<String> oneDimension = new ArrayList<>();
for (var tempList : list) {
for (var string : tempList) {
if (!oneDimension.contains(string))
oneDimension.add(string);
else
oneDimension.add("n/a");
outList.add(oneDimension);
}
}
return outList;
}
This "solution" results in: [[key1, key2, n/a, key3]] - not a "true" 2-d list. How would I achieve the same format as the input list? The result would be: [[key1, key2], [n/a, key3]]. Am I missing something else?
You should create the inner List in the correct place - inside the outer loop.
In addition, I'd use a single HashSet to keep track of the Strings that already appeared.
List<List<String>> outList = new ArrayList<>();
Set<String> uniques = new HashSet<>();
for (var tempList : list) {
List<String> oneDimension = new ArrayList<>();
for (var string : tempList) {
if (uniques.add(string)) // add will return true if string was actually added to Set
oneDimension.add(string);
else
oneDimension.add("n/a");
}
}
outList.add(oneDimension);
}
I've searched through many questions on this site with somewhat similar underlying concepts, however after many hours of attempting to solve this problem myself and reviewing I am still lost. If there is another question that answers this I will be more than happy to give it a look over.
Ultimately I want to create a recursive method such that it takes two lists and returns a Set of String lists:
//Example of such a function definition
private static Set<List<String>> myRecursiveMethod(List<String> listOne,
List<String> listTwo) {
}
When I say "Set of String lists" I mean specifically the following:
(Note:"AD" == "DA")
// if the two following lists are INPUTTED into myRecursiveMethod();
// listOne = ["A","B"]
// listTwo = ["C","D"]
// the following set is OUTPUTTED: [["AC","BD"],["AD","BC"]]
Such that if there were three elements in both listOne and listTwo, there would be SIX elements in the set. i.e:
// listOne = ["A","B","C"]
// listTwo = ["D","E","F"]
// OUTPUTTED: [["AD","BE","CF"],["AD","BF","CE"],["BD","AE","CF"],
// ["BD","AF","CE"],["CD","AE","BF"],["CD","AF","BE"]]
I tried writing this using a double enhanced FOR loop so I could understand the logic. My FOR loop approach is terrible and only works for the HARD-CODED limit of list.size() == 2.
// Create Lists and append elements
List<String> listOne = new ArrayList<String>();
listOne.add("A");
listOne.add("B");
List<String> listTwo = new ArrayList<String>();
listTwo.add("C");
listTwo.add("D");
// List One = ["A","B"]
// List Two = ["C","D"]
// Create new List
List<List<String>> newList = new ArrayList<List<String>>();
Integer counter = 0;
for (String s : listOne) {
counter++;
for (String p : listTwo) {
// A HARD-CODED bad implementation of this method
if (counter < 3) {
List<String> newListTwo = new ArrayList<String>();
newListTwo.add(s.concat(p));
newList.add(newListTwo);
} else if (!(counter % 2 == 0)) {
newList.get(1).add(s.concat(p));
} else {
newList.get(0).add(s.concat(p));
}
}
}
System.out.println(newList); // = [["AC","BD"],["AD","BC"]]
Also you can note that I defined List<List<String>> Rather than Set<List<String>>. This was due to my badly coded attempted which relies on the list.get() method.
So my current recursive method is as follows:
private static Set<List<String>> myRecursiveMethod(List<String> listOne,
List<String> listTwo)
{
//Base Case:
if (listOne.isEmpty){
return new HashSet<List<String>>;
}
//Recursive Case:
else {
String listOneFirst = listOne.get(0);
String listTwoFirst = listTwo.get(0);
List<String> sampleList = new ArrayList<String>();
sampleList.add(listOneFirst+listTwoFirst);
Set<List<String>> newSet = new HashSet<List<String>>(myRecursiveMethod())
newSet.add(sampleList);
return newSet;
}
}
This method only acts like this currently:
INPUT:
List One = ["A","B"]
List Two = ["C","D"]
OUTPUT:
[["AC"]["BD"]]
DESIRED OUTPUT:
[["AC","BD"],["AD","BC"]]
EDIT:
After reviewing responses my W.I.P code for the class:
private static Set<List<String>> myRecursiveMethod(List<String> listOne,
List<String> listTwo) {
//Backup Case (user enters an empty list)
if (listOne.isEmpty()){
return new HashSet<List<String>>();
}
// Base Case:
if (listOne.size() == 1) {
List<String> mergedStrings = new ArrayList<>();
for (String s : listTwo) {
mergedStrings.add(listOne.get(0).concat(s));
}
Set<List<String>> builtHashSet = new HashSet<List<String>();
builtHashSet.add(mergedStrings);
return builtHashSet;
}
// Recursive Case:
else {
// Ensure original list values arn't changed.
List<String> newListOne = new ArrayList<String>(listOne);
List<String> newListTwo = new ArrayList<String>(listTwo);
//first two elements...I don't think this is correct
String listOneFirst = newListOne.get(0);
String listTwoFirst = newListTwo.get(0);
List<String> sampleList = new ArrayList<String>();
sampleList.add(listOneFirst + listTwoFirst);
//used for making recursive case smaller
newListOne.remove(0);
// Calls recursion
Set<List<String>> newSet = new HashSet<List<String>>(
myRecursiveMethod(newListOne, newListTwo));
newSet.add(sampleList);
return newSet;
}
}
I think the problem is here:
if (listOne.isEmpty){
return new HashSet<List<String>>;
}
You are correct, at some point your recursion has to end, and you have to start building the desired output. But the desired output is not a Set with an empty list. It is a Set containing some lists with some content. Thus: don't wait until listOne is empty. Instead:
if (listOne.size() == 1) {
List<String> mergedStrings = new ArrayList<>();
mergedStrings = ... merge the ONE listOne entry with all listTwo entries
Set<List<String>> rv = new HashSet<>();
rv.add(mergedStrings);
return rv;
}
In other words: you use recursion to reduce the length of the first list by one. And when only one element is left in that list, it is time to merge in the second list.
Now lets look into how to "use" that (calling the method rec for brevity); putting down some pseudo code to show the steps we need:
rec([a, b], [c,d]) -->
rec([a], [c,d]) X rec([b], [c, d]) -->
<[ac, ad]> X <[bc, bd]> -->
<[ac, ad], [bc, bd]>
"X" meaning "joining" two results from recursive calls; should be as easy as:
Set<List<String>> rec1 = rec(...);
return rec1.addAll(rec2 ...
Lets say you have an array like this: String[] theWords = {"hello", "good bye", "tomorrow"}. I want to remove/ignore all the strings in the array that have the letter 'e'. How would I go about doing that? My thinking is to go:
for (int arrPos = 0; arrPos < theWords.length; arrPos++) { //Go through the array
for (int charPos = 0; charPos < theWords[arrPos].length(); charPos++) { //Go through the strings in the array
if (!((theWords[arrPos].charAt(charPos) == 'e')) { //Finds 'e' in the strings
//Put the words that don't have any 'e' into a new array;
//This is where I'm stuck
}
}
}
I'm not sure if my logic works and if I'm even on the right track. Any responses would be helpful. Many thanks.
One easy way to filter an array is to populate an ArrayList with if in a for-each loop:
List<String> noEs = new ArrayList<>();
for (String word : theWords) {
if (!word.contains("e")) {
noEs.add(word);
}
}
Another way in Java 8 is to use Collection#removeIf:
List<String> noEs = new ArrayList<>(Arrays.asList(theWords));
noEs.removeIf(word -> word.contains("e"));
Or use Stream#filter:
String[] noEs = Arrays.stream(theWords)
.filter(word -> !word.contains("e"))
.toArray(String[]::new);
You can directly use contains() method of String class to check if "e" is present in your string. That will save your extra for loop.
It would be simple if you use ArrayList.
importing import java.util.ArrayList;
ArrayList<String> theWords = new ArrayList<String>();
ArrayList<String> yourNewArray = new ArrayList<String>;//Initializing you new array
theWords.add("hello");
theWords.add("good bye");
theWords.add("tommorow");
for (int arrPos = 0; arrPos < theWords.size(); arrPos++) { //Go through the array
if(!theWords.get(arrPos).contains("e")){
yourNewArray.add(theWords.get(arrPos));// Adding non-e containing string into your new array
}
}
The problem you have is that you need to declare and instantiate the String array before you even know how many elements are going to be in it (since you wouldn't know how many strings would not contain 'e' before going through the loop).
Instead, if you use an ArrayList you do not need to know the required size beforehand. Here is my code from start to end.
String[] theWords = { "hello", "good bye", "tomorrow" };
//creating a new ArrayList object
ArrayList<String> myList = new ArrayList<String>();
//adding the corresponding array contents to the list.
//myList and theWords point to different locations in the memory.
for(String str : theWords) {
myList.add(str);
}
//create a new list containing the items you want to remove
ArrayList<String> removeFromList = new ArrayList<>();
for(String str : myList) {
if(str.contains("e")) {
removeFromList.add(str);
}
}
//now remove those items from the list
myList.removeAll(removeFromList);
//create a new Array based on the size of the list when the strings containing e is removed
//theWords now refers to this new Array.
theWords = new String[myList.size()];
//convert the list to the array
myList.toArray(theWords);
//now theWords array contains only the string(s) not containing 'e'
System.out.println(Arrays.toString(theWords));
I am trying to figure out how to add a string, into a string ArrayList, between two strings that are already in. So if I have this
ArrayList<String> List = new ArrayList<String>();
List.add("Yes");
List.add("No");
List.add("Maybe");
How would I go along putting the word "Or" between them and make the ArrayList contain
"Yes" "Or" "No" "Or" "Maybe"?
I have three advices.
First, to name the variables, start with lower-case.
Second, use List as type of variable, instead of ArrayList, you will thank me later, trust me.
Third, to do what you ask for, there is overloaded method add for choosing position :
List<String> list = new ArrayList<String>();
list.add("Yes");
list.add("No");
list.add(1,"Maybe"); //insert into position 1 and shift everything to the right.
For this example, if you use System.out.println(list);, you will get this output :
[Yes, Maybe, No]
For adding Or instruction, it would be like this :
List<String> list = new ArrayList<String>();
list.add("Yes");
list.add("No");
list.add("Maybe");
list.add(1, "Or");
list.add(3, "Or");
System.out.println(list);
Output :
[Yes, Or, No, Or, Maybe]
Also, if you want to make your program more re-usable, you can write a method, that will do this for you for any case of list :
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Yes");
list.add("No");
list.add("Maybe");
list.add("Probably");
list.add("Never");
List<String> orList = addOr(list);
System.out.println(orList);
}
public static List<String> addOr(List<String> list){
List<String> newList = new ArrayList<>();
int count = 0;
for(String text : list){
count++;
newList.add(text);
if (count != list.size()){
newList.add("Or");
}
}
return newList;
}
Having this output :
[Yes, Or, No, Or, Maybe, Or, Probably, Or, Never]
However, if you want to use that list for outputing some message for user, it is not good idea to add "Or", because it is really not part of information. Rather it is good, to create method, which will create output String you desire.
This code
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Yes");
list.add("No");
list.add("Maybe");
list.add("Probably");
list.add("Never");
String niceOutput = addOr(list);
System.out.println("Choose from following options: " + niceOutput);
}
public static String addOr(List<String> list){
String orText = "";
int count = 0;
for(String text : list){
count++;
orText += '\'' + text + '\'';
if (count != list.size()){
orText += " or ";
}
}
return orText;
}
Having this output :
Choose from following options: 'Yes' or 'No' or 'Maybe' or 'Probably' or 'Never'
According to Add object to ArrayList at specified index
List.add(1, "or")
List.add(3, "or")
This should solve your problem.