Filter and use text out of a txt file
I want to filter it so that it only uses the latest information for each different combination of PART and FUNCTION so that I only end up having around a handful of results per combo instead of thousands of outdated ones.
ID is based on time here.
This is what I've currently got:
while((line = br.readLine())!= null){
info = line.split(",");
Map<String,String> qgi=new HashMap<String,String>();
qgi.put("ID",info[0]);
qgi.put("FUNCTION",info[1]);
qgi.put("PART",info[2]);
qgi.put("STATE",info[3]);
sourceList.add(qgi);
}
for (int i = 0, len = sourceList.size(); i < len; i++) {
Map<String,String> gqi =(Map) sourceList.get(i);
if ( gqi.get("PART").equals("chw") && gqi.get("FUNCTION").equals("diskusage") && gqi.get("ID").equals("20181122125601"))//Get highest value with those parameters
{
resultList.add(gqi);
}
}
So currently my ID equals a specific date and time, and I want it to be the highest ID for that specific combination(chw and diskusage).
I want to essentially do something like
if(a.get("b").equals("something") && a.get("c").equals("something") && thisCombinationsID >= everyOtherIDWithThisCombination{
resultList.add(a);
}
java list dictionary
add a comment |
I want to filter it so that it only uses the latest information for each different combination of PART and FUNCTION so that I only end up having around a handful of results per combo instead of thousands of outdated ones.
ID is based on time here.
This is what I've currently got:
while((line = br.readLine())!= null){
info = line.split(",");
Map<String,String> qgi=new HashMap<String,String>();
qgi.put("ID",info[0]);
qgi.put("FUNCTION",info[1]);
qgi.put("PART",info[2]);
qgi.put("STATE",info[3]);
sourceList.add(qgi);
}
for (int i = 0, len = sourceList.size(); i < len; i++) {
Map<String,String> gqi =(Map) sourceList.get(i);
if ( gqi.get("PART").equals("chw") && gqi.get("FUNCTION").equals("diskusage") && gqi.get("ID").equals("20181122125601"))//Get highest value with those parameters
{
resultList.add(gqi);
}
}
So currently my ID equals a specific date and time, and I want it to be the highest ID for that specific combination(chw and diskusage).
I want to essentially do something like
if(a.get("b").equals("something") && a.get("c").equals("something") && thisCombinationsID >= everyOtherIDWithThisCombination{
resultList.add(a);
}
java list dictionary
Doing like in your question, you will have 4 values in your map, which basically represents just a single data set, consisting of a single ID, a single FUNCTION, a single PART and a single STATE. Is that what you want to have? You cannot filter it properly because it only has 4 totally different values... See the answer by @JBNizet and create one object per data set. Those are filterable from aMap
orList
.
– deHaar
Nov 26 '18 at 13:15
add a comment |
I want to filter it so that it only uses the latest information for each different combination of PART and FUNCTION so that I only end up having around a handful of results per combo instead of thousands of outdated ones.
ID is based on time here.
This is what I've currently got:
while((line = br.readLine())!= null){
info = line.split(",");
Map<String,String> qgi=new HashMap<String,String>();
qgi.put("ID",info[0]);
qgi.put("FUNCTION",info[1]);
qgi.put("PART",info[2]);
qgi.put("STATE",info[3]);
sourceList.add(qgi);
}
for (int i = 0, len = sourceList.size(); i < len; i++) {
Map<String,String> gqi =(Map) sourceList.get(i);
if ( gqi.get("PART").equals("chw") && gqi.get("FUNCTION").equals("diskusage") && gqi.get("ID").equals("20181122125601"))//Get highest value with those parameters
{
resultList.add(gqi);
}
}
So currently my ID equals a specific date and time, and I want it to be the highest ID for that specific combination(chw and diskusage).
I want to essentially do something like
if(a.get("b").equals("something") && a.get("c").equals("something") && thisCombinationsID >= everyOtherIDWithThisCombination{
resultList.add(a);
}
java list dictionary
I want to filter it so that it only uses the latest information for each different combination of PART and FUNCTION so that I only end up having around a handful of results per combo instead of thousands of outdated ones.
ID is based on time here.
This is what I've currently got:
while((line = br.readLine())!= null){
info = line.split(",");
Map<String,String> qgi=new HashMap<String,String>();
qgi.put("ID",info[0]);
qgi.put("FUNCTION",info[1]);
qgi.put("PART",info[2]);
qgi.put("STATE",info[3]);
sourceList.add(qgi);
}
for (int i = 0, len = sourceList.size(); i < len; i++) {
Map<String,String> gqi =(Map) sourceList.get(i);
if ( gqi.get("PART").equals("chw") && gqi.get("FUNCTION").equals("diskusage") && gqi.get("ID").equals("20181122125601"))//Get highest value with those parameters
{
resultList.add(gqi);
}
}
So currently my ID equals a specific date and time, and I want it to be the highest ID for that specific combination(chw and diskusage).
I want to essentially do something like
if(a.get("b").equals("something") && a.get("c").equals("something") && thisCombinationsID >= everyOtherIDWithThisCombination{
resultList.add(a);
}
java list dictionary
java list dictionary
edited Nov 26 '18 at 14:23
Dragonthoughts
1,64241017
1,64241017
asked Nov 26 '18 at 13:00
CrawlyCrawly
11
11
Doing like in your question, you will have 4 values in your map, which basically represents just a single data set, consisting of a single ID, a single FUNCTION, a single PART and a single STATE. Is that what you want to have? You cannot filter it properly because it only has 4 totally different values... See the answer by @JBNizet and create one object per data set. Those are filterable from aMap
orList
.
– deHaar
Nov 26 '18 at 13:15
add a comment |
Doing like in your question, you will have 4 values in your map, which basically represents just a single data set, consisting of a single ID, a single FUNCTION, a single PART and a single STATE. Is that what you want to have? You cannot filter it properly because it only has 4 totally different values... See the answer by @JBNizet and create one object per data set. Those are filterable from aMap
orList
.
– deHaar
Nov 26 '18 at 13:15
Doing like in your question, you will have 4 values in your map, which basically represents just a single data set, consisting of a single ID, a single FUNCTION, a single PART and a single STATE. Is that what you want to have? You cannot filter it properly because it only has 4 totally different values... See the answer by @JBNizet and create one object per data set. Those are filterable from a
Map
or List
.– deHaar
Nov 26 '18 at 13:15
Doing like in your question, you will have 4 values in your map, which basically represents just a single data set, consisting of a single ID, a single FUNCTION, a single PART and a single STATE. Is that what you want to have? You cannot filter it properly because it only has 4 totally different values... See the answer by @JBNizet and create one object per data set. Those are filterable from a
Map
or List
.– deHaar
Nov 26 '18 at 13:15
add a comment |
2 Answers
2
active
oldest
votes
Create a class RowKey
, with a part
and a function
, and proper equals
and hashCode
overrides (the IDE can generate that for you).
Create a class Row
, with a rowKey
(of type RowKey
), an id
and a state
.
Create a Map<RowKey, Row>
containing, for each row key, the row that has the max ID (empty initially).
Iterate throught the lines of the file. For each line, create an instance of Row.
Then check if an entry already exists in the map for this row key. If not, or if the new row has a bigger ID than the existing row, store the new row in the map. The Map.merge
can help doing that.
At the end, the values of the map contains what you want.
The implementation is left as an exercise for the reader.
"Create a class RowKey, with a part and an info, and proper equals and hashCode overrides (the IDE can generate that for you)." I'm assuming you mean a 'part' and a 'function' right, or what would you have me do with info(which is a C-style array in my piece of code)?
– Crawly
Nov 26 '18 at 13:56
Yes, sorry for the confusion. Fixed now.
– JB Nizet
Nov 26 '18 at 13:58
add a comment |
final class Data {
private int id;
private final String function;
private final String part;
private final String state;
public Data(String id, String function, String part, String state) {
this.id = Integer.parseInt(id);
this.function = function;
this.part = part;
this.state = state;
}
}
List<Data> sourceList = new ArrayList<>();
while ((line = br.readLine()) != null) {
String info = line.split(",");
sourceList.add(new Data(info[0], info[1], info[2], info[3]));
}
final Predicate<Data> specificCombination = data -> "something".equals(data.function) && "something".equals(data.part);
final Comparator<Data> compareByIdDesc = (d1, d2) -> Integer.compare(d2.id, d1.id);
Data data = sourceList.stream().filter(specificCombination).min(compareByIdDesc).orElse(null);
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53481692%2ffilter-and-use-text-out-of-a-txt-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Create a class RowKey
, with a part
and a function
, and proper equals
and hashCode
overrides (the IDE can generate that for you).
Create a class Row
, with a rowKey
(of type RowKey
), an id
and a state
.
Create a Map<RowKey, Row>
containing, for each row key, the row that has the max ID (empty initially).
Iterate throught the lines of the file. For each line, create an instance of Row.
Then check if an entry already exists in the map for this row key. If not, or if the new row has a bigger ID than the existing row, store the new row in the map. The Map.merge
can help doing that.
At the end, the values of the map contains what you want.
The implementation is left as an exercise for the reader.
"Create a class RowKey, with a part and an info, and proper equals and hashCode overrides (the IDE can generate that for you)." I'm assuming you mean a 'part' and a 'function' right, or what would you have me do with info(which is a C-style array in my piece of code)?
– Crawly
Nov 26 '18 at 13:56
Yes, sorry for the confusion. Fixed now.
– JB Nizet
Nov 26 '18 at 13:58
add a comment |
Create a class RowKey
, with a part
and a function
, and proper equals
and hashCode
overrides (the IDE can generate that for you).
Create a class Row
, with a rowKey
(of type RowKey
), an id
and a state
.
Create a Map<RowKey, Row>
containing, for each row key, the row that has the max ID (empty initially).
Iterate throught the lines of the file. For each line, create an instance of Row.
Then check if an entry already exists in the map for this row key. If not, or if the new row has a bigger ID than the existing row, store the new row in the map. The Map.merge
can help doing that.
At the end, the values of the map contains what you want.
The implementation is left as an exercise for the reader.
"Create a class RowKey, with a part and an info, and proper equals and hashCode overrides (the IDE can generate that for you)." I'm assuming you mean a 'part' and a 'function' right, or what would you have me do with info(which is a C-style array in my piece of code)?
– Crawly
Nov 26 '18 at 13:56
Yes, sorry for the confusion. Fixed now.
– JB Nizet
Nov 26 '18 at 13:58
add a comment |
Create a class RowKey
, with a part
and a function
, and proper equals
and hashCode
overrides (the IDE can generate that for you).
Create a class Row
, with a rowKey
(of type RowKey
), an id
and a state
.
Create a Map<RowKey, Row>
containing, for each row key, the row that has the max ID (empty initially).
Iterate throught the lines of the file. For each line, create an instance of Row.
Then check if an entry already exists in the map for this row key. If not, or if the new row has a bigger ID than the existing row, store the new row in the map. The Map.merge
can help doing that.
At the end, the values of the map contains what you want.
The implementation is left as an exercise for the reader.
Create a class RowKey
, with a part
and a function
, and proper equals
and hashCode
overrides (the IDE can generate that for you).
Create a class Row
, with a rowKey
(of type RowKey
), an id
and a state
.
Create a Map<RowKey, Row>
containing, for each row key, the row that has the max ID (empty initially).
Iterate throught the lines of the file. For each line, create an instance of Row.
Then check if an entry already exists in the map for this row key. If not, or if the new row has a bigger ID than the existing row, store the new row in the map. The Map.merge
can help doing that.
At the end, the values of the map contains what you want.
The implementation is left as an exercise for the reader.
edited Nov 26 '18 at 13:57
answered Nov 26 '18 at 13:11
JB NizetJB Nizet
547k588941021
547k588941021
"Create a class RowKey, with a part and an info, and proper equals and hashCode overrides (the IDE can generate that for you)." I'm assuming you mean a 'part' and a 'function' right, or what would you have me do with info(which is a C-style array in my piece of code)?
– Crawly
Nov 26 '18 at 13:56
Yes, sorry for the confusion. Fixed now.
– JB Nizet
Nov 26 '18 at 13:58
add a comment |
"Create a class RowKey, with a part and an info, and proper equals and hashCode overrides (the IDE can generate that for you)." I'm assuming you mean a 'part' and a 'function' right, or what would you have me do with info(which is a C-style array in my piece of code)?
– Crawly
Nov 26 '18 at 13:56
Yes, sorry for the confusion. Fixed now.
– JB Nizet
Nov 26 '18 at 13:58
"Create a class RowKey, with a part and an info, and proper equals and hashCode overrides (the IDE can generate that for you)." I'm assuming you mean a 'part' and a 'function' right, or what would you have me do with info(which is a C-style array in my piece of code)?
– Crawly
Nov 26 '18 at 13:56
"Create a class RowKey, with a part and an info, and proper equals and hashCode overrides (the IDE can generate that for you)." I'm assuming you mean a 'part' and a 'function' right, or what would you have me do with info(which is a C-style array in my piece of code)?
– Crawly
Nov 26 '18 at 13:56
Yes, sorry for the confusion. Fixed now.
– JB Nizet
Nov 26 '18 at 13:58
Yes, sorry for the confusion. Fixed now.
– JB Nizet
Nov 26 '18 at 13:58
add a comment |
final class Data {
private int id;
private final String function;
private final String part;
private final String state;
public Data(String id, String function, String part, String state) {
this.id = Integer.parseInt(id);
this.function = function;
this.part = part;
this.state = state;
}
}
List<Data> sourceList = new ArrayList<>();
while ((line = br.readLine()) != null) {
String info = line.split(",");
sourceList.add(new Data(info[0], info[1], info[2], info[3]));
}
final Predicate<Data> specificCombination = data -> "something".equals(data.function) && "something".equals(data.part);
final Comparator<Data> compareByIdDesc = (d1, d2) -> Integer.compare(d2.id, d1.id);
Data data = sourceList.stream().filter(specificCombination).min(compareByIdDesc).orElse(null);
add a comment |
final class Data {
private int id;
private final String function;
private final String part;
private final String state;
public Data(String id, String function, String part, String state) {
this.id = Integer.parseInt(id);
this.function = function;
this.part = part;
this.state = state;
}
}
List<Data> sourceList = new ArrayList<>();
while ((line = br.readLine()) != null) {
String info = line.split(",");
sourceList.add(new Data(info[0], info[1], info[2], info[3]));
}
final Predicate<Data> specificCombination = data -> "something".equals(data.function) && "something".equals(data.part);
final Comparator<Data> compareByIdDesc = (d1, d2) -> Integer.compare(d2.id, d1.id);
Data data = sourceList.stream().filter(specificCombination).min(compareByIdDesc).orElse(null);
add a comment |
final class Data {
private int id;
private final String function;
private final String part;
private final String state;
public Data(String id, String function, String part, String state) {
this.id = Integer.parseInt(id);
this.function = function;
this.part = part;
this.state = state;
}
}
List<Data> sourceList = new ArrayList<>();
while ((line = br.readLine()) != null) {
String info = line.split(",");
sourceList.add(new Data(info[0], info[1], info[2], info[3]));
}
final Predicate<Data> specificCombination = data -> "something".equals(data.function) && "something".equals(data.part);
final Comparator<Data> compareByIdDesc = (d1, d2) -> Integer.compare(d2.id, d1.id);
Data data = sourceList.stream().filter(specificCombination).min(compareByIdDesc).orElse(null);
final class Data {
private int id;
private final String function;
private final String part;
private final String state;
public Data(String id, String function, String part, String state) {
this.id = Integer.parseInt(id);
this.function = function;
this.part = part;
this.state = state;
}
}
List<Data> sourceList = new ArrayList<>();
while ((line = br.readLine()) != null) {
String info = line.split(",");
sourceList.add(new Data(info[0], info[1], info[2], info[3]));
}
final Predicate<Data> specificCombination = data -> "something".equals(data.function) && "something".equals(data.part);
final Comparator<Data> compareByIdDesc = (d1, d2) -> Integer.compare(d2.id, d1.id);
Data data = sourceList.stream().filter(specificCombination).min(compareByIdDesc).orElse(null);
answered Nov 26 '18 at 13:34
oleg.cherednikoleg.cherednik
7,19421219
7,19421219
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53481692%2ffilter-and-use-text-out-of-a-txt-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Doing like in your question, you will have 4 values in your map, which basically represents just a single data set, consisting of a single ID, a single FUNCTION, a single PART and a single STATE. Is that what you want to have? You cannot filter it properly because it only has 4 totally different values... See the answer by @JBNizet and create one object per data set. Those are filterable from a
Map
orList
.– deHaar
Nov 26 '18 at 13:15