Filter and use text out of a txt file












0















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);
}









share|improve this question

























  • 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


















0















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);
}









share|improve this question

























  • 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
















0












0








0








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);
}









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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



















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














2 Answers
2






active

oldest

votes


















1














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.






share|improve this answer


























  • "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



















0














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);





share|improve this answer
























    Your Answer






    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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.






    share|improve this answer


























    • "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
















    1














    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.






    share|improve this answer


























    • "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














    1












    1








    1







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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



















    • "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













    0














    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);





    share|improve this answer




























      0














      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);





      share|improve this answer


























        0












        0








        0







        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);





        share|improve this answer













        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);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 13:34









        oleg.cherednikoleg.cherednik

        7,19421219




        7,19421219






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Costa Masnaga

            Create new schema in PostgreSQL using DBeaver

            Fotorealismo