Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Retrieve desired values from a HashMap to print duplicate value

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Retrieve desired values from a HashMap to print duplicate value

    Please consider the following code:
    public static void main(String[] args) {
     
    List<String> fileContents = new ArrayList<String>();
     
    fileContents.add("AB1011");
    fileContents.add("AB1012");
    fileContents.add("AB1013");
    fileContents.add("AB1014");
    fileContents.add("AB1015");
    fileContents.add("AB1015");
    fileContents.add("AB1012");
    ;
    String[] sample_letter = { "A1", "E2", "G1", "C3", "B1", "F2", "H1", "D3", "C1", "G2", "A2", "E3", "D1", "H2",
    "B2", "F3", "E1", "A3", "C2", "G3", "F1", "B3", "D2", "H3", "A4", "E5", "G4", "C6", "B4", "F5", "H4",
    "D6", "C4", "G5", "A5", "E6", "D4", "H5", "B5", "F6", "E4", "A6", "C5", "G6", "F4", "B6", "D5", "H6",
    "A7", "E8", "G7", "C9", "B7", "F8", "H7", "D9", "C7", "G8", "A8", "E9", "D7", "H8", "B8", "F9", "E7",
    "A9", "C8", "G9", "F7", "B9", "D8", "H9", "A10", "E11", "G10", "C12", "B10", "F11", "H10", "D12", "C10",
    "G11", "A11", "E12", "D10", "H11", "B11", "F12", "E10", "A12", "C11", "G12", "F10", "B12", "D11",
    "H12" };
     
    List<String[]> rows = new ArrayList<String[]>();
     
    Map<String, List<Integer>> mapDups = new HashMap<>(); // name, list of line numbers
     
    Map<Integer, Integer> indexMap = new HashMap<>(); // line number, index of the line number
     
    ArrayList<Integer> firstPositionofOriginalCase = new ArrayList<Integer>();
    ArrayList<Integer> duplicatePositionofOriginalCase = new ArrayList<Integer>();
     
    for (int i = 0; i < fileContents.size(); i++) {
    String name = fileContents.get(i);
    List<Integer> lineNos = mapDups.get(name);
    if (lineNos != null) {
     
    for (int j = 0; j < lineNos.size(); j++) {
    int lineNo = lineNos.get(j);
     
    indexMap.put(lineNo, i);
    duplicatePositionofOriginalCase.add(i);
    firstPositionofOriginalCase.add(lineNo);
     
    }
    }
     
    if (lineNos == null)
    lineNos = new ArrayList<Integer>();
    lineNos.add(i);
    mapDups.put(name, lineNos);
    }
     
    for (var entry : mapDups.entrySet()) {
    System.out.println(entry.getKey() + "|" + entry.getValue());
    }
     
    // Map for storing
     
    for (int i = 0; i < fileContents.size(); i++) {
    String replicate = "         "; // placeholder 9 spaces for when a duplicate is not found
    String Aux = "0";
     
    String[] rowInfo = { fileContents.get(i) + "_" + sample_letter[i], replicate, sample_letter[i] };
     
    System.out.println("Adding: " + fileContents.get(i) + "_" + sample_letter[i] + " | " + replicate + " | "
    + sample_letter[i] + "|" + Aux);
     
    rows.add(rowInfo);
    }
     
    }
    The above code prints the following:
    AB1015|[4, 5]
    AB1011|[0]
    AB1012|[1, 6]
    AB1013|[2]
    AB1014|[3]
    Adding: AB1011_A1 |           | A1|0
    Adding: AB1012_E2 |           | E2|0
    Adding: AB1013_G1 |           | G1|0
    Adding: AB1014_C3 |           | C3|0
    Adding: AB1015_B1 |           | B1|0
    Adding: AB1015_F2 |           | F2|0
    Adding: AB1012_H1 |           | H1|0

    And I am looking for the following output.

    Adding: AB1011_A1 |           | A1|0
    Adding: AB1012_E2 |  AB1012_H1         | E2|0
    Adding: AB1013_G1 |           | G1|0
    Adding: AB1014_C3 |           | C3|0
    Adding: AB1015_B1 | AB1015_F2          | B1|0
    Adding: AB1015_F2 |           | F2|0
    Adding: AB1012_H1 |           | H1|0

    Explanation of what I'm looking for:

    As shown above, I want the duplicate value (the replicate variable in the code) to be printed next to the original value. In the above desired output, since AB1012 has a duplicate, the duplicate value was printed next to the original value, which is AB1012_H1. Similarly, for AB1015.
    Looping over the mapDups is giving me the following information and telling me that original position of AB1015 is 4 and duplicate is found at 5th position. Similary, original position of AB1012 is 1 and duplicate is found at 6th position. I was thinking of using two array lists to store firstPositionofOriginalCase and duplicatePositionofOriginalCase but I'm not sure if this is the right way to go about this problem.

    AB1015|[4, 5]
    AB1011|[0]
    AB1012|[1, 6]
    AB1013|[2]
    AB1014|[3]

    Hence, wanted to ask if anyone can think of better way of handling above situation such that I can get what I'm looking for.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,173
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Retrieve desired values from a HashMap to print duplicate value

    Can you post full code that is properly indented and that has the import statements and the class name so that the code can be compiled and executed for testing?
    Also can you add some comments to the code that describes what the code is supposed to do?


    Is this the same topic: https://coderanch.com/t/789443/java/...icate-original
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Want to Add Duplicate Key in HashMap
    By rampnq in forum Collections and Generics
    Replies: 8
    Last Post: January 17th, 2018, 05:01 PM
  2. Duplicate values add to Set
    By rajanalaj in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 22nd, 2014, 03:49 AM
  3. retrieve data from database using Hashmap (multiple values in one key)
    By ashin12 in forum Collections and Generics
    Replies: 2
    Last Post: April 4th, 2012, 05:22 AM
  4. How to make set to allow duplicate values... Please help me!!!
    By JavaKiddo in forum Collections and Generics
    Replies: 9
    Last Post: January 4th, 2012, 08:22 AM
  5. check duplicate key and value in hashmap
    By starmandell in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 9th, 2011, 04:25 PM