Map some names and values using java - java

I have a set of values as a repsonse like this.
from this
4,0,1581664239228,6,799,0,845,253,0,0,0,0,0,0,0,0,0,0,1448,594,0,1276257,0,0,0,0,1100,0,0,0,0,0,0,0,2047,2158,0,13,1
I have to map these values to below one..The order should be same like version: 4 , build: 0, tuneStartBaseUTCMS: 1581664239228 etc etc
version,build,tuneStartBaseUTCMS,ManifestDLStartTime,ManifestDLTotalTime,ManifestDLFailCount,VideoPlaylistDLStartTime,VideoPlaylistDLTotalTime,VideoPlaylistDLFailCount,AudioPlaylistDLStartTime,AudioPlaylistDLTotalTime,AudioPlaylistDLFailCount,VideoInitDLStartTime,VideoInitDLTotalTime,VideoInitDLFailCount,AudioInitDLStartTime,AudioInitDLTotalTime,AudioInitDLFailCount,VideoFragmentDLStartTime,VideoFragmentDLTotalTime,VideoFragmentDLFailCount,VideoBitRate,AudioFragmentDLStartTime,AudioFragmentDLTotalTime,AudioFragmentDLFailCount,AudioBitRate,drmLicenseAcqStartTime,drmLicenseAcqTotalTime,drmFailErrorCode,LicenseAcqPreProcessingDuration,LicenseAcqNetworkDuration,LicenseAcqPostProcDuration,VideoFragmentDecryptDuration,AudioFragmentDecryptDuration,gstPlayStartTime,gstFirstFrameTime,contentType,streamType,firstTune
I have written as follows...but it is not working as ex
String abcd = "4,0,1581664239228,6,799,0,845,253,0,0,0,0,0,0,0,0,0,0,1448,594,0,1276257,0,0,0,0,1100,0,0,0,0,0,0,0,2047,2158,0,13,1";
String valueName = "version,build,tuneStartBaseUTCMS,ManifestDLStartTime,ManifestDLTotalTime,ManifestDLFailCount,VideoPlaylistDLStartTime,VideoPlaylistDLTotalTime,VideoPlaylistDLFailCount,AudioPlaylistDLStartTime,AudioPlaylistDLTotalTime,AudioPlaylistDLFailCount,VideoInitDLStartTime,VideoInitDLTotalTime,VideoInitDLFailCount,AudioInitDLStartTime,AudioInitDLTotalTime,AudioInitDLFailCount,VideoFragmentDLStartTime,VideoFragmentDLTotalTime,VideoFragmentDLFailCount,VideoBitRate,AudioFragmentDLStartTime,AudioFragmentDLTotalTime,AudioFragmentDLFailCount,AudioBitRate,drmLicenseAcqStartTime,drmLicenseAcqTotalTime,drmFailErrorCode,LicenseAcqPreProcessingDuration,LicenseAcqNetworkDuration,LicenseAcqPostProcDuration,VideoFragmentDecryptDuration,AudioFragmentDecryptDuration,gstPlayStartTime,gstFirstFrameTime,contentType,streamType,firstTune";
String[] valueArr = abcd.split(",");
String[] valueNameArr = valueName.split(",");
List<String> valueList = Arrays.asList(valueArr);
List<String> valueNameList = Arrays.asList(valueNameArr);
System.out.println(valueList.size() + "jjj: " + "valueNameList::: " + valueNameList.size());
LinkedHashMap<String, String> result = new LinkedHashMap<String, String>();
for (String name : valueNameList) {
System.out.println("name: " + name);
for (String value : valueList) {
System.out.println("value: " + value);
result.put(name, value);
}
}
System.out.println("RESULT::::::::::::::::::::::::::::" + result);
Result prints:
{version=1, build=1, tuneStartBaseUTCMS=1, ManifestDLStartTime=1, ManifestDLTotalTime=1, ManifestDLFailCount=1, VideoPlaylistDLStartTime=1, VideoPlaylistDLTotalTime=1, VideoPlaylistDLFailCount=1, AudioPlaylistDLStartTime=1, AudioPlaylistDLTotalTime=1, AudioPlaylistDLFailCount=1, VideoInitDLStartTime=1, VideoInitDLTotalTime=1, VideoInitDLFailCount=1, AudioInitDLStartTime=1, AudioInitDLTotalTime=1, AudioInitDLFailCount=1, VideoFragmentDLStartTime=1, VideoFragmentDLTotalTime=1, VideoFragmentDLFailCount=1, VideoBitRate=1, AudioFragmentDLStartTime=1, AudioFragmentDLTotalTime=1, AudioFragmentDLFailCount=1, AudioBitRate=1, drmLicenseAcqStartTime=1, drmLicenseAcqTotalTime=1, drmFailErrorCode=1, LicenseAcqPreProcessingDuration=1, LicenseAcqNetworkDuration=1, LicenseAcqPostProcDuration=1, VideoFragmentDecryptDuration=1, AudioFragmentDecryptDuration=1, gstPlayStartTime=1, gstFirstFrameTime=1, contentType=1, streamType=1, firstTune=1}

Your loop is wrong
Try this
for(int i = 0; i < valueList.size(); i++){
result.put(valueNameList(i), valueList(i));
}

Is there not supposed to be a one-to-one relationship between abcd values and valueName ? If there is one-to-one, then an inner loop is wrong isn't it.
String abcd = "4,0,1581664239228,6,799,0,845,253,0,0,0,0,0,0,0,0,0,0,1448,594,0,1276257,0,0,0,0,1100,0,0,0,0,0,0,0,2047,2158,0,13,1";
String valueName = "version,build,tuneStartBaseUTCMS,ManifestDLStartTime,ManifestDLTotalTime,ManifestDLFailCount,VideoPlaylistDLStartTime,VideoPlaylistDLTotalTime,VideoPlaylistDLFailCount,AudioPlaylistDLStartTime,AudioPlaylistDLTotalTime,AudioPlaylistDLFailCount,VideoInitDLStartTime,VideoInitDLTotalTime,VideoInitDLFailCount,AudioInitDLStartTime,AudioInitDLTotalTime,AudioInitDLFailCount,VideoFragmentDLStartTime,VideoFragmentDLTotalTime,VideoFragmentDLFailCount,VideoBitRate,AudioFragmentDLStartTime,AudioFragmentDLTotalTime,AudioFragmentDLFailCount,AudioBitRate,drmLicenseAcqStartTime,drmLicenseAcqTotalTime,drmFailErrorCode,LicenseAcqPreProcessingDuration,LicenseAcqNetworkDuration,LicenseAcqPostProcDuration,VideoFragmentDecryptDuration,AudioFragmentDecryptDuration,gstPlayStartTime,gstFirstFrameTime,contentType,streamType,firstTune";
String[] list1 = abcd.split(",");
String[] list2 = valueName.split(",");
if (list1.length == list2.length) {
for (int x = 0; x < list1.length; x++) {
System.out.println(list2[x] + ":" + list1[x]);
}
}
Simply split and iterate
result
version:4
build:0
tuneStartBaseUTCMS:1581664239228
ManifestDLStartTime:6
ManifestDLTotalTime:799
ManifestDLFailCount:0
VideoPlaylistDLStartTime:845
VideoPlaylistDLTotalTime:253
VideoPlaylistDLFailCount:0
AudioPlaylistDLStartTime:0
AudioPlaylistDLTotalTime:0
AudioPlaylistDLFailCount:0
VideoInitDLStartTime:0
VideoInitDLTotalTime:0
VideoInitDLFailCount:0
AudioInitDLStartTime:0
AudioInitDLTotalTime:0
AudioInitDLFailCount:0
VideoFragmentDLStartTime:1448
VideoFragmentDLTotalTime:594
VideoFragmentDLFailCount:0
VideoBitRate:1276257
AudioFragmentDLStartTime:0
AudioFragmentDLTotalTime:0
AudioFragmentDLFailCount:0
AudioBitRate:0
drmLicenseAcqStartTime:1100
drmLicenseAcqTotalTime:0
drmFailErrorCode:0
LicenseAcqPreProcessingDuration:0
LicenseAcqNetworkDuration:0
LicenseAcqPostProcDuration:0
VideoFragmentDecryptDuration:0
AudioFragmentDecryptDuration:0
gstPlayStartTime:2047
gstFirstFrameTime:2158
contentType:0
streamType:13
firstTune:1

Related

How to Count Repetition of Words in Array List?

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}

Replace strings in a long String:

I have the following string:
Where Are You [Employee Name]?
your have a [Shift] shift...
and a list of strings that contains:
1. Employee Name
2. Shift
I need to find the given strings in the list in the long string and replace them with another content (including the [ and ] characters).
So for example the first string is need to be change to:
Where Are You Jhon Green?
your have a morning shift...
Is there any simple way to do that? using IndexOf will give me the location of this string but how would I include the [ , ] charecters as well?
UPDATE:
This is the code I tested so far:
Scanner sc = new Scanner(smsText);
for (String s; (s = sc.findWithinHorizon("(?<=\\[).*?(?=\\])", 0)) != null;)
{
words.add(s);
}
for (int j = 0; j < words.size(); j++)
{
Log.d(TAG, "The value for column: "+words.get(j) +" is: "+ rowData.getValue(words.get(j)));
smsText.replaceFirst("\\[" + words.get(j) + "\\]", rowData.getValue(words.get(j)));
}
Log.d(TAG, "Final String is: "+ smsText);
which is not giving me the right result, the string are not replaced.
UPDATE2:
The solution that worked for me is:
Scanner sc = new Scanner(smsText);
for (String s; (s = sc.findWithinHorizon("(?<=\\[).*?(?=\\])", 0)) != null;)
{
columnNames.add(s);
}
for (int j = 0; j < columnNames.size(); j++)
{
Log.d(TAG, "The value for column: "+columnNames.get(j) +" is: "+ rowData.getValue(columnNames.get(j)));
smsText = smsText.replaceFirst("\\[" + columnNames.get(j) + "\\]", rowData.getValue(columnNames.get(j)));
}
Log.d(TAG, "Final String is: "+ smsText);
Thanks to all for your help.
String key = myColumns.getName();
s.replaceFirst("\\[" + key + "\\]", myReplacements.getReplacement(key));
You could also use indexOf, but with a replace function it's immediately clear what you're trying to do.
try this
String s = "Where Are You [Employee Name]? your have a [Shift] shift..";
Map<String, String> replacementMap = new HashMap<>();
replacementMap.put("[Employee Name]", "John Green");
replacementMap.put("[Shift]", "morning");
for(Entry<String, String> e : replacementMap.entrySet()) {
s = s.replace(e.getKey(), e.getValue());
}
System.out.println(s);
output
Where Are You John Green? your have a morning shift..
A general solution could look something like this:
String message = "Where are you [Employee Name]? You have a [Shift] shift!";
Map<String, String> variables = new HashMap<>();
variables.put("Employee Name", "John Green");
variables.put("Shift", "morning");
StringBuffer endResult = new StringBuffer();
Matcher m = Pattern.compile("\\[(.*?)\\]").matcher(message);
while (m.find()) {
m.appendReplacement(endResult, variables.get(m.group(1)));
}
m.appendTail(endResult);
System.out.println(endResult.toString());
i know regex is there but if you want to go for recursive function here it is
public string replaceString(string str, string[] values, int index)
{
if (str.IndexOf('[') == -1 || str.IndexOf(']') == -1 || index > values.Length-1)
return str;
else
return replaceString(str.Replace(str.Substring(str.IndexOf('['), (str.IndexOf(']') - str.IndexOf('['))+1), values[index]), values, ++index);
}
calling this method
string strforreplac = "Where Are You [Employee Name]? your have a [Shift] shift...]";
string[] strvalues = {"Emil","morning"};
string newstring = replaceString(strforreplac,strvalues,0);

Remove Data and Arraylist looping

I have a object with 3 variable (id(string), year(int), pay(double))
I have created an arraylist that contains object.
So now I need to sum the the pay if they have the same id and year and store it in a new array! is that possible?
ArrayList<Earning> temp = new ArrayList();
ArrayList<Earning> temp = new ArrayList();
double tempEarning = 0.0;
int count = 0;
for (int i = 0; i < weeklyEarnings.size(); i++) {
Earning e = weeklyEarnings.get(i);
String id = e.getId();
int year = e.getYear();
tempEarning = e.getEarning();
Earning e2 = weeklyEarnings.get(i + 1);
if (id.equalsIgnoreCase(e2.getId()) && year == e2.getYear()) {
tempEarning += e2.getEarning();
} else {
Earning tempEarn = new Earning();
tempEarn.setEarning(tempEarning);
tempEarn.setId(id);
tempEarn.setYear(year);
temp.add(tempEarn);
count++;
tempEarning = 0.0;
}
}
weeklyEarnings.clear();
weeklyEarnings = temp;
temp.clear();
Can someone Help me? Thanks a lot!
Try this:
HashMap<String, Earning> totalEarnings = new HashMap<String, Earning>();
for (Earning earning : weeklyEarnings) {
Earning tmpEarning = totalEarnings.get(earning.getId() + earning.getYear());
if (tmpEarning == null) {
tmpEarning = new Earning();
tmpEarning.setId(earning.getId());
tmpEarning.setYear(earning.getYear());
totalEarnings.put(earning.getId() + earning.getYear(), tmpEarning);
}
tmpEarning.setEarning(tmpEarning.getEarning() + earning.getEarning());
}
for (Earning earning : totalEarnings.values())
{
System.out.println(earning.getId() + ' ' + earning.getyear() + ' ' + earning.getEarning());
}
You can do it like this.
Iterate through the list.
Match each item in the list with one id.
If match found "Sum Pay".
Continue for each item in the list.

Add to list at certain index

I'm having a problem with some list manipulation. I take the user's input and search through it: if i find an "=" sign i assume that the string in front of it is the name of a variable , so on the line right above that variable i want to add a new string to the user's input (in this case it is called "tempVAR", doesn't really matter though). I've been trying to do this with StringBuilder but without any success , so i currently am trying to do it with ArrayLists but I am getting stuck at adding new elements to the list. Because of the way list.add(index,string) works , the elements to the right of what i am adding will always add +1 to their index. Is there a way to always know exactly what index i am looking for even after a random number of string has been added? Here is my code so far, if you run it you will see what i mean, instead of "tempVAR" or "tempVar1" being added above the name of the variable they will be added one or to positions in the wrong way.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
public class ToTestStuff {
static List<String> referenceList = new ArrayList<String>();
public static final String SEMICOLUMN = ";";
public static final String BLANK = " ";
public static final String EMPTY = "";
public static final String LEFT_CURLY = "{";
public static final char CARRIAGE_RETURN = '\r';
public static final String CR_STRING = "CARRIAGE_RETURN_AND_NEW_LINE";
public static final char NEW_LINE = '\n';
public static void main(String[] args) {
List<String> test = new ArrayList<String>();
String x = "AGE_X";
String y = "AGE_Y";
String z = "AGE_YEARS";
String t = "P_PERIOD";
String w = "T_VALID";
referenceList.add(x);
referenceList.add(y);
referenceList.add(z);
referenceList.add(t);
referenceList.add(w);
String text2 = " if ( AGE_YEARS > 35 ) {\r\n"
+ " varX = P_PERIOD ;\r\n"
+ " }\r\n"
+ " if ( AGE_YEARS < 35 ) {\r\n"
+ " varY = T_VALID ;\r\n"
+ " varZ = AGE_Y ;\r\n"
+ " varA = AGE_X ;\r\n"
+ " }";
detectEquals(text2);
}
public static String detectEquals(String text) {
String a = null;
// text = text.trim();
// text = TestSplitting.addDelimiters(text);
String[] newString = text.split(" ");
List<String> test = Arrays.asList(newString);
StringBuilder strBuilder = new StringBuilder();
HashMap<String, List<Integer>> signs = new HashMap<String, List<Integer>>();
HashMap<String, List<Integer>> references = new HashMap<String, List<Integer>>();
HashMap<Integer, Integer> indexesOfStringAndList = new HashMap<Integer, Integer>();
List<String> testList = new ArrayList<String>();
List<Integer> lastList = new ArrayList<Integer>();
List<Integer> indexList = new ArrayList<Integer>();
List<Integer> refList = new ArrayList<Integer>();
List<String> keysList = new ArrayList<String>();
List<List> minList = new ArrayList<List>();
String previous = null;
int index = 0;
Object obj = new Object();
List<Integer> referenceValueList = new ArrayList<Integer>();
List<Integer> indexPosition = new ArrayList<Integer>();
String b = null;
int indexOfa = 0;
// System.out.println("a----> " + test);
List<String> anotherList = new ArrayList(test);
for (int i = 0; i < anotherList.size(); i++) {
a = anotherList.get(i).trim();
index = strBuilder.length();// - a.length();
// index = i;
strBuilder.append(a); // "=", 3 - if, 14 - while, 36 , "=", 15
testList.add(a);
if (a.equals("if") || a.equals("=")) {
lastList.add(i);
indexOfa = i;
indexesOfStringAndList.put(index, indexOfa);
refList.add(index);
indexPosition.add(index);
if (signs.containsKey(a)) {
signs.get(a).add(index);
} else {
signs.put(a, refList);
}
refList = new ArrayList<Integer>();
}
if (referenceList.contains(a)) {
indexList.add(index);
if (references.containsKey(a)) {
references.get(a).add(index);
} else {
references.put(a, indexList);
}
indexList = new ArrayList<Integer>();
}
}
for (String k : references.keySet()) {
keysList.add(k);
referenceValueList = references.get(k);
obj = Collections.min(referenceValueList);
int is = (Integer) obj;
ArrayList xx = new ArrayList();
xx.add(new Integer(is));
xx.add(k);
minList.add(xx);
}
for (List q : minList) {
Integer v = (Integer) q.get(0);
String ref = (String) q.get(1);
int x = closest(v, indexPosition);
int lSize = anotherList.size();
int sizeVar = lSize - test.size();
int indexOfPx = 0;
int px = 0;
if (x != 0) {
px = indexesOfStringAndList.get(x) - 1;
} else {
px = indexesOfStringAndList.get(x);
}
if (px == 0) {
System.out.println("previous when x=0 " +anotherList.get(px+sizeVar));
anotherList.add(px, "tempVar1=\r\n");
} else {
previous = anotherList.get(px + sizeVar);
System.out.println("previous is---> " + previous + " at position " + anotherList.indexOf(previous));
anotherList.add(anotherList.indexOf(previous) - 1, "\r\ntempVAR=");
}
}
strBuilder.setLength(0);
for (int j = 0; j < anotherList.size(); j++) {
b = anotherList.get(j);
strBuilder.append(b);
}
String stream = strBuilder.toString();
// stream = stream.replaceAll(CR_STRING, CARRIAGE_RETURN + EMPTY + NEW_LINE);
System.out.println("after ----> " + stream);
return stream;
}
public static int closest(int of, List<Integer> in) {
int min = Integer.MAX_VALUE;
int closest = of;
for (int v : in) {
final int diff = Math.abs(v - of);
if (diff < min) {
min = diff;
closest = v;
}
}
return closest;
}
}
I've mapped the positions of the "=" and "if" to their positions in the StringBuilder, but these are remnants from when i was trying to use a stringBuilder to do what i said above.
I have been struggling with this for a few days now and still haven't managed to do what i need, i am not sure where i am going wrong. At the moment i am hellbent on making this work as it is (with either lists or string builder) after which , if there is a better way i will look into that and adapt this accordingly.
The addDelimiters() method is a method i created to avoid writing the string as you see it in "String text2" but i took that out for this because it would only clutter my already chaotic code even more :), i don't think it has any relevance to why what i am trying to do is not working.
TLDR: at the line above front of every varX or varY or other "var" i would like to be able to add a string to the list but i think my logic in getting the variable names or in adding to the list is wrong.
I think we both know that your code is messed up and that you need many more abstractions to make it better. But you could make it work by maintaining an offset variable, lets say "int offset". Each time you insert a string after the initial pass you increment it, and when you access the list you use it, "list.get(index+offset);". Read up on Abstract syntax trees. , which are a great way to parse and manipulate languages.

Java:how to group similar strings (items) to respective array (group)?

I have the following string "0#Aitem, 0#Aitem2, 0#Aitem3, 1#Bitem, 1#Bitem2, 2#Citem, Nitem, Nitem2".
the 0# shows group number. so Aitem, Aitem2, Aitem3 will belong to group 0. Bitem, Bitem2 in group 1. Citem in group 2. If there is no group number, they will all be place in separate group. So Nitem, Nitem2 will be placed in group 3.
I would like to create an array for each group, and place the "items" in respective group(array). So I would end up with something like
[array("Aitem,Aitem2,Aitem3"), array("Bitem, Bitem2"), array("Citem"), array("Nitem, Nitem2")]
I am guessing I need an arrayList to hold all the groups (arrays) which respectively has appropriate elements (items).
This is what I started with but I don't know if this is the best approach. The string is dynamic, so there can be any number of groups and has to follow the criteria above.
String[] x = Pattern.compile(",").split("0#item, 0#item2, 0#item3, 1#item, 1#item2, 2#item, item");
for (int ii=0; ii<x.length; ii++) {
System.out.println(i + " \"" + x[ii] + "\"");
}
My answer shows how you can use a single regex to extract both the group and the item. You can then store these in a map.
String s = "0#Aitem, 0#Aitem2, 0#Aitem3, 1#Bitem, 1#Bitem2, 2#Citem, Nitem, Nitem2";
Pattern p = Pattern.compile("(\\d*)[#]{0,1}(\\w+?)(,|$)");
Matcher m = p.matcher(s);
Map<String, List<String>> map = new TreeMap<String, List<String>>();
while(m.find()){
String group = m.group(1);
String item = m.group(2);
List<String> items = map.get(group);
if(items == null){
items = new ArrayList<String>();
map.put(group, items);
}
items.add(item);
}
//let's print it out
for(String key : map.keySet()){
System.out.println(key + " : " + map.get(key));
}
prints:
: [Nitem, Nitem2]
0 : [Aitem, Aitem2, Aitem3]
1 : [Bitem, Bitem2]
2 : [Citem]
At the moment, items with no group are keyed against an empty string. I'll leave it at as an exercise for you to handle this scenario. It should simply be a case of finding the max key and incrementing it.
I'm sure the regex can be improved too as it was written in haste.
//Create a map for storing groups
Map<String, Collection<String>> groupMap = new HashMap<String, Collection<String>>();
String[] parts = yourString.split("[, ]+"); //Split by each word
for (String part : parts) { //Go over all words
String[] subparts = part.split("#"); //Split to index and value
String groupKey;
String value;
if (subparts.length == 1) { //There is no '#' sign
groupKey = null;
value = subparts[0];
} else if (subparts.length == 2) { //There is one '#'sign
groupKey = subparts[0];
value = subparts[1];
} else {
throw new IllegalArgumentException("Can not parse string");
}
Collection<String> groupContents = groupMap.get(groupKey); //Extract list of items in this group
if (groupContents == null) { //If there was no such group yet - create one
groupMap.put(groupKey, groupContents = new ArrayList<String>());
}
groupContents.add(value); //Add item to group
}
HashMap<String,List<String> myItems = new HashMap<String,List<String>);
Then you can whatever class you want with an sting alias and store as many items as you whish.
You are on right way but I'd like to correct you a little bit.
Use the following regex to split: str.split("\\s*,\\s*"). This will support all possible spaces.
When you get separate item you have to split it again: item.split("#").
To store all this create data structure like List<List<String>>. Then do the following:
String[] parts = item.split("#");
int group = Integer.parseInt(parts[0]);
String itemName = parts[1];
List<String> groupList = allGroups.get(group);
if (groupList == null) {
groupList = new ArrayList<String>();
allGroups[group] = groupList;
}
groupList.add(itemName);
I am sorry if the code sample contains syntax errors. It is written here, on the site and should just help you to see the idea.
Another solution based on Maps.
System.out.println("###Parsing results and populating map");
String[] x = Pattern.compile("\\s*,\\s*").split(
"0#item, 0#item2, 0#item3, 1#item, 1#item2, 2#item, item");
Map<String, List<String>> result = new TreeMap<String, List<String>>();
for (int i = 0; i < x.length; i++) {
String y[] = x[i].split("#");
if (y.length > 1) {
System.out.println("group: '" + y[0] + "' item: '" + y[1] + "'");
List<String> l = result.get(y[0]);
if(l==null){
l = new ArrayList<String>();
result.put(y[0], l);
}
l.add(y[1]);
}
}
System.out.println("###Returning values stored in map, as separate arrays");
for(Entry<String,List<String>> entry: result.entrySet()){
System.out.println("Group:" + entry.getKey());
//System.out.println("Items:");
List <String> l = entry.getValue();
// This is where the final array is
String[] finalArray = l.toArray(new String[1]);
for (int i = 0; i < finalArray.length; i++) {
System.out.println(finalArray[i]);
}
}

Categories