I'm trying to split a String with a format like this :
"abc=cde,dfe=lk,f,sss=f,d,s"
I'd like to recover these values in a map by using the first set of characters as a key and the second ones as value.
For example
key: abc, value: cde
key: dfe, value: lk,f
key: sss, value: f,d,s
So splitting these values for the last occurrence of ",".
Any ideas on how to do it?
I tried with regex and Stringtokenizer but I can't manage to recover just the last occurrence of ","
You could use the following regex (could possibly be optimized):
,(?=(?:(?!,).)*=)
(see on Regex101)
This matches a , which has no subsequent , until the next =.
You need to use regex for this.
Full Code :
public class Test {
public static void main(String args[]) {
String input = "abc=cde,dfe=lk,f,sss=f,d,s";
String[] arrOfStr = input.split(",(?=(?:(?!,).)*=)");
HashMap<String, String> properties = new HashMap<String, String>();
for(int i=0;i<arrOfStr.length;i++) {
String[] temp = arrOfStr[i].split("=");
properties.put(temp[0], temp[1]);
}
System.out.println("Input String : " +input);
System.out.println("\nFinal properties : ");
properties.entrySet().forEach(entry->{
System.out.println("key = " +entry.getKey() + " :: value = " + entry.getValue());
});
}
}
Output :
Input String : abc=cde,dfe=lk,f,sss=f,d,s
Final properties :
key = dfe :: value = lk,f
key = sss :: value = f,d,s
key = abc :: value = cde
Full Code :
public class Test {
public static void main(String args[]) {
String text = "abc=cde,dfe=lk,f,sss=f,d,s";
String[] parts = text.split(",");
Map<String, String> map = new LinkedHashMap<>();
String key = null;
StringBuilder value = new StringBuilder();
for (int i = 0; i < parts.length; i++) {
if (parts[i].contains("=")) {
if (key != null) {
map.put(key, value.toString());
value.setLength(0);
}
String[] innerParts = parts[i].split("=");
key = innerParts[0];
value.append(innerParts[1]);
} else {
value.append(',').append(parts[i]);
}
}
map.put(key, value.toString());
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry);
}
}
}
Output :
abc=cde
dfe=lk,f
sss=f,d,s
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 want to split string with multiple string delimiter.
For example :
String is "abc[11]xyz[86]pqr[87]sdv[11]adf[86]fgr[87]fadggthy"
Output :
abc
xyz
pqr
sdv
adf
fgr
fadggthy
I want to split string by "[11]" , "[86]" , "[87]"
Tried following code but does not work.
void testSplit() {
StringBuilder message = new StringBuilder("abc[11]xyz[86]pqr[87]sdv[11]adf[86]fgr[87]fadggthy");
Map<String, String> replaceStringMap = new HashMap();
replaceStringMap.put("\\\\[11\\\\]", "11");
replaceStringMap.put("\\\\[86\\\\]", "86");
replaceStringMap.put("\\\\[87\\\\]", "87");
String starter = "(";
String middle = ")|(";
String end = ")";
Set<String> keySet = replaceStringMap.keySet();
boolean isFirst = true;
StringBuilder regex = new StringBuilder(starter);
Iterator<String> itr = keySet.iterator();
while(itr.hasNext()) {
String string = itr.next();
if(itr.hasNext()) {
regex.append(string);
regex.append(middle);
} else {
regex.append(string);
regex.append(end);
}
}
System.out.println(regex.toString());
String[] strings = message.toString().split(regex.toString());
for(String s : strings) {
System.out.println(s);
}
}
Output :
(\\[87\\])|(\\[11\\])|(\\[86\\])
abc[11]xyz[86]pqr[87]sdv[11]adf[86]fgr[87]fadggthy
Output:abc[11]xyz[86]pqr[87]sdv[11]adf[86]fgr[87]fadggthy
Expected Output:
abc
xyz
pqr
sdv
adf
fgr
fadggthy
Below Code works :
String regex = "(\\[87\\])|(\\[11\\])|(\\[86\\])";
Here if i hardcode regex then it works but when i generate regex dynamically by reading value from map then it does not work.
Problem is that i can't generate regex at dynamic level.
You have an extra pair of \ in your delimeters.
Given this:
StringBuilder message = new StringBuilder("abc[11]xyz[86]pqr[87]sdv[11]adf[86]fgr[87]fadggthy");
Map<String, String> replaceStringMap = new HashMap();
replaceStringMap.put("\\[11\\]", "11");
replaceStringMap.put("\\[86\\]", "86");
replaceStringMap.put("\\[87\\]", "87");
String starter = "(";
String middle = ")|(";
String end = ")";
Set<String> keySet = replaceStringMap.keySet();
boolean isFirst = true;
StringBuilder regex = new StringBuilder(starter);
Iterator<String> itr = keySet.iterator();
while (itr.hasNext()) {
String string = itr.next();
if (itr.hasNext()) {
regex.append(string);
regex.append(middle);
} else {
regex.append(string);
regex.append(end);
}
}
System.out.println(regex.toString());
String[] strings = message.toString().split(regex.toString());
for (String s : strings) {
System.out.println(s);
}
}
It yields this:
(\[86\])|(\[87\])|(\[11\])
abc
xyz
pqr
sdv
adf
fgr
fadggthy
A general solution, for any value between [] considered as separator:
String test = abc[11]xyz[86]pqr[87]sdv[11]adf[86]fgr[87]fadggthy
String r = "(\\[[^\\]]*\\])"
for(String part : test.split(r)) println(part)
> abc
> xyz
> pqr
> sdv
> adf
> fgr
> fadggthy
String str = "id1;;name1 || id2;;name2 || id3;;name3||";
into id1 name1 ...and then store it in hashmap as id1 - key , name1- value
id2 - key , name2 - value
......
One way to reach your goal is to use a StringTokenizer.
Code example:
public static void main(String[] args) {
String input = "id1;;name1 || id2;;name2 || id3;;name3||";
Map<String, String> map = new HashMap<>();
// You have to split two times
for (String outer : splitBy(input, " || ")) {
List<String> inner = splitBy(outer, ";;"); // for example: outer="id1;;name1"
// Make sure, you have two inner input-elements
if (inner.size() == 2) {
String key = inner.get(0); // First element of List = Key
String value = inner.get(1); // Second element of List = Value
map.put(key, value);
}
}
}
private static List<String> splitBy(String toSplit, String delimiter) {
List<String> tokens = new ArrayList<>();
StringTokenizer tokenizer = new StringTokenizer(toSplit, delimiter);
while (tokenizer.hasMoreTokens()) {
tokens.add(tokenizer.nextToken());
}
return tokens;
}
Also take a look at this: Scanner vs. StringTokenizer vs. String.Split
for this particular case you should do something like this:
Map<String, String> yourHashMap = new HashMap<String, String>();
String input = "id1;;name1 || id2;;name2 || id3;;name3||";
// "\" is special character so it need an escape
String[] yourStrings = input.split("\\|\\|");
String[] hashObject = new String[2];
for (int i = 0; i <= yourStrings.length - 1; i++) {
//fist you have to remove the whitespaces
yourStrings[i] = yourStrings[i].replaceAll("\\s+", "");
hashObject = yourStrings[i].split(";;");
yourHashMap.put(hashObject[0], hashObject[1]);
}
Your input string have a strange format, I recommend you to change it.