Saturday, August 16, 2014

Don't know how to explain this
































































































































































































































































































































































































































































































































Hello. I've made a program that reads a chat log on a website, and would like to fetch only the latest 3 messages that have NOT been read by the program yet. What I have at the moment:
































































































































































































































































































































































































































































































































































































































































































































































































Java Code:









































































































































































































































































































































































































































































































































String[] lines = data.split(System.getProperty("line.separator"));
Set<String> current = new HashSet<>();

for (int i = 3; i < 6; i++) {
if (lines.length < i)
return current;

if (current.contains(lines[i]))
continue;

current.add(lines[i]);
}







































































































































































































































































































































































































































































































































Here's the 3 latest lines. Now before I add the line to the 'current' HashSet, I would like to somehow check that the line was not in the previous 3 lines. Basically I don't want to see duplicated lines.































































































































































































































































This method is called whether there are new lines or not, so simply creating a List/Set of the 3 previous messages will not work.
































































































































































































































































































































































































































































































































How would I go about doing this?
































































































































































































































































































































































































































































































































Edit: came up with a rather simple way.
































































































































































































































































































































































































































































































































































































































































































































































































Java Code:









































































































































































































































































































































































































































































































































String[] lines = data.split(System.getProperty("line.separator"));
Set<String> current = new HashSet <>();

for (int i = 3; i < 6; i++) {
if (lines.length < i)
return current;

String line = lines[i];

if (current.contains(line))
continue;

if (latestMessages.contains(lines[i]))
continue;

current.add(line);
}

latestMessages.addAll(current);

if (latestMessages.size() >= 7) {
for (int i = 0; i < latestMessages.size() - 3; i++) {
latestMessages.remove(i);
}
}






































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































No comments:

Post a Comment