We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,524 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to get previous values in next iteration in collection

 HI Could yu please tell me how to get previous values from current iteration

 in the below example : public void parseDtoCollection(Collection dtos) contains list of dtos 
 in private AssetClassVersion createNewAssetClassVersion(AssetDTO dto) method i am iterating but i want to get 
to get  previous itearation values in current iteration if the dto.assetClassId is same then i want to append like previous matchedstring value + current matchedstring value


 public void parseDtoCollection(Collection dtos) {


        for (Iterator i = dtos.iterator(); i.hasNext();) {

            AssetDTO dto = (AssetDTO) i.next();          

            AssetClass assetClass = findAssetClass(dto.assetClassId);            

            AssetClassVersion assetClassVersion = createNewAssetClassVersion(dto);

            assetClass.addVersion(assetClassVersion);


            if (dto.id > 0) {

                InformationAsset infoAsset =
                        findInformationAsset(dto.id, assetClass);
                InformationAssetVersion infoAssetVersion =
                        createInfoAssetVersion(dto);
                infoAsset.setBusinessArea(createBusinessArea(dto));
                infoAsset.addVersion(infoAssetVersion);
                infoAsset.setAssetClass(assetClass);




            }
        }
    }


    private AssetClass findAssetClass(int id) { 
        Integer oid = new Integer(id);


        AssetClass assetClass = (AssetClass) assetClassMap.get(oid);


        if (null == assetClass) {
            assetClass = getOJBFactory().getAssetClass(); 
            assetClass.setOid(oid);

            assetClassMap.put(oid, assetClass);

            AssetClassAssets aca = getOJBFactory().getAssetClassAssets();
            aca.setAssetClass(assetClass);
            assetHolder.put(oid,  aca);

        }

        return assetClass;      
    }

       private AssetClassVersion createNewAssetClassVersion(AssetDTO dto) {     



        AssetClassVersion ac = new AssetClassVersion();     

        ac.setVersion(new Integer(dto.assetClassVersion)); 

        ac.setName(dto.assetClassName);

        ac.setDescription(dto.assetClassDescription);
        ac.setStatus(Status.findById(dto.assetClassStatusId));
        ac.setModifyDate(dto.assetClassModifyDate);
        ac.setEffectiveDate(dto.assetClassEffectiveDate);
        ac.setSeriesCode(dto.seriesCode);     

        ac.setMatchedString(dto.matchedString);     


        return ac;        
    }   


    private InformationAsset findInformationAsset(int id, AssetClass assetClass) {
        Integer oid = new Integer(id);
        InformationAsset infoAsset = (InformationAsset)infoAssetMap.get(oid);
        if (null == infoAsset) {
            infoAsset = getOJBFactory().getInformationAsset();
            infoAsset.setOid(oid);
            infoAsset.setAssetClass(assetClass); 
            infoAssetMap.put(oid, infoAsset);
        }

        return infoAsset;       
    }
3
Contributors
6
Replies
1 Day
Discussion Span
11 Months Ago
Last Updated
7
Views
raviaaaa
Light Poster
36 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I'm not sure how you are using the word "previous" here, but if that's the opposite of "next" in the Iterator then...
If you have your dtos in a List (rather than just a Collection) then that implies a sequence for the members, and you can get a ListIterator rather than a simple Iterator. ListIterators allow both forward and backward traversal of the List.

JamesCherrill
... trying to help
Moderator
8,534 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

There are 2 things that comes to mind..
1. Did you code this yourself
2. If so , then why didnt you use a supported object like ListIterator of ArrayList which will be easy to manipulate?

Also with the int and interger examples...

line 40 and 85 is nt necessary as you could simply do

public static void foo(int bar, Dim dim){
   // int does not really need interger object as it safe casted.
   int faa = bar; // there was no need for new interger. 
   dim = new Dim;

   /// bla bla bla

}

on line 47 and 88..

if(foo.equals("null"){ // then do this or that}
Always use this if you are checking for null datas
Its very accurate.
Also mind you that null and zero are not the same.
zero is a value but null means empty. So be very careful what you are checking for.

Hope it helps :-)

richieking
Master Poster
789 posts since Jun 2009
Reputation Points: 58
Solved Threads: 156
Skill Endorsements: 0

@ritchieking: looks like you were distracted or something when you wrote that one!

The original code creates an Integer (nb spelling!) from the int parameter because its used as a key value in the Map, and Map keys can't be primitives. Modern Java versions will auto box/unbox to convert implicitly between int and Integer, but it cases like that it's helpful to keep it explicit.

dim = new Dim; Starting a method by overwriting a parameter's value is never going to be a good idea.

if(foo.equals("null")) this is in no possible way a substitute for if (foo == null) What were you thinking?

JamesCherrill
... trying to help
Moderator
8,534 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

HI All
my requiurement is:
I am getting list, list consists of follwing

id version name assstatusid seriescode matchedstring
10 1 ab 3 34 match1
11 2 some 3 67 mat
12 3 name 3 678 matchstring
12 3 name 3 678 matchstringone
12 3 name 3 678 matchstri
12 3 name 3 678 sonemat

in the above code i want to display in such way that
here id 12 is repeating but matchedstring column having different values i want to display in
o/p:
12 3 name 3 678 matchstring, matchstringone, matchstri, sonemat

can you please let me know how to solve this based on above code iam using JDK 14 and Tomcat 4

Regards
ravi

---------------------------------------------------------------------------------------------------------

raviaaaa
Light Poster
36 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You really must update your Java.

JamesCherrill
... trying to help
Moderator
8,534 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

Pseudo-code:

String previous = "";
loop through values {
    if (value.equals(previous)) print on same line;
    else  start a new line;
    previous = value;
}
JamesCherrill
... trying to help
Moderator
8,534 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0765 seconds using 2.73MB