I have two ArrayList sourceMessageList and TargetMessageList. I need to compare both the message list data.
Now lets say List1 - 100 Records. List2 - 1000 records
From List1- 1st record is compared with each record in list2 and then List1- 2nd record is compared with each record in list2.
But list2 is getting the value hasNext() to true for 1st source data in list1.
private void compareMessageList(ArrayList<String> source_messageList, ArrayList<String> target_messageList)
throws Exception {
// TODO Auto-generated method stub
Iterator<String> sourceMessageIterator = source_messageList.iterator();
Iterator<String> targetMessageIterator = null;
while (sourceMessageIterator.hasNext()) {
String sourceMessage = (String) sourceMessageIterator.next();
targetMessageIterator = target_messageList.iterator();
while (targetMessageIterator.hasNext()) {
String targetMessage = (String) targetMessageIterator.next();
if (getCorpValue(sourceMessage).equalsIgnoreCase(getCorpValue(targetMessage))) {
assertXMLEquals(convertSwiftMessageToXML(sourceMessage), convertSwiftMessageToXML(targetMessage));
}
}
}
if (buffer.toString().length() > 0) {
writeDifferenceTofile(buffer.toString());
buffer.delete(0, buffer.length());
throw new CatsException("There are some differences in the files.");
}
System.out.println("Exiting now ...");
}
The above code is taking too much time to execute.
To speed things up:
HashMap<String, String> lowers = new HashMap<String, String>();
for (String source : source_messageList) {
lowers.put(getCorpValue(source).toLowerCase(), source);
}
for (String target : target_messageList) {
final String corpTarget = getCorpValue(target).toLowerCase();
if(lowers.containsKey(corpTarget)) {
assertXMLEquals(
convertSwiftMessageToXML(lowers.get(corpTarget)),
convertSwiftMessageToXML(target)
);
}
}
Related
The task is to read the given file and return list of full names. I've separated the lines successfully and should be able to get both first and last names, but I'm a bit confused about how should I do that.
How am I able to get full names from readData()?
What I'm looking for is this output ["Alice Smith", "Bob Brown", "Carol White", "David Doe"] and not duplicated names.
My code looks like this so far:
public class GradeRepository {
public GradeRepository(){
readData();
}
public void readData() {
for (String line : readLines()) {
String[] parts = line.split("\\|");
String firstName = parts[0];
String lastName = parts[1];
String subject = parts[2];
String grade = parts[3];
System.out.println(firstName);
System.out.println(lastName);
System.out.println(subject);
System.out.println(grade);
System.out.println(Arrays.toString(parts));
}
}
public List<String> getFullNames() {
List<String> fullNames = new ArrayList<>();
return fullNames;
}
private List<String> readLines() {
try {
return Files.readAllLines(Paths.get("src/ex1/grades.txt"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Given text file:
grades.txt
Alice|Smith|math|5
Bob|Brown|english|4
David|Doe|math|3
Bob|Brown|math|4
Bob|Brown|chemistry|5
Alice|Smith|english|4
Carol|White|chemistry|3
David|Doe|chemistry|4
readData needs to be modified either to return a list of String[] where each string array represents a line or a field List<String[]> data needs to be created in GradeRepository and populated in readData.
Next, to get rid of duplicate names a Set<String> should be used as suggested in the comments, and LinkedHashSet implementation allows to keep the insertion order.
Example implementation where readData returns a list:
public List<String[]> readData() {
List<String[]> data = new ArrayList<>();
for (String line : readLines()) {
String[] parts = line.split("\\|");
// ... print parts as above if necessary...
data.add(parts);
}
return data;
}
public Set<String> getFullNames() {
Set<String> fullNames = new LinkedHashSet<>();
for (String[] row : readData()) {
fullNames.add(row[0] + " " + row[1]);
}
return fullNames;
}
It may be more preferable to use Stream API to avoid creation of intermediate collections, so all these methods may be rewritten into one:
public Set<String> getFullNames() throws Exception {
return Files.lines(Path.of("dataset.txt")) // Stream<String>
.map(line -> line.split("\\|")) // Stream<String[]>
.filter(arr -> arr.length > 1) // ensure there are 2 columns at least
.map(arr -> arr[0] + " " + arr[1]) // Stream<String>
.collect(Collectors.toCollection(LinkedHashSet::new)); // get collection of unique names
}
I'm working with Google Spreadsheets Api and Android Studio. I'm reading info from a sheet, and showing the results into a List View. My problem is that I can only retrieve the info from the first row, but not from the next ones. How can I do that?
I have the next code:
private class MakeRequestTask extends AsyncTask<Void, Void, List<String>> {
private Exception mLastError = null;
MakeRequestTask(GoogleAccountCredential credential) {
}
#Override
protected List<String> doInBackground(Void... params) {
try {
return getDataFromApi();
} catch (Exception e) {
mLastError = e;
cancel(true);
return null;
}
}
private List<String> getDataFromApi() throws IOException {
String range = "Sheet1!A2:H";
List<String> results = new ArrayList<String>();
ValueRange response = mService.spreadsheets().values()
.get(spreadsheet_id, range)
.execute();
List<List<Object>> values = response.getValues();
if (values == null) {
//No me deja mostrar toast aqui
} else {
for (List row : values) {
row.get(0);
row.get(1);
row.get(2);
row.get(5);
System.out.println("resultados:" + row.get(0) + ", " + row.get(1));
}
}
return results;
}
Try to check the sample code on Reading multiples ranges
To read multiple discontinuous ranges, use a spreadsheets.values.batchGet, which lets you specify any number of ranges to retrieve:
Here is a sample Java code:
List<String> ranges = Arrays.asList(
//Range names ...
);
BatchGetValuesResponse result = service.spreadsheets().values().batchGet(spreadsheetId)
.setRanges(ranges).execute();
Another thing is try to replace the for loop to:
for (List row : values) {
// Print columns A and E, which correspond to indices 0 and 4.
System.out.printf("%s, %s\n", row.get(0), row.get(4));
}
You can specify the indices you needed. In the example index 0 to 4.
Hope this is what you are looking for.
I've this kind of String:
{aa=bbbb, cc=blabla1, ee=ffff, cc=blabla2, gg=hhhh, cc=blabla3,.......}
and i want to get a list of all words after cc=.
How can i do it? I'm not very confident with regex stuff.
public static void main(String[] args) {
String input = "aa=bbbb, cc=blabla1, ee=ffff, cc=blabla2, gg=hhhh, cc=blabla3";
String[] splitValues = input.split(", ");
Map<String,List<String>> results = new Hashtable<>();
List<String> valueList = null;
// iterate through each key=value adding to the results
for (String a : splitValues) {
// a = "aa=bbbb" etc
String[] keyValues = a.split("=");
// you can check if values exist. This assumes they do.
String key = keyValues[0];
String value = keyValues[1];
// if it is already in map, add to its value list
if (results.containsKey(key)) {
valueList = results.get(key);
valueList.add(value);
} else {
valueList = new ArrayList<>();
valueList.add(value);
results.put(key, valueList);
}
}
System.out.println("cc= values");
valueList = results.get("cc");
// assumes value is in results
for (String a : valueList)
System.out.println(a);
}
Your question is very vague but I am guessing the String is provided as is, like:
String toSearch = "{aa=bbbb, cc=blabla1, ee=ffff, cc=blabla2, gg=hhhh, cc=blabla3,.......}";
By list I am guessing you are referring to the abstract List object and not to an array. Here is a solution:
String toSearch = "{aa=bbbb, cc=blabla1, ee=ffff, cc=blabla2, gg=hhhh, cc=blabla3,.......}";
List<String> result = new ArrayList<String>();
int prevMatch = 0;
while (toSearch.indexOf("cc=", prevMatch+1) != -1) {
result.add(toSearch.substring( // Substring method.
toSearch.indexOf("cc=",prevMatch+1)+3,toSearch.indexOf(",") //Getting correct indexes.
));
prevMatch = toSearch.indexOf("cc=",prevMatch+1);
}
The prevMatch variable ensures that the indexOf("cc=") that will be returned will be the next one occurring in the String. For the above String the returning ArrayList will contain the words "blabla1","blabla2", "blabla3" and whatever else is encountered.
If we have input file which contains the pair of state and city, there can be multiple cities which belongs to same state. What we have to do is we have to make that single state as key and the cities which belongs to that state as value.
For example, I am reading the following data from a file:
Maharashtra - Pune
Madhyapradesh - Bhopal
Maharashtra - Mumbai
Maharashtra - Nagpur
Here Maharashtra will become a key, with Pune, Mumbai and Nagpur becoming values. What I did is first I split The data into state and city. I am now trying to store the states in a list and then check the list but I am stuck.
How can I make the Maharashtra as key and Pune, Mumbai and Nagpur as its respective values? Like this:
Maharashtra- Pune, Mumbai, Nagpur.
This is what I have so far:
public class DataManagerImpl implements DataManager {
#Override
public Map<String, List<String>> populateCityDataMap(String fileName)
throws FileNotFoundException {
// TODO Auto-generated method stub
Map<String, List<String>> map = new HashMap<String, List<String>>();
List<String> valSetOne = new ArrayList<String>();
List<String> list=null;
String nameAndRollNumber=null;
String[] nameAndRollNumbers =null;
String State=null;
Scanner s = null;
try {
s = new Scanner(new File("F:\\Participant_Workspace\\Q4\\CityStateLocator\\StateCityDetails.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (s.hasNext()) {
nameAndRollNumber = s.nextLine();
nameAndRollNumbers = nameAndRollNumber.split("-");
State = nameAndRollNumbers[0];
String City=nameAndRollNumbers[1];
/*System.out.println(valSetOne);
map.put(State,valSetOne);*/
System.out.println(State+" "+City);
list.add(State);
}
/*Iterator<String> CrunchifyIterator = list.iterator();
while (CrunchifyIterator.hasNext()) {
System.out.println(CrunchifyIterator.next());
}*/
System.out.println(list);
return null;
}
}
You need something like map with key as state (which is a String) and value as list of cities (i.e. list of strings). So your data structure should be something like:
Map<String, List<String>> map ...
List<String> cities = map.get(state);
if (cities == null) {
cities = new ArrayList<String>();
map.put(state, cities);
}
cities.add(city);
I need help for this case below :
I have 2 method :
private void calculateTime(Map.Entry<List<String>, List<LogRecord>> entry, List<LogProcess> processList) {
List<List<LogRecord>> processSpentTime = new ArrayList<List<LogRecord>>();
processSpentTime = subListProcess(entry, processSpentTime);
for (List<LogRecord> item : processSpentTime) {
processList = parse(item, DEFAULT_START_LEVEL);
}
}
and the second method
private List<LogProcess> parse(List<LogRecord> recordList, int level) {
List<LogProcess> processList = new ArrayList<LogProcess>();
if(!recordList.isEmpty()) {
LogProcess process = findProcess(recordList, level);
if(!(process instanceof NullLogProcess)) {
if(!(process instanceof IncompleteLogProcess)) {
processList.add(process);
}
int fromIndex = recordList.indexOf(process.returnStartIndexOfNextProcess()) + 1;
processList.addAll(parse(recordList.subList(fromIndex, recordList.size()), level));
}
}
return processList;
}
public LogProcess findProcess(List<LogRecord> recordList, int level) {
LogRecord endRecord = null;
LogRecord startRecord = findStartRecord(recordList);
if(startRecord instanceof NullLogRecord) {
return new NullLogProcess();
}
List<LogRecord> startEndRecord = findStartEndRecord(startRecord, recordList);
startRecord = startEndRecord.get(0);
endRecord = startEndRecord.get(1);
LogProcess process = returnLogProcess(startRecord, endRecord);
process.setLevel(level);
process.setChildren(findChildProcess(recordList, startRecord, endRecord, level + 1));
return process;
}
private List<LogProcess> findChildProcess(List<LogRecord> recordList, LogRecord startRecord, LogRecord endRecord, int level) {
int fromIndex = recordList.indexOf(startRecord) + 1;
int toIndex = recordList.indexOf(endRecord);
if(toIndex > fromIndex) {
List<LogRecord> recordSubList = recordList.subList(fromIndex, toIndex);
return parse(recordSubList, level);
} else {
return new ArrayList<LogProcess>();
}
}
private List<LogRecord> findStartEndRecord(LogRecord startRecord, List<LogRecord> recordList) {
List<LogRecord> startEndRecord = new ArrayList<LogRecord>();
if (!recordList.isEmpty()) {
startEndRecord.add(startRecord);
for (LogRecord record : recordList) {
boolean isStartRecord = record.isStartPoint() && record.hasSameActionName(startRecord);
if(isStartRecord){
startEndRecord = new ArrayList<LogRecord>();;
startEndRecord.add(record);
continue;
}
boolean isEndRecord = record.isEndPoint() && record.hasSameActionName(startRecord);
if (isEndRecord) {
startEndRecord.add(record);
return startEndRecord;
}
}
return startEndRecord;
}
return startEndRecord;
}
private LogRecord findStartRecord(List<LogRecord> recordList) {
for (LogRecord record : recordList) {
if (record.isStartPoint()){
recordList.remove(record);
return record;
}
}
return new NullLogRecord();
}
at the method calculatime in the for loop I just get the result for the first item, and after that I got error the same the title . please help me and explain me more for this case .
The name of this exception is a bit confusing, because it isn't related to multi threading.
What happens is that you are iterating over a collection which is being modified while you are iterating over it.
If performance is not your highest concern, a simple way out would be to copy the list and iterate over that copy and add items to the original list.
My guess is it's related to recordList.subList():
Returns a view of the portion of this list. [..] The returned list is backed by this list. [..] The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. [..] All methods first check to see if the actual modCount of the backing list is equal to its expected value, and throw a ConcurrentModificationException if it is not.
I don't see any modification, so it probably happens in findProcess(). Consider creating a copy of that list:
new ArrayList(recordList.subList())
You are getting the exception because of this :
for (LogRecord record : recordList) {
if (record.isStartPoint()){
recordList.remove(record); <--- This is the cause
return record;
}
}
Use an Iterator Instead
Iterator<LogRecord> iterator = recordList.iterator();
while(iterator.hasNext()){
LogRecord logRecord = iterator.next();
if(record.isStartPoint()){
iterator.remove();
return logRecord;
}
Check if this works