Comparison of two lists in java using for loop - java

I have two list word containing words (word is a copy of the list words) and existingGuesses containing characters and I want to compare them (means compare whether each character is present in the list word or not) by iterating through a for loop. Can anybody suggest me how to do the comparison?
public List<String> getWordOptions(List<String> existingGuesses, String newGuess)
{
List<String> word = new ArrayList<String>(words);
/* String c = existingGuesses.get(0);
ListIterator<String> iterator = word.listIterator();
while(iterator.hasNext()){
if(word.contains(c))
{
word.remove(c);
}
}*/
for(String temp: word){
for(String cha: existingGuesses){
}
}
return null;
}

You can check for the guesses in words like this by using the List#contains(Object).
for(String myGuess: existingGuesses){
if(word.contains(myGuess)) {
// Do what you want
}
}

How about the following O(N) complexity code
public List<String> getWordOptions(List<String> existingGuesses, String newGuess) {
List<String> word = new ArrayList<String>(words);
for (String cha : existingGuesses) {
if (word.contains(cha)) {
word.remove(cha);
}
}
return null;
}

If you want to compare them and remove them if its there,
Then You can use the List#removeAll(anotherlist)
Removes from this list all of its elements that are contained in the specified collection (optional operation).
(Got clue from word.remove(c);) from your commented code.

You can use Collection.retainAll:
List<String> word=new ArrayList<String>();//fill list
List<String> existingGuesses=new ArrayList<String>();//fill list
List<String> existingWords=new ArrayList<String>(word);
existingWords.retainAll(existingGuesses);
//existingWords will only contain the words present in both the lists
System.out.println(existingWords);

Related

How can I validate whether the String contains any of Strings from a List?

I found only questions about cases where the given string to match is a substring of the list items but what I need is the opposite:
I have a list of strings which might be the exact or a substring of a given String.
Using:
boolean bValid = listOfStrings.contains(sText);
gives me false in case the sText is not found in the list exactly as it is.
For example:
Suppose I have the following strings list:
List<String> listOfStrings = Arrays.asList("ABC","DEF","GHI");
And I need to find whether one of the items equals or a substring of, for example the string: ABC1".
I will get false. But I'd like to get true in that case.
Using Stream API you can do that in the following way:
String s = "ABC1";
bValid = listOfStrings.stream()
.anyMatch(s::contains);
Basically, reverse the check - check if s contains any of Strings in List.
You need to iterate over your list and check whether any of the list items is contained in your string.
for(String s: listOfStrings) {
if(sText.contains(s)) {
bvalid = true;
}
}
Just a performance improvement of #Tobias's solution.
Make sure to put break in loop, if a match has been found.
for(String s: listOfStrings) {
if(sText.contains(s)) {
bvalid = true;
break;
}
}
List<String> listOfStrings = Arrays.asList("ABC", "DEF", "GHI");
boolean bValid = false;
String sText = "ABC1";
for(String str: listOfStrings) {
if(sText.contains(str)) {
bValid = true;
}
}
System.out.println(bValid);
Hope the above code helps you a bit. Reply if not and clearly brief your question.
Thanks.

Java compare two string lists for specific characters and delete it

I have two String lists.
List-1 Ex.: "ftzat955","zat75" "a875rt955", "rRat955", "duozf", "ulw7trRT"
List-2 Ex.: "rt", "zf"
Now I want to loop the first List and delete all the String that contain "rt" and "zf" from List-2.
I am not searching against a complete string instead I'm only searching for a string-pair in whole strings.
I didn't find any Utils that I can use to solve this problem.
Can somebody help me with this?
Thank you for helps.
List<String> resultList = new ArrayList<String>();
for (String l1 : list1)
{
boolean stay = true;
for (String l2 : list2)
if (l1.contains(l1))
stay = false;
if (stay)
resultList.add(l1);
}
public ArrayList<String> filterList(ArrayList<String> mainList, ArrayList<String> comparableList){
ArrayList<String> filteredList=new ArrayList<String>();
for(String s: comparableList){
for (String s2:mainList){
if(s2.contains(s))filteredList.add(s2);
}
}
return filteredList;
}

ArrayList content check over entire array

I'm trying to get this to return a certain number of array entries based on their containing a certain input string.
/**
* This method returns a list of all words from
* the dictionary that include the given substring.
*/
public ArrayList<String> wordsContaining(String text)
{
ArrayList<String> contentCheck = new ArrayList<String>();
for(int index = 0; index < words.size(); index++)
{
if(words.contains(text))
{
contentCheck.add(words.get(index));
}
}
return contentCheck;
}
I don't understand why this keeps returning freaking every value in the array instead of only the entries containing the string bit.
Thanks!
Your condition:
if(words.contains(text))
checks whether the text is in the list or not. That would be true for all or none of the elements.
What you want is:
if(words.get(index).contains(text))
Apart from that, it would be better if you use enhanced for statement:
for (String word: words) {
if(word.contains(text)) {
contentCheck.add(word);
}
}
You have 2 issues in your code
The first one is one is that You check in your condition
if(words.contains(text)) - this check that text is in list
and what you probably want is to check that given item of list contains text
public List<String> wordsContaining(String text)
{
List<String> contentCheck = new ArrayList<String>();
for(String word : words) //For each word in words
{
if(word.contains(text)) // Check that word contains text
{
contentCheck.add(word);
}
}
return contentCheck;
}

How to Count Unique Values in an ArrayList?

I have to count the number of unique words from a text document using Java. First I had to get rid of the punctuation in all of the words. I used the Scanner class to scan each word in the document and put in an String ArrayList.
So, the next step is where I'm having the problem! How do I create a method that can count the number of unique Strings in the array?
For example, if the array contains apple, bob, apple, jim, bob; the number of unique values in this array is 3.
public countWords() {
try {
Scanner scan = new Scanner(in);
while (scan.hasNext()) {
String words = scan.next();
if (words.contains(".")) {
words.replace(".", "");
}
if (words.contains("!")) {
words.replace("!", "");
}
if (words.contains(":")) {
words.replace(":", "");
}
if (words.contains(",")) {
words.replace(",", "");
}
if (words.contains("'")) {
words.replace("?", "");
}
if (words.contains("-")) {
words.replace("-", "");
}
if (words.contains("‘")) {
words.replace("‘", "");
}
wordStore.add(words.toLowerCase());
}
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
}
System.out.println("The total number of words is: " + wordStore.size());
}
Are you allowed to use Set? If so, you HashSet may solve your problem. HashSet doesn't accept duplicates.
HashSet noDupSet = new HashSet();
noDupSet.add(yourString);
noDupSet.size();
size() method returns number of unique words.
If you have to really use ArrayList only, then one way to achieve may be,
1) Create a temp ArrayList
2) Iterate original list and retrieve element
3) If tempArrayList doesn't contain element, add element to tempArrayList
Starting from Java 8 you can use Stream:
After you add the elements in your ArrayList:
long n = wordStore.stream().distinct().count();
It converts your ArrayList to a stream and then it counts only the distinct elements.
I would advice to use HashSet. This automatically filters the duplicate when calling add method.
Although I believe a set is the easiest solution, you can still use your original solution and just add an if statement to check if value already exists in the list before you do your add.
if( !wordstore.contains( words.toLowerCase() )
wordStore.add(words.toLowerCase());
Then the number of words in your list is the total number of unique words (ie: wordStore.size() )
This general purpose solution takes advantage of the fact that the Set abstract data type does not allow duplicates. The Set.add() method is specifically useful in that it returns a boolean flag indicating the success of the 'add' operation. A HashMap is used to track the occurrence of each original element. This algorithm can be adapted for variations of this type of problem. This solution produces O(n) performance..
public static void main(String args[])
{
String[] strArray = {"abc", "def", "mno", "xyz", "pqr", "xyz", "def"};
System.out.printf("RAW: %s ; PROCESSED: %s \n",Arrays.toString(strArray), duplicates(strArray).toString());
}
public static HashMap<String, Integer> duplicates(String arr[])
{
HashSet<String> distinctKeySet = new HashSet<String>();
HashMap<String, Integer> keyCountMap = new HashMap<String, Integer>();
for(int i = 0; i < arr.length; i++)
{
if(distinctKeySet.add(arr[i]))
keyCountMap.put(arr[i], 1); // unique value or first occurrence
else
keyCountMap.put(arr[i], (Integer)(keyCountMap.get(arr[i])) + 1);
}
return keyCountMap;
}
RESULTS:
RAW: [abc, def, mno, xyz, pqr, xyz, def] ; PROCESSED: {pqr=1, abc=1, def=2, xyz=2, mno=1}
You can create a HashTable or HashMap as well. Keys would be your input strings and Value would be the number of times that string occurs in your input array. O(N) time and space.
Solution 2:
Sort the input list.
Similar strings would be next to each other.
Compare list(i) to list(i+1) and count the number of duplicates.
In shorthand way you can do it as follows...
ArrayList<String> duplicateList = new ArrayList<String>();
duplicateList.add("one");
duplicateList.add("two");
duplicateList.add("one");
duplicateList.add("three");
System.out.println(duplicateList); // prints [one, two, one, three]
HashSet<String> uniqueSet = new HashSet<String>();
uniqueSet.addAll(duplicateList);
System.out.println(uniqueSet); // prints [two, one, three]
duplicateList.clear();
System.out.println(duplicateList);// prints []
duplicateList.addAll(uniqueSet);
System.out.println(duplicateList);// prints [two, one, three]
public class UniqueinArrayList {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
List al=new ArrayList();
al.add("Stack");
al.add("Stack");
al.add("over");
al.add("over");
al.add("flow");
al.add("flow");
System.out.println(al);
Set s=new LinkedHashSet(al);
System.out.println(s);
Iterator itr=s.iterator();
while(itr.hasNext()){
sb.append(itr.next()+" ");
}
System.out.println(sb.toString().trim());
}
}
3 distinct possible solutions:
Use HashSet as suggested above.
Create a temporary ArrayList and store only unique element like below:
public static int getUniqueElement(List<String> data) {
List<String> newList = new ArrayList<>();
for (String eachWord : data)
if (!newList.contains(eachWord))
newList.add(eachWord);
return newList.size();
}
Java 8 solution
long count = data.stream().distinct().count();

string compare in java

I have a ArrayList, with elements something like:
[string,has,was,hctam,gnirts,saw,match,sah]
I would like to delete the ones which are repeating itself, such as string and gnirts, and delete the other(gnirts). How do I go about achieving something as above?
Edit: I would like to rephrase the question:
Given an arrayList of strings, how does one go about deleting elements containing reversed strings?
Given the following input:
[string,has,was,hctam,gnirts,saw,match,sah]
How does one reach the following output:
[string,has,was,match]
Set<String> result = new HashSet<String>();
for(String word: words) {
if(result.contains(word) || result.contains(new StringBuffer(word).reverse().toString())) {
continue;
}
result.add(word);
}
// result
You can use a comparator that sorts the characters before checking them for equality. This means that compare("string", "gnirts") will return 0. Then use this comparator as you traverse through the list and copy the matching elements to a new list.
Another option (if you have a really large list) is to create an Anagram class that extends the String class. Override the hashcode method so that anagrams produce the same hashcode, then use a hashmap of anagrams to check your array list for anagrams.
HashSet<String> set = new HashSet<String>();
for (String str : arraylst)
{
set.add(str);
}
ArrayList<String> newlst = new ArrayList<String>();
for (String str : arraylst)
{
if(!set.contains(str))
newlst.add(str);
}
To remove duplicate items, you can use HashMap (), where as the key codes will be used by the sum of the letters (as each letter has its own code - is not a valid situation where two different words have an identical amount of code numbers), as well as the value - this the word. When adding a new word in a HashMap, if the amount of code letters of new words is identical to some of the existing key in a HashMap, then the word with the same key is replaced by a new word. Thus, we get the HashMap collection of words without repetition.
With regard to the fact that the bottom line "string" looks better "gnirts". It may be a situation where we can not determine which word is better, so the basis has been taken that the final form of the word is not important - thing is that there are no duplicate
ArrayList<String> mainList = new ArrayList<String>();
mainList.add("string,has,was,hctam,gnirts,saw,match,sah");
String[] listChar = mainList.get(0).split(",");
HashMap <Integer, String> hm = new HashMap<Integer, String>();
for (String temp : listChar) {
int sumStr=0;
for (int i=0; i<temp.length(); i++)
sumStr += temp.charAt(i);
hm.put(sumStr, temp);
}
mainList=new ArrayList<String>();
Set<Map.Entry<Integer, String>> set = hm.entrySet();
for (Map.Entry<Integer, String> temp : set) {
mainList.add(temp.getValue());
}
System.out.println(mainList);
UPD:
1) The need to maintain txt-file in ANSI
In the beginning, I replaced Scaner on FileReader and BufferedReader
String fileRStr = new String();
String stringTemp;
FileReader fileR = new FileReader("text.txt");
BufferedReader streamIn = new BufferedReader(fileR);
while ((stringTemp = streamIn.readLine()) != null)
fileRStr += stringTemp;
fileR.close();
mainList.add(fileRStr);
In addition, all the words in the file must be separated by commas, as the partition ishonoy lines into words by the function split (",").
If you have words separated by another character - replace the comma at the symbol in the following line:
String[] listChar = mainList.get(0).split(",");

Categories