Count the Number of String Matches - java

I want to get the smallest match of strings in the list. Though I'm successful doing so but the problem is i want to count that how many match counts have been made so:
List<String> mylist=new LinkedList<String>();
Set<String> result=new LinkedHashSet<String>();
mylist.add("interpreter");
mylist.add("interprete");
mylist.add("interpret");
mylist.add("developed");
mylist.add("develops");
mylist.add("develop");
mylist.add("interpret");
String small="";
Collections.sort(mylist);
Collections.reverse(mylist);
for(int i=0;i<mylist.size();i++)
{
small=mylist.get(i);
for(int j=i;j<mylist.size();j++)
{
if(small.contains(mylist.get(j)))
{
small=mylist.get(j);
}
}
result.add(small);
}
for (String string : result) {
System.out.println(string);
}
So that the output should be:
interpret=4
develop=4
Problem occurs with the following code i am trying:
List<String> mylist=new LinkedList<String>();
Set<String> result=new LinkedHashSet<String>();
mylist.add("interpreter");
mylist.add("interprete");
mylist.add("interpret");
mylist.add("developed");
mylist.add("develops");
mylist.add("develop");
mylist.add("interpret");
mylist.add("crawler");
mylist.add("crawl");
mylist.add("mobile");
mylist.add("mob");
mylist.add("juni");
mylist.add("junis");
Collections.sort(mylist);
Collections.reverse(mylist);
String small="";
int c=0;
for(int i=0;i<mylist.size();i++)
{
c+=1;
small=mylist.get(i);
for(int j=i;j<mylist.size();j++)
{
if(small.contains(mylist.get(j)))
{
small=mylist.get(j);
c+=1;
}
}
result.add(small);
}
for (String string : result) {
System.out.println(string+"="+c);
}
can somebody help me please!

Putting #jambriz's answer in your code:
1.Use a HashMap
HashMap<String, Integer> result= new LinkedHashMap<String, Integer>();
2.Instead of result.add(small); now, add the value in hashmap only if the value is new or the count is less than the previous count. Also, set c=0 here
if (!result.containsKey(small) || result.get(small) < c)
result.put(small, c);
c = 0;
3.At Last print your results:
for (String key : result.keySet())
System.out.println(key + ": " + result.get(key));

ok, first of all, your first code would print
interpret
develop
because you aren´t counting anything
and it should be
interpret=4
develop=3
anyway.
The second block only has one counter 'c'. You should have one counter per each found word.
I would suggest using a Map of Strings and Integers. When the String is nonexistent you put(small,1) and when it exists you get the Integer and add one to it.
Let us know if that worked
(btw, there's no regex in your code, it shouldn't be tagged as such)

String[] smallestStrings; //this has "interpret", "develop"
int[] matches = new int[smallestStrings.length];
for (int i = 0; i < matches.length; i++) {
matches[i] = 0;
for (String s : mylist) if (s.contains(smallestStrings[i])) matches[i]++;
}

Related

Grouping the nested loop elements based on the first iteration variable in Java

I have 2 lists one for the sentence one for the keywords. The idea is to check if the sentence have the keywords. and put them in a list for each sentence in order.
I am sorry if this is already duplicated here in advance.
List <String> sentence= new ArrayList <>();
sentence.add("this is a good dog");
sentence.add("cats drink milk");
sentence.add("Animals are beautiful creatures");
List <String> keyword= new ArrayList <>();
keyword.add("dog");
keyword.add("cats");
keyword.add("beautiful");
keyword.add("good");
keyword.add("are");
keyword.add("this");
keyword.add("milk");
My idea was to create 2 nested loops for each list:
for (int b = 0; b < sentence.size(); b++) {
for (int c = 0; c < keyword.size(); c++) {
if (sentence.get(b).contains(keyword.get(c))) {
System.out.println(keyword.get(c));
}
}
}
The output of this is:
dog
good
this
cats
milk
beautiful
are
The desired output would be:
[this,good,dog]
[cats,milk]
[are,beautiful]
So it is like getting all the existing keywords, in the order of the sentence,not related to keywords order.
and then group the existing keywords for each sentence, as in the order of existence.
Hope it is clear. Would really appreciate any ideas. doesnt have to follow the same method.
Iterate over your sentence list. For each sentence iterate over your keyword list. Add each found keyword found in a tempList, sort the tempList by the index of keyword in sentence and finally add each tempList to a list of lists. Example:
public static void main(String[] args) {
List <String> sentence= new ArrayList <>();
sentence.add("this is a good dog");
sentence.add("cats drink milk");
sentence.add("Animals are beautiful creatures");
List <String> keyword= new ArrayList <>();
keyword.add("dog");
keyword.add("cats");
keyword.add("beautiful");
keyword.add("good");
keyword.add("are");
keyword.add("this");
keyword.add("milk");
List<List<String>> result = new LinkedList<>();
for(String sen: sentence){
List<String> tempList = new ArrayList<>();
for(String key: keyword){
if(sen.contains(key)){
tempList.add(key);
}
}
tempList.sort(new Comparator<String>() {
#Override
public int compare(String o1, String o2) {
return sen.indexOf(o1) - sen.indexOf(o2) ;
}
});
result.add(tempList);
}
for(List<String> r : result){
System.out.println(r);
}
}
You need a slight modification in your looping
for (int i = 0; i < sentence.size(); i++) {
String[] matchArray = new String[sentence.get(i).split(" ").length];
for (int j = 0; j < keyword.size(); j++) {
if (sentence.get(i).contains(keyword.get(j))) {
matchArray[Arrays.asList(sentence.get(i).split(" ")).indexOf(keyword.get(j))] = keyword.get(j);
}
}
List<String> matchList = new ArrayList<String>();
for(String match: matchArray) {
if(match != null) {
matchList.add(match);
}
}
System.out.println(matchList);
}
For every sentence create an array with size same as the sentence (just to ensure size). Now when matches are found get the index of the match from sentence and add element to that particular index of the array. So at the end of keyword iteration you will have all matches in array with null values if some words are not matching.
Now declare a new List of String into which add the elements from array which are not null. At last print the list.
I think Map would be a good choice here. Just make sentences keys for the map and keywords as value. Following is the code for the same.
Map <String, ArrayList<String>> sentences= new HashMap<>();
sentences.put("this is a good dog", new ArrayList<>());
sentences.put("cats drink milk", new ArrayList<>());
sentences.put("Animals are beautiful creatures", new ArrayList<>());
List <String> keyword= new ArrayList <>();
keyword.add("dog");
keyword.add("cats");
keyword.add("beautiful");
keyword.add("good");
keyword.add("are");
keyword.add("this");
keyword.add("milk");
keyword.forEach(word -> sentences.entrySet().stream()
.filter(map -> map.getKey().contains(word)).
forEach(map -> sentences.computeIfAbsent(map.getKey(), key->new ArrayList<>()).add(word)));
sentences.forEach((key, value) -> System.out.println(value));
Try something like this:
for (String sen: sentence) {
System.out.print("[");
boolean first = true;
for (String word: sen.split("[\\s\\p{Punct}]")) {
if (keyword.contains(word)) {
if (first) {
first = false;
} else {
System.out.print(",");
}
System.out.print(word);
}
}
System.out.println("]");
}
this should do it, printing exactly in the format you requested :
for (int b = 0; b < sentence.size(); b++) {
String arr[] = sentence.get(b).split("\\s+");
List result = new ArrayList<>();
for (int c = 0; c < arr.length; c++ ) {
if (keyword.contains(arr[c]))
result.add(arr[c]);
}
System.out.println(result);
}
I would use the following :
for(String currentSentence : sentence) {
List<String> keywordsInSentence = new ArrayList<>();
for (String word : currentSentence.split("\\s+")) {
if (keyword.contains(word)) {
keywordsInSentence.add(word);
}
}
System.out.println(keywordsInSentence);
}
You can try it here.
(and I'd rename sentence into sentences or sentenceList and similarly for keyword, otherwise it's just confusing)
If you need to do anything more to the keywords than immediately displaying them, you could insert the keywordsInSentence lists into a Map<String, List<String>> you would value by replacing the System.out.println by map.put(currentSentence, keywordsInSentence).

Anagrams finding in java

I stuck on a problem. I have a String array which is consist of String[]={"eat", "tea", "tan", "ate", "nat", "bat"} Now, I should segregated those word which have same letter on it and make a group. eat,tea,ate they have same letter in each word so this is a group. Group 2 should be tan,nat and Group3 should be bat. So I have to make a list of list to store those groups.
My approach:
To solve this problem I first find out the ascii values of each letter and then add those ascii values for a word. Like eat find out the ascii values of e,a,t and add them. I take this approach because if the letters are repeated in the words then they must have same ascii sum. After that I group them same Ascii sums and find out which words have those sums then they belongs to same group.
My progress
I find out ascii sums and put them in a hashmap. But then I could not group the same values. As I failed to group the ascii values I cannot find out the words.I have no clue how to proceed.
I also follow this posts
post1
post2
But there approach and my approach is not same. Also the questions are different from mine. I am discussing here about a different approach which is depend upon ASCII values.
My code:
public List<List<String>> groupAnagrams(String[] strs) {
ArrayList<Character>indivistr=new ArrayList<>();
ArrayList<Integer>dup=new ArrayList<>();
HashMap<Integer,Integer>mappingvalues=new HashMap<>();
for(int i=0;i<strs.length;i++){
int len=strs[i].length();
int sum=0;
for(int j=0;j<len;j++){
indivistr.add(strs[i].charAt(j));
int ascii=(int)strs[i].charAt(j);
sum=sum+ascii;
}
mappingvalues.put(i,sum);
}
}
One more approach
I transfer the map keys in a Arraylist and map values in a ArrayList. Something like that,
ArrayList<Integer>key_con=new ArrayList<
(mappingvalues.keySet());
ArrayList<Integer>val_con=new ArrayList<>(mappingvalues.values());
Then using two loops and put the same values into another list.
for(int k=0;k<val_con.size();k++){
for(int k1=k+1;k1<val_con.size();k1++){
if(val_con.get(k).equals(val_con.get(k1))){
dup.add(val_con.get(k1));
}
}
Now if I print dup output will be [314, 314, 314, 323] which is partially correct. It should be 314,314,314,323,323,311
This should get you started.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Main {
public static void main(String args[]) throws Exception {
String[] words ={"eat", "tea", "tan", "ate", "nat", "bat"};
for(List<String> list : groupAnagrams(words))
System.out.println(list);
}
public static List<ArrayList<String>> groupAnagrams(String[] words) {
List<ArrayList<String>> wordGroups = new ArrayList<ArrayList<String>>();
HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
for(String word : words) {
int sum = 0;
for(char c : word.toCharArray())
sum += c;
if(map.containsKey(sum))
map.get(sum).add(word);
else {
ArrayList<String> list = new ArrayList<String>();
list.add(word);
map.put(sum, list);
}
}
for(ArrayList<String> list : map.values())
wordGroups.add(list);
return wordGroups;
}
}
This program will work for small scale things such as this but consider the following input data:
{"a", "#!"}
The sum of these Strings are both 97.
Since you're using ASCII values to find anagrams you might run into a case such as this. This isn't a particularly pressing matter until you start messing around with lowercase letters and capitals. Easy fix for that is just a String.ToUpperCase() and map the symbols to huge numbers and you're good to go.
For posterity:
public class anagrams {
public static void main(String args[]) {
int numberOfAnagrams = 0;
String[] stringArray = {"eat", "tea", "tan", "ate", "nat", "bat", "plate", "knot"};
List<String> stringList = Arrays.asList(stringArray);
for(int i = 0; i < stringList.size() - 1; i++) {
for(int j = i + 1; j < stringList.size(); j++) {
if(isAnagram(stringList.get(i), stringList.get(j))) {
System.out.println(stringList.get(i) + " " + stringList.get(j));
numberOfAnagrams += 2;
}
}
}
System.out.println(numberOfAnagrams);
}
private static boolean isAnagram(String s1, String s2) {
// In order for two String to be anagrams, they must have the same length.
if(s1.length() != s2.length()) {
return false;
}
// If s2 does not contain even one of s1's chars return false.
for(int i = 0; i < s1.length(); i++) {
if(!s2.contains("" + s1.charAt(i))) {
return false;
}
}
return true;
}
}
Based on the asci approach I have made a working code
public static void main(String[] args) {
String[] values ={"eat", "tea", "tan", "ate", "nat", "bat"};
Map<Integer, List<String>> resultMap = new HashMap<Integer, List<String>>();
for (String value : values) {
char[] caharacters = value.toLowerCase().toCharArray();
int asciSum = 0;
for (char character : caharacters) {
asciSum = asciSum + (int)character;
}
System.out.println(asciSum);
if(resultMap.containsKey(asciSum)) {
resultMap.get(asciSum).add(value);
}else {
List<String> list = new ArrayList<String>();
list.add(value);
resultMap.put(asciSum, list);
}
}
System.out.println(resultMap);
}
This will give result
{323=[tan, nat], 311=[bat], 314=[eat, tea, ate]}
but if we encounter diff characters with same asci value sum like 10+11 = 20+1
below code will work where based on the sorted string we make the result map
public static void main(String[] args) {
String[] values ={"eat", "tea", "tan", "ate", "nat", "bat"};
Map<String, List<String>> resultMap = new HashMap<String, List<String>>();
for (String value : values) {
char[] caharacters = value.toLowerCase().toCharArray();
Arrays.sort(caharacters);
String sortedValue = new String(caharacters);
System.out.println(sortedValue);
if(resultMap.containsKey(sortedValue)) {
resultMap.get(sortedValue).add(value);
}else {
List<String> list = new ArrayList<String>();
list.add(value);
resultMap.put(sortedValue, list);
}
}
System.out.println(resultMap);
}
This will return
{aet=[eat, tea, ate], abt=[bat], ant=[tan, nat]}
I have fixed the comments and edits provided.
Here's my idea, first I would create a class that will store the original string and it's sorted version:
class Anagram {
String s;
String sorted;
}
Then I map the input to my list of Anagram:
List<Anagram> collect = Arrays.stream(strs)
.map(a -> new Anagram(a, Arrays.stream(a.split(""))
.sorted()
.reduce(String::concat).get()))
.collect(Collectors.toList());
Then I just group the obtained list by sorted string:
Map<String, List<Anagram>> groupBy = collect
.stream()
.collect(Collectors.groupingBy(Anagram::getSorted));
Now you have the lists with grouped anagrams, just extract from them the original string:
List<List<String>> result = new ArrayList<>();
for(List<Anagram> list : collect1.values()) {
List<String> myList = list.stream().map(Anagram::getS).collect(Collectors.toList());
result.add(myList);
}

How to search for similarities in an ArrayList

I have an arrayList of String which I would like to display in a Spinner or drop down menu, but mostly what happens is I have Strings repeating, what I want to do is to search through the arrayList for similarities, if its found a string for example "Hello World" occurs 7 times in the arrayList, remove the the other 6 and assign 7 to it to show that it occurred 7 times, so my new String will be "Hello world (7)", could anyone help me on how I can implement this in my code below:
for(int i = 0; i < timesCalleddb.getAllTimesCalled(missedCall.getNumber()).size(); i++)
{
if(timesCalleddb.getAllTimesCalled(missedCall.getNumber()).get(i) ==
timesCalleddb.getAllTimesCalled(missedCall.getNumber()).get(i+1))
{
//where am guessing the implementation my problem should be
}
}
You should consider using map data structure, since you have to store the counter, otherwise, hash set would be perfect:
ArrayList<String> strs = ...;
HashMap<String, Integer> counter = new HashMap<String, Integer>();
for(String s : strs) {
counter.put(s, counter.get(s) == null ? 1 : counter.get(s) + 1);
}
for(String s : counter.keySet()) {
System.out.println(s + " (" + counter.get(s) + ")");
}
You could do the following:
Iterate over the List and make a HashMap<String, Integer> indicating how many times each String appears.
Remove duplicates from the List using list = new ArrayList<String>(new LinkedHashSet<String>(list));. Using a LinkedHashSet means that the order is kept.
Build up a new List by iterating over the List and adding either string or string + " (" + map.get(string) + ")" depending on whether map.get(string) is 1 or more than 1.
You can use this code for filtering you CustomList based on a particular String of the list. If you want to count the number of occurence, you can add some counter in the loop.
List<MyCustomObject> arrayList = new ArrayList<>();
List<MyCustomObject> result = new ArrayList<>();
Set<String> set = new HashSet<>();
for(MyCustomObject item : arrayList) {
if(set.add(item.getSomeString()) {
resultArray.add(item);
}
}
arrayList.clear();
arrayList.addAll(result);
To remove the repeated String from a List, use the following:
List<String> arrayList = new ArrayList<>();
// Your elements must be added to the arrayList, before progressing to the next step.
Set<String> set = new HashSet<>();
set.addAll(arrayList );
// Code for getting count of each String
int count = 0;
List<Integer> arrayListCount = new ArrayList<>();
for (Iterator<String> it = set.iterator(); it.hasNext(); ) {
String str = it.next();
arrayListCount.add(count , 0);
for(int i = 0; i < arrayList.size(); i++){
String s = arrayList.get(i);
if (str.equals(s)) {
arrayListCount.set(count , arrayListCount.get(count) + 1);
}
}
count++;
}
// Code for getting count ends here
arrayList.clear();
arrayList.addAll(set);
Note: The sequence of the List won't be retained.
Hope that Helps!!!

Counting occurrences in a string array and deleting the repeats using java

i'm having trouble with a code. I have read words from a text file into a String array, removed the periods and commas. Now i need to check the number of occurrences of each word. I managed to do that as well. However, my output contains all the words in the file, and the occurrences.
Like this:
the 2
birds 2
are 1
going 2
north 2
north 2
Here is my code:
public static String counter(String[] wordList)
{
//String[] noRepeatString = null ;
//int[] countArr = null ;
for (int i = 0; i < wordList.length; i++)
{
int count = 1;
for(int j = 0; j < wordList.length; j++)
{
if(i != j) //to avoid comparing itself
{
if (wordList[i].compareTo(wordList[j]) == 0)
{
count++;
//noRepeatString[i] = wordList[i];
//countArr[i] = count;
}
}
}
System.out.println (wordList[i] + " " + count);
}
return null;
I need to figure out 1) to get the count value into an array.. 2) to delete the repetitions.
As seen in the commenting, i tried to use a countArr[] and a noRepeatString[], in hopes of doing that.. but i had a NullPointerException.
Any thought on this matter will be much appreciated :)
I would first convert the array into a list because they are easier to operate on than arrays.
List<String> list = Arrays.asList(wordsList);
Then you should create a copy of that list (you'll se in a second why):
ArrayList<String> listTwo = new ArrayList<String>(list);
Now you remove all the duplicates in the second list:
HashSet hs = new HashSet();
hs.addAll(listTwo);
listTwo.clear();
listTwo.addAll(hs);
Then you loop through the second list and get the frequency of that word in the first list. But first you should create another arrayList to store the results:
ArrayList<String> results = new ArrayList<String>;
for(String word : listTwo){
int count = Collections.frequency(list, word);
String result = word +": " count;
results.add(result);
}
Finally you can output the results list:
for(String freq : results){
System.out.println(freq);}
I have not tested this code (can't do that right now). Please ask if there is a problem or it doesnÄt work. See these questions for reference:
How do I remove repeated elements from ArrayList?
One-liner to count number of occurrences of String in a String[] in Java?
How do I clone a generic List in Java?
some syntax issues in your code but works fine
ArrayList<String> results = new ArrayList<String>();
for(String word : listTwo){
int count = Collections.frequency(list, word);
String result = word +": "+ count;
results.add(result);
}

How do I compare 2 arraylists in a Hashmap?

I have an arraylist called fetchContactName() which returns all names (31) in my contact list, an arraylist called fetchContactNumbers() which returns all phone numbers (31) associated with fetchContactName(), and an arraylist called newList which has all the numbers that sent me text messages (6).
I was trying to change the phone numbers in newList by looking for the same numbers in fetchContactNumbers(). When there was a match, I would then index the position in fetchContactNumbers() and use that index to get the name at the same position in fetchContactName(). From there, I would replace the numbers in newList with the names in fetchContactName() according to the indexed position.
This is an example I've made that executes as stated above:
http://pastebin.com/pApHNkXa
The problem with the above example is that when I apply it to my listview, only one element in the list is changed to a name. More oddly, newList.get(3) is the only one that changed to a name. Not newList at positions 0, 1, 2, 4 and 5.
From others suggests, how can I use a HashMap to give the same expected result as the example given above? I was told to use fetchContactNumbers() and fetchContactNames() in the Hashmap then use the HashMap to compare with newList, but I cant find any source to achieve this.
UPDATE:
Here is the working code for my HashMap, but the results are still the same as above:
for (String str : fetchContactNames()) {
contactNameNew += str + " ";
}
for (String str2 : fetchContactNumbers()) {
contactNumberNew += str2 + " ";
}
//String to split
String temp[];
String temp2[];
String delimiter = " ";
temp = contactNameNew.split(delimiter);
temp2 = contactNumberNew.split(delimiter);
HashMap<String, String> contacts = new HashMap<String, String>();
contacts.put(contactNameNew, contactNumberNew);
for(int i = 0; i < fetchContactNames().size(); i++){
contacts.put(temp[i], temp2[i]);
}
Iterator<Entry<String, String>> iterator = contacts.entrySet().iterator();
while(iterator.hasNext()){
Entry<String, String> entry = iterator.next();
int position = getPosition(newlist, entry.getValue());
if (newlist.contains(entry.getValue())) {
newlist.set(position, entry.getKey());
}
}
I think it will be better if I changed your code and answered...
import java.util.ArrayList;
import java.util.HashMap;
public class StringT {
public static void main(String[] args) {
// alpha would represent fetchContactNames()
ArrayList<String> alpha = new ArrayList<String>();
// bravo would represent fetchContactNumbers()
HashMap<String, String> bravo = new HashMap<String, String>();
// charlie would represent newList
ArrayList<String> charlie = new ArrayList<String>();
bravo.put("1", "One"); charlie.add("5");
bravo.put("2", "Two"); charlie.add("1");
bravo.put("3", "Three"); charlie.add("3");
bravo.put("4", "Four"); charlie.add("4");
bravo.put("5", "Five"); charlie.add("2");
// this will print charlie just as you see above
for(int i = 0; i < charlie.size(); i ++){
System.out.println(charlie.get(i));
}
System.out.println("---------------------");
// this will print charlie with the numbers replaced as names
for(int i = 0; i < charlie.size(); i++){
String value;
if((value = bravo.get(charlie.get(i))) != null){
charlie.set(i, value);
}
}
for(int i = 0; i < charlie.size(); i ++){
System.out.println(charlie.get(i));
}
}
}
your code is working that u placed here http://pastebin.com/pApHNkXa i tried it in android and its working alright its giving output as five, two, seven, three .

Categories