I have a Input String as :
String str="1,1,2,2,2,1,3";
I want count each id occurrence and store them into List,and I want output Like this:
[
{
"count": "3",
"ids": "1, 2"
}
{
"count": "1",
"ids": "3"
}
]
I tried by using org.springframework.util.StringUtils.countOccurrencesOf(input, "a"); like this. But after counting not getting the things like I want.
This will give you the desired result. You first count the occurrences of each character, then you group by count each character in a new HashMap<Integer, List<String>>.
Here's a working example:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Test {
public static void main(String[] args) {
String str = "1,1,2,2,2,1,3";
String[] list = str.split(",");
HashMap<String, Integer> occr = new HashMap<>();
for (int i = 0; i < list.length; i++) {
if (occr.containsKey(list[i])) {
occr.put(list[i], occr.get(list[i]) + 1);
} else {
occr.put(list[i], 1);
}
}
HashMap<Integer, List<String>> res = new HashMap<>();
for (String key : occr.keySet()) {
int count = occr.get(key);
if (res.containsKey(count)) {
res.get(count).add(key);
} else {
List<String> l = new ArrayList<>();
l.add(key);
res.put(count, l);
}
}
StringBuffer sb = new StringBuffer();
sb.append("[\n");
for (Integer count : res.keySet()) {
sb.append("{\n");
List<String> finalList = res.get(count);
sb.append("\"count\":\"" + count + "\",\n");
sb.append("\"ids\":\"" + finalList.get(0));
for (int i = 1; i < finalList.size(); i++) {
sb.append("," + finalList.get(i));
}
sb.append("\"\n}\n");
}
sb.append("\n]");
System.out.println(sb.toString());
}
}
EDIT: A more generalised solution
Here's the method that returns a HashMap<Integer,List<String>>, which contains the number of occurrences of a string as a key of the HashMap where each key has a List<String> value which contains all the strings that occur key number of times.
public HashMap<Integer, List<String>> countOccurrences(String str, String delimiter) {
// First, we count the number of occurrences of each string.
String[] list = str.split(delimiter);
HashMap<String, Integer> occr = new HashMap<>();
for (int i = 0; i < list.length; i++) {
if (occr.containsKey(list[i])) {
occr.put(list[i], occr.get(list[i]) + 1);
} else {
occr.put(list[i], 1);
}
}
/** Now, we group them by the number of occurrences,
* All strings with the same number of occurrences are put into a list;
* this list is put into a HashMap as a value, with the number of
* occurrences as a key.
*/
HashMap<Integer, List<String>> res = new HashMap<>();
for (String key : occr.keySet()) {
int count = occr.get(key);
if (res.containsKey(count)) {
res.get(count).add(key);
} else {
List<String> l = new ArrayList<>();
l.add(key);
res.put(count, l);
}
}
return res;
}
You need to do some boring transfer, I'm not sure if you want to keep the ids sorted. A simple implementation is:
public List<Map<String, Object>> countFrequency(String s) {
// Count by char
Map<String, Integer> countMap = new HashMap<String, Integer>();
for (String ch : s.split(",")) {
Integer count = countMap.get(ch);
if (count == null) {
count = 0;
}
count++;
countMap.put(ch, count);
}
// Count by frequency
Map<Integer, String> countByFrequency = new HashMap<Integer, String>();
for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
String chars = countByFrequency.get(entry.getValue());
System.out.println(entry.getValue() + " " + chars);
if (chars == null) {
chars = "" + entry.getKey();
} else {
chars += ", " + entry.getKey();
}
countByFrequency.put(entry.getValue(), chars);
}
// Convert to list
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
for (Map.Entry<Integer, String> entry : countByFrequency.entrySet()) {
Map<String, Object> item = new HashMap<String, Object>();
item.put("count", entry.getKey());
item.put("ids", entry.getValue());
result.add(item);
}
return result;
}
Hey check the below code, it help you to achieve your expected result
public class Test
{
public static void main(String args[])
{
String str = "1,1,2,2,2,1,3"; //Your input string
List<String> listOfIds = Arrays.asList(str.split(",")); //Splits the string
System.out.println("List of IDs : " + listOfIds);
HashMap<String, List<String>> map = new HashMap<>();
Set<String> uniqueIds = new HashSet<>(Arrays.asList(str.split(",")));
for (String uniqueId : uniqueIds)
{
String frequency = String.valueOf(Collections.frequency(listOfIds, uniqueId));
System.out.println("ID = " + uniqueId + ", frequency = " + frequency);
if (!map.containsKey(frequency))
{
map.put(frequency, new ArrayList<String>());
}
map.get(frequency).add(uniqueId);
}
for (Map.Entry<String, List<String>> entry : map.entrySet())
{
System.out.println("Count = "+ entry.getKey() + ", IDs = " + entry.getValue());
}
}
}
One of the approach i can suggest you is to
put each "character" in hashMap as a key and "count" as a value.
Sample code to do so is
String str = "1,1,2,2,2,1,3";
HashMap<String, String> map = new HashMap();
for (String c : str.split(",")) {
if (map.containsKey( c)) {
int count = Integer.parseInt(map.get(c));
map.put(c, ++count + "");
} else
map.put(c, "1");
}
System.out.println(map.toString());
}
<!--first you split string based on "," and store into array, after that iterate array end of array lenght in side loop create new map and put element in map as a Key and set value as count 1 again check the key and increase count value in map-->
like....
String str="1,1,2,2,2,1,3";
String strArray=str.split(",");
Map strMap= new hashMap();
for(int i=0; i < strArray.length(); i++){
if(!strMap.containsKey(strArray[i])){
strMap.put(strArray[i],1)
}else{
strMap.put(strArray[i],strMap.get(strArray[i])+1)
}
}
String str="1,1,2,2,2,1,3";
//Converting given string to string array
String[] strArray = str.split(",");
//Creating a HashMap containing char as a key and occurrences as a value
Map<String,Integer> charCountMap = new HashMap<String, Integer>();
//checking each element of strArray
for(String num :strArray){
if(charCountMap.containsKey(num))
{
//If char is present in charCountMap, incrementing it's count by 1
charCountMap.put(num, charCountMap.get(num)+1);
}
else
{
//If char is not present in charCountMap, and putting this char to charCountMap with 1 as it's value
charCountMap.put(num, 1);
}
}
//Printing the charCountMap
for (Map.Entry<String, Integer> entry : charCountMap.entrySet())
{
System.out.println("ID ="+entry.getKey() + " count=" + entry.getValue());
}
}
// Split according to comma
HashMap<String, Integer> hm = new HashMap<String, Integer>();
for (String key : tokens) {
if (hm.containsKey(key)) {
Integer currentCount = hm.get(key);
hm.put(key, ++currentCount);
} else {
hm.put(key, 1);
}
}
// Organize info according to ID
HashMap<Integer, String> result = new HashMap<Integer, String>();
for (Map.Entry<String, Integer> entry : hm.entrySet()) {
Integer newKey = entry.getValue();
if (result.containsKey(newKey)) {
String newValue = entry.getKey() + ", " + result.get(newKey);
result.put(newKey, newValue);
} else {
result.put(newKey, entry.getKey());
}
}
And here is a complete Java 8 streaming solution for the problem. The main idea is to first build a map of the occurances of each id, which results in:
{1=3, 2=3, 3=1}
(first is ID and second the count) and then to group by it by the count:
public static void main(String[] args) {
String str = "1,1,2,2,2,1,3";
System.out.println(
Pattern.compile(",").splitAsStream(str)
.collect(groupingBy(identity(), counting()))
.entrySet().stream()
.collect(groupingBy(i -> i.getValue(), mapping( i -> i.getKey(), toList())))
);
}
which results in:
{1=[3], 3=[1, 2]}
This is the most compact version I could come up with. Is there anything even smaller?
EDIT: By the way here is the complete class, to get all static method imports right:
import static java.util.function.Function.identity;
import java.util.regex.Pattern;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
public class Java8StreamsTest6 {
public static void main(String[] args) {
String str = "1,1,2,2,2,1,3";
System.out.println(
Pattern.compile(",").splitAsStream(str)
.collect(groupingBy(identity(), counting()))
.entrySet().stream()
.collect(groupingBy(i -> i.getValue(), mapping(i -> i.getKey(), toList())))
);
}
}
I'm having trouble parsing a given text file based on words count frequency. Everything is happening accordingly except ordering, based on words count.
My Approach
Parsing the text file
Splitting & Storing every words in a String Array
passed array will have the count for every unique word and passed to Map
and Map will sort this based on Value
Here is the code:
public class TestApp {
public static void main(String[] args) throws IOException {
String Line = null;
Map<String, Integer> finalMap = new HashMap<String, Integer>();
FileParser parser = new FileParser();
Reader fileReader = new FileReader("E:\\21350.txt");
BufferedReader bufferReader = new BufferedReader(fileReader);
while ((Line = bufferReader.readLine()) != null) {
String[] str = Line.split("\\s");
finalMap = parser.parseFile(str);
}
// new TestApp().showEntry(finalMap); // Before
parser.sortByValue(finalMap);
new TestApp().showEntry(finalMap); // After
bufferReader.close();
}
public void showEntry(Map<String, Integer> map) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " --> " + entry.getValue());
}
}
}
public class FileParser {
Map<String, Integer> map = new HashMap<String, Integer>();
public Map<String, Integer> parseFile(String[] strArray) {
for (String key : strArray) {
int counter = 1;
if (map.containsKey(key))
map.put(key, map.get(key) + 1);
else
map.put(key, counter);
}
return map;
}
public Map<String, Integer> sortByValue(Map<String, Integer> map) {
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
int x = (Integer) ((Map.Entry) o1).getValue();
int y = (Integer) ((Map.Entry) o2).getValue();
if (x > y)
return -1;
else if (x < y)
return 1;
else
return 0;
}
});
Map<String, Integer> result = new LinkedHashMap<String, Integer>();
Iterator it = list.iterator();
while (it.hasNext()) {
Map.Entry<String, Integer> entry = (Map.Entry) it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}
}
Whats wrong with this code?
parser.sortByValue(finalMap); returns a new (sorted) map which you ignore. Instead, you print the old (unsorted) finalMap.
finalMap = parser.sortByValue(finalMap);
should fix this.
Also note that the code above creates one map per file but prints only the last one of all of them.
Depending on what you want, you should either pass the map into parser.parseFile(str); to accumulate the results for all files or sort+print inside the loop to get one result per file.
public class HashtableDemo {
static String newLine = System.getProperty("line.separator");
public static void main(String[] args) {
//dictionary can be created using HashTable object
//as dictionary is an abstract class
Hashtable ht = new Hashtable();
//put(key, value)
ht.put("MIKE", 1);
ht.put("CHIN", 2);
ht.put("CHRIS", 3 );
ht.put("HOLY", 4);
//looping through all the elements in hashtable
String str;
//you can retrieve all the keys in hashtable using .keys() method
Enumeration names = ht.keys();
while(names.hasMoreElements()) {
//next element retrieves the next element in the dictionary
str = (String) names.nextElement();
//.get(key) returns the value of the key stored in the hashtable
System.out.println(str + ": " + ht.get(str) + newLine);
}
}
}
How can I add the all the key values into a variable, like 1+2+3+4=10?
Unless you need synchronization, I would recommend using a Map instead of the HashTable. As suggested by the documentation:
If a thread-safe implementation is not needed, it is recommended to
use HashMap in place of Hashtable.
Example:
Map<String, Integer> ht = new HashMap<String, Integer>();
ht.put("MIKE", 1);
ht.put("CHIN", 2);
ht.put("CHRIS", 3 );
ht.put("HOLY", 4);
int total = 0;
for(Integer value: ht.values()){
total+=value;
}
System.out.println(total);
Instead of iterating over the keys, you could simply iterate over the value:
int sum = 0;
for (Integer val : ht.values()) {
sum += val;
}
I've these code for searching occurrence in Array-List but my problem is how I can get result
out side of this for loop in integer type cause I need in out side , may be there is another way for finding
occurrence with out using for loop can you help me ?
thank you...
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");
Set<String> unique = new HashSet<String>(list);
for (String key : unique) {
int accurNO = Collections.frequency(list, key);
System.out.println(key + ": " accurNO);
}
You should declare a map like Map<String, Integer> countMap = new HashMap<String, Integer>(); before the loop, and populate it within the loop.
Map<String, Integer> countMap = new HashMap<String, Integer>();
for (String key : unique) {
int accurNO = Collections.frequency(list, key);
coutMap.put(key, accurNO);
//...
}
//now you have a map with keys and their frequencies in the list
Set unique = new HashSet(list);
and
Collections.frequency(list, key);
are too much overhead.
Here is how i would do it
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");
Map<String, Integer> countMap = new HashMap<>();
for (String word : list) {
Integer count = countMap.get(word);
if(count == null) {
count = 0;
}
countMap.put(word, (count.intValue()+1));
}
System.out.println(countMap.toString());
Output
{aaa=2, bbb=1}
EDIT output one by one: iterate over the set of entries of the map
for(Entry<String, Integer> entry : countMap.entrySet()) {
System.out.println("frequency of '" + entry.getKey() + "' is "
+ entry.getValue());
}
Output
frequency of 'aaa' is 2
frequency of 'bbb' is 1
EDIT 2 No need for looping
String word = null;
Integer frequency = null;
word = "aaa";
frequency = countMap.get(word);
System.out.println("frequency of '" + word + "' is " +
(frequency == null ? 0 : frequency.intValue()));
word = "bbb";
frequency = countMap.get(word);
System.out.println("frequency of '" + word + "' is " +
(frequency == null ? 0 : frequency.intValue()));
word = "foo";
frequency = countMap.get(word);
System.out.println("frequency of '" + word + "' is " +
(frequency == null ? 0 : frequency.intValue()));
Output
frequency of 'aaa' is 2
frequency of 'bbb' is 1
frequency of 'foo' is 0
Note that you will always have a collection and you need extract the count from it for a particular word one way or another.
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");
Map<String,Integer> countMap = new HashMap();
Set<String> unique = new HashSet<String>(list);
for (String key : unique) {
int accurNO = Collections.frequency(list, key);
countMap.put(key,accurNO);
System.out.println(key + ": " accurNO);
}
The Map answers work, but you can extend this answer to solve more problems.
You create a class that has the field values you need, and put the class in a List.
import java.util.ArrayList;
import java.util.List;
public class WordCount {
private String word;
private int count;
public WordCount(String word) {
this.word = word;
this.count = 0;
}
public void addCount() {
this.count++;
}
public String getWord() {
return word;
}
public int getCount() {
return count;
}
}
class AccumulateWords {
List<WordCount> list = new ArrayList<WordCount>();
public void run() {
list.add(new WordCount("aaa"));
list.add(new WordCount("bbb"));
list.add(new WordCount("ccc"));
// Check for word occurrences here
for (WordCount wordCount : list) {
int accurNO = wordCount.getCount();
System.out.println(wordCount.getWord() + ": " + accurNO);
}
}
}
I would sort the list first to avoid going thru the whole list with Collections.frequency every time. The code will be longer but much more efficient
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");
Map<String, Integer> map = new HashMap<String, Integer>();
Collections.sort(list);
String last = null;
int n = 0;
for (String w : list) {
if (w.equals(last)) {
n++;
} else {
if (last != null) {
map.put(last, n);
}
last = w;
n = 1;
}
}
map.put(last, n);
System.out.println(map);
output
{aaa=2, bbb=1}
I have two hashmaps and I would like to fill a third hashmap which keys will be the values of the first hash map and the values will be the values of the second hashmap splitted to an array.
i.e.:
hashmap1 = {1=e1, 2=e2}
hashmap2 = {10=word1-word2-word3, 20=word4-word5-word6}
the result:
hashmap3 = {e1=word1-word2-word3, e2=word4-word5-word6}
This is what I did so far:
static HashMap<Integer, String> catnamecatkeys = new HashMap<Integer, String>();
static HashMap<Integer, String> keywords = new HashMap<Integer, String>();
static HashMap<String, String> tempHash = new HashMap<String, String>();
static HashMap<String, String[]> hash = new HashMap<String, String[]>();
static String[] arr;
public static void main(String[] args) {
catnamecatkeys.put(1, "e1");
catnamecatkeys.put(2, "e2");
keywords.put(1, "word1-word2-word3");
keywords.put(2, "word4-word5-word6");
for (int key : catnamecatkeys.keySet()) {
tempHash.put(catnamecatkeys.get(key),null);
}
for(String tempkey: tempHash.keySet()){
tempHash.put(tempkey,keywords.entrySet().iterator().next().getValue());
arr = tempHash.get(tempkey).split("-");
hash.put(tempkey, arr);
}
System.out.println(tempHash);
for (String hashkey : hash.keySet()) {
for (int i = 0; i < arr.length; i++) {
System.out.println(hashkey + ":" + hash.get(hashkey)[i]);
}
}
}
but the output is:
hashmap3 = {e1=word1-word2-word3, e2=word1-word2-word3}
Any Ideas please?
You should initialize Iterator outside the loop, Here is complete example -
static HashMap<Integer, String> catnamecatkeys = new HashMap<Integer, String>();
static HashMap<Integer, String> keywords = new HashMap<Integer, String>();
static HashMap<String, String> tempHash = new HashMap<String, String>();
static HashMap<String, String[]> hash = new HashMap<String, String[]>();
static String[] arr;
public static void main(String[] agrs)
{
catnamecatkeys.put(1, "e1");
catnamecatkeys.put(2, "e2");
keywords.put(1, "word1-word2-word3");
keywords.put(2, "word4-word5-word6");
for (int key : catnamecatkeys.keySet()) {
tempHash.put(catnamecatkeys.get(key),null);
}
Set<Entry<Integer,String>> set = keywords.entrySet();
Iterator<Entry<Integer, String>> iterator= set.iterator();
for(String tempkey: tempHash.keySet()){
tempHash.put(tempkey,iterator.next().getValue());
arr = tempHash.get(tempkey).split("-");
hash.put(tempkey, arr);
}
System.out.println(tempHash);
for (String hashkey : hash.keySet()) {
for (int i = 0; i < arr.length; i++) {
System.out.println(hashkey + ":" + hash.get(hashkey)[i]);
}
}
}
Your problem is this line:
keywords.entrySet().iterator().next().getValue()
is always going to return the same entry of the keywords HashMap. Try building your new hashmap with something like:
for (int i = 1; i < 3; i++) {
tempHash.put(catnamecatkeys.get(i), keywords.get(i));
}
According to your example where you have:
hashmap1 = {1=e1, 2=e2}
hashmap2 = {10=word1-word2-word3, 20=word4-word5-word6}
the result:
hashmap3 = {e1=word1-word2-word3, e2=word4-word5-word6}
There is no common key between hashmap1 and hashmap2, so we are trying to relate the value from hashmap1 with key "1" to the value in hashmap2 with key "10". There is no way to do this unless additional information about how to map the entries from hashmap1 to hashmap2 is retained. This additional information could be the insertion order into the map if a map that guarantees iteration order to be the same as insertion order is used (e.g. LinkedHashMap).