• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1579
  • Last Modified:

Manipulating data in a TreeMap

Hi

I am trying to manipulate data in a TreeMap and have run into a roadblock about placing it back into the treemap after I have found items I am looking either by changing certain values in them or actually removing items too.

First thing I do it look for the text 'full' if it has the same workId as previous itmes remove the those keys (kept in an ArrayList)


import java.util.*;

public class MySort
{
      private TreeMap treeMap;

      public MySort ()
      {
            treeMap = new TreeMap();
      }


    public void sort()
      {
            Data data[] = new Data[10];
            for (int a = 0; a <= 9; a++)
            {
                  data[a] = new Data();
            }

            data[0].workId = "19512K0001";
            data[0].updateType = "full";
            data[0].number = "3";
            data[0].numberOfParts = "1";
            data[0].partNumber = "1";
            data[0].fileSize = "33";
            data[0].dateSent = "2006-05-01.10.21.21.123456";
            data[0].datacontents = "test0.txt";

            
            data[1].workId = "19512K0001";
            data[1].updateType = "full";
            data[1].number = "2";
            data[1].numberOfParts = "1";
            data[1].partNumber = "1";
            data[1].fileSize = "33";
            data[1].dateSent = "2006-05-01.10.24.21.123456";
            data[1].datacontents = "test1.txt";

            
            data[2].workId = "19512K0001";
            data[2].updateType = "full";
            data[2].number = "1";
            data[2].numberOfParts = "2";
            data[2].partNumber = "1";
            data[2].fileSize = "33";
            
            data[2].dateSent = "2006-05-01.10.25.21.123456";
            data[2].datacontents = "test2.txt";


            
            data[3].workId = "19512K0001";
            data[3].updateType = "full";
            data[3].number = "1";
            data[3].numberOfParts = "2";
            data[3].partNumber = "2";
            data[3].fileSize = "33";
            data[3].dateSent = "2006-05-01.10.25.21.123457";
            data[3].datacontents = "test3.txt";

            
            data[4].workId = "19512YK0002";
            data[4].updateType = "full";
            data[4].number = "1";
            data[4].numberOfParts = "3";
            data[4].partNumber = "1";
            data[4].fileSize = "33";
            data[4].dateSent = "2006-05-01.10.25.21.223456";
            data[4].datacontents = "test4.txt";

            
            data[5].workId = "19512K0002";
            data[5].updateType = "full";
            data[5].number = "1";
            data[5].numberOfParts = "3";
            data[5].partNumber = "3";
            data[5].fileSize = "33";
            data[5].dateSent = "2006-05-01.11.25.22.223456";
            data[5].datacontents = "test5.txt";

            
            data[6].workId = "19512K0002";
            data[6].updateType = "full";
            data[6].number = "1";
            data[6].numberOfParts = "3";
            data[6].partNumber = "2";
            data[6].fileSize = "33";
            data[6].dateSent = "2006-05-01.10.25.22.223456";
            data[6].datacontents = "test6.txt";

            
            data[7].workId = "19512K0003";
            data[7].updateType = "partial";
            data[7].number = "3";
            data[7].numberOfParts = "1";
            data[7].partNumber = "1";
            data[7].fileSize = "33";
            data[7].dateSent = "2006-06-01.10.21.21.123456";
            data[7].datacontents = "test7.txt";

               
                data[8].workId = "1001YK0003";
                data[8].updateType = "full";
                data[8].number = "1";
                data[8].numberOfParts = "2";
                data[8].partNumber = "1";
                data[8].fileSize = "33";
                data[8].dateSent = "2006-06-03.10.21.21.123456";
                data[8].datacontents = "test8.txt";

               
                data[9].workId = "1001YK0003";
                data[9].updateType = "full";
                data[9].number = "1";
                data[9].numberOfParts = "2";
                data[9].partNumber = "2";
                data[9].fileSize = "33";
                data[9].dateSent = "2006-06-03.10.21.21.123458";
                data[9].datacontents = "test9.txt";




            treeMap = new TreeMap();
            for (int i = 0; i <= 9; i++)
            {
                  Data rd = data[i];
                  String key = rd.workId + rd.dateSent + rd.number + rd.partNumber;
                  Object value = rd;
                  treeMap.put(key, value);
                  System.out.println(key);
            }

            System.out.println("*********************");

            Iterator it = treeMap.keySet().iterator();

            int partialCount = 0;
            ArrayList validKey = new ArrayList();
            String workId = "";
            int count = 0;

            while (it.hasNext())
            {
                count ++;
                  partialCount ++;

                 String key = (String)it.next();
                  Data rd = (Data) treeMap.get(key);
                  String currentWorkId = rd.workId;
                  if (!currentWorkId.equals(workId))
                  {
                        partialCount = 1;
                        workId = currentWorkId;
                        
                  }

                  validKey.add (key);
                  System.out.println(key);

                  if (rd.updateType.equals("full"))
                  {
                  
                  System.out.println (count + ", " + partialCount);
                  
                  for (int k = count - partialCount; k < validKey.size() - 1; k++)
                        {
                              System.out.println ("k is " + k);
                    String rk = (String)validKey.get(k);
                            validKey.remove(rk);
                              System.out.println (rk + " deleted");
                              k = k - 1;
                        }

                        partialCount = 0;
                        
                  }
                  

                  

            }
            
            //Make a new Treemap with new keylist

            TreeMap newtp= new TreeMap();
            for (int g = 0; g < validKey.size(); g++)
            {
                  String key = (String)validKey.get(g);
                  Data rd = (Data)treeMap.get(key);
                  newtp.put(key,rd);                  
            }
            
            
            
            Iterator it1 = newtp.keySet().iterator();

            
            ArrayList validKey2 = new ArrayList();
            String workId2 = "";
            int count2 = 0;
            int country=0;
            String previousdata="";
            String previouskey="";
            int previousfileSize=0;
            String totaldata="";
            String totalsize="";
            
            
            
            while (it1.hasNext())
            {
                count2 ++;
                  

                 String key = (String)it1.next();
                  Data rd1 = (Data) newtp.get(key);
                  String currentWorkId = rd1.workId;
                  System.out.println ("before if currentWorkId is " + workId);
                  validKey2.add (key);
                  if (!currentWorkId.equals(workId2))
                  {
                        System.out.println ("currentWorkId is " + workId);
                        workId2 = currentWorkId;                        
                        
                        if(!rd1.numberOfParts.equals("1"))
                              {
                                    totaldata=rd1.datacontents;
                                    previousfileSize=Integer.parseInt(rd1.fileSize);
                                    previouskey=key;
                                    country ++;
                              }
                        else
                        {
                              previouskey="";
                              previousfileSize=0;
                              totaldata="";
                              
                        }
                        
                  }
                  else
                  {
                        System.out.println ("same workId" );
                  
                        
                        System.out.println(key);

                        if(!rd1.numberOfParts.equals("1"))
                        {
                        System.out.println (count2);
                        
                        System.out.println ("country is " +country);
                        
                        
                        
                        totaldata=totaldata+rd1.datacontents;
                        previousfileSize=previousfileSize+Integer.parseInt(rd1.fileSize);
                        totalsize=""+previousfileSize;
                        rd1.datacontents=totaldata;
                        rd1.fileSize=""+previousfileSize;
                        rd1.numberOfParts="1";
                        rd1.partNumber="1";
                        System.out.println ("totaldata is " +totaldata);
                        System.out.println ("totalsize is " +totalsize);
                        
                                                               

                        /* How do I place this back into the Treemapp and remove the other parts that I just combined????

*/                  


                        }
                        
                  
                  }
                  
            }
            
            
            
            



               

      }


    class Data
      {
            
            String workId;
            String updateType;
            String number;
            String numberOfParts;
            String partNumber;
            String fileSize;            
            String dateSent;
            String datacontents;

            public Data()
            {
            }

            public Data (String wi, String u, String num, String np, String sn, String ds, String datas, String datai)
            {
                  
                  workId = wi;
                  updateType = u;
                  number = num;
                  numberOfParts = np;
                  partNumber = sn;
                  fileSize = ds;
                  
                  dateSent = datas;
                  datacontents = datai;
            }

               

      }

      static public void main(String[] args)
      {
            MySort helper = new MySort();
            helper.sort();
            
      }
}

1)What is the best way to look for/remove duplicate items say older "fulls" or "partials" that are not needed.  Should I do it like I first did with the removing non fulls in the beginning?  Is there a better way that creating an Arraylist for valid keys?

2) How do I place items back into the Treemap after I have found what I am looking for and remove the other pieces that I just concatenated (added sizes, combined parts, and reset other items in that particular object????

This is driving me crazy, so it is worth more than the usual 500 points  for help with/ working functions to accomplish items 1 & 2

Thanks
ryno71
0
ryno71
Asked:
ryno71
2 Solutions
 
CEHJCommented:
Duplicates would be replaced automatically based on the equals method of the key. You can use this to accomplish uniqueness based on your criteria. You may also be able to use a Set rather than a Map
0
 
fffej78Commented:
> First thing I do it look for the text 'full' if it has the same workId as previous itmes remove the those keys (kept in an ArrayList)

So, there can be only one element at a time which has the same "workID"?  In which case, just do something like this.

for each bit of data
  map.put( data.workID, data )

Because data.workID is the key, it'll overwrite previous items.  I guess you might need to check for the word full, so obviously

for each bit of data
   if ( data.updateType is full )
      map.put( data.workID, data )
   else
      // data update is partial so I could update a bit of data by
      Data old = map.get( data.workID)
      Data updatedData = old;
      // edit data accordingly
      map.put( data.workID, updatedData )



Just another word of advice, use the most abstract type possible!  You shouldn't have code like

TreeMap treeMap = new TreeMap();

Unless you are using methods specific to the TreeMap class.  Much better to have:

Map map = new TreeMap();

Because then at some point you can change your mind and have a HashMap or any other Map implementation.  Similarly ArrayList should be replaced with List.  The more abstract the better!
0
 
ryno71Author Commented:
Sorry guys..  I figured it out using a comparator.  Alot of you guys suggested this before...  Thats what I ended up using.  If I had used a treemap as above it would have been ugly and alot of looping with if then else checks.

thanks
ryno71
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now