Create DocID|Page String to format cutom

an anonymous user · September 16, 2020 · Java
import java.util.*;
import java.lang.*;
import java.io.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.List;

// The main method must be in a class named "Main".
class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
        
        
       
         String obj = "DocA|1,DocA|2,DocA|3,DocA|4,DocB|1,DocB|3,DocC|1";
            String[] input = {"DocA|1", "DocA|2", "DocA|3", "DocA|4", "DocB|1", "DocB|3", "DocC|1"};
            
          //	String[] input = toStringArray(jsonObject);
	        Map<String, List <Integer>> data = Arrays.stream(input) // get stream of Strings
	              .map(s -> s.split("\\|")) // split each string into array using | delimiter
	              .collect(Collectors.groupingBy(
	                  arr -> arr[0], // use documentId as key
	                  Collectors.mapping(arr -> Integer.parseInt(arr[1]), Collectors.toList())) // parse page IDs and collect them to list
	        );
	         System.out.println("StringArray:"+obj);
	         
	         System.out.println(data);
	         
	         
	         
	         
	  
	        ArrayList<String> listdoc = new ArrayList<String>();
			
	        String docid ="";
	        for(int i=0; i<input.length;i++){
	            
	            String[] value = input[i].split("\\|");
	            String curentDocId = value[0];
	            
	            if(i==0){
	                listdoc.add(curentDocId);
	                docid = curentDocId;
	            }
	              else if (!curentDocId.equals(docid)) {
	                    listdoc.add(curentDocId);
	              }
	              docid = curentDocId;
	        }
		      
	        //System.out.println(listdoc);
	     
	        
	        for(int i=0;i<data.size();i++){
	            
	            String mapObj = listdoc.get(i).toString();
	            List<Integer> arrPage = data.get(mapObj);
	            //System.out.println(arrPage);
	            
	            String id = listdoc.get(i).toString();
	            
	            for(int j=0; j<arrPage.size(); j++){
	               //Integer page = Integer.parseInt(arrPage.get[j]);
	                System.out.println("REMOVE "+ id + "Page" + arrPage.get(j));
	                
	                //remove
	               
	            }
	            // SAVE
	            System.out.println("SAVE Doc"+id);
	            
	        }
	        
	        
	        
    }
}

Comments

Please sign up or log in to contribute to the discussion.