How to compare and replace array values in array list

时间:2019-04-08 13:24:56

标签: java arrays

I have to update the role array at the backend using java. Role array value is a combination of project code and level of access. Existing value of role array will be look like below format:

 {
   "role": [
     "1694:admin",
     "4836:user"
   ]
 }

I'll get new role value in array list as input. For ex:-

 { 
 "update-role": [
     "1694:user",
     "2:admin"
   ]
 }

i have to compare and replace the array list. If i get same company code i have to replace the value at the backend otherwise i have append the new values. So the output should be look like below value.

{ 
 "role": [
     "1694:user",
     "4836:user",
     "2:admin"
   ]
 }

Tried below code. But not able to get desired output. Can some one help?

ArrayList<String> oktavar = new ArrayList<String>(eventContext.getMessage().getInvocationProperty("oktavar"));
        ArrayList<String> csvvar = new ArrayList<String>(eventContext.getMessage().getInvocationProperty("csvvar"));

        String s2 = eventContext.getMessage().getInvocationProperty("oktavar").toString();
        String s3 = eventContext.getMessage().getInvocationProperty("csvvar").toString();

        System.out.println("from java: Splitting array list" + "oktavar" + oktavar + "csvvar" + csvvar);

        StringTokenizer st22 = new StringTokenizer(s2, "[]",false);
        StringTokenizer st33 = new StringTokenizer(s3, "[]",false);

         System.out.println( "st22 values " +st22 +"----"+ st33 );

        ArrayList<String> newvar = new ArrayList<String>();
         StringTokenizer st2 = new StringTokenizer(st22.nextToken(), ":");
         StringTokenizer st3 = new StringTokenizer(st33.nextToken(), ":");
         System.out.println( "st2 values " +st2 +"----"+ st3 );
        for (String i : csvvar){
            /*String checkString = (String) st2.next
            System.out.println("i value" +i +"checkstring"+ checkString);*/
            System.out.println("i " + i);
            for(String j: oktavar){ 
                 while (st3.hasMoreElements()) {
                     while (st2.hasMoreElements()) {
                         String t1 = (String) st3.nextToken();
                         String t2 = (String) st2.nextToken();

                System.out.println("t1 " + t1 + "t2 " + t2);
                if(t1.equalsIgnoreCase(t2) ){
                    newvar.add(i);
                    break;
                }
                else
                    System.out.println("j " + j);
                    newvar.add(j);
                    break;
            }
                 }
            }
            }


            System.out.println("new array" + newvar);

0 个答案:

没有答案