Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clone a Properties object

I'm a Java beginner so please bare with possibly silly questions.

I want to build a Properties object and then clone it. I need the clone to be a Properties object as well, as I need to apply methods to it that apply to Properties objects.

I have written the following code:

public class TryTask_B2 {

public static void main(String args[]) {

    Properties garList = new Properties();  // create "Properties" obj for 'Gar'
    Set gars;  // def a set for the 'Gar' keys
    String strGar;

    // Fill the "Properties" obj for 'Gar':
    garList.put("Gar_1", "rotura de lunas");
    garList.put("Gar_2", "arbitraje de ley");
    garList.put("Gar_3", "Adaptación del hogar");
    garList.put("Gar_4", "rotura de lunas");

    // Create clone of original "Properties" obj 'Gar':
    Object garList_clone = garList.clone();
    Set gars_clone;  // def a set for the cloned 'Gar' keys
    String strGar_clone;

    gars = garList.keySet();  // get a set-view of the 'Gar' keys
    Iterator itrGar = gars.iterator();
    gars_clone = garList_clone.keySet();  // get a set-view of the cloned 'Gar' keys
    Iterator itr_clone = gars_clone.iterator();

    Iterator itrGar_1 = gars.iterator();
    System.out.println("Original list of Gars: ");
    while(itrGar_1.hasNext()){
        strGar = (String) itrGar_1.next();  
        System.out.println(strGar + " : " + garList.getProperty(strGar) + ".");  
    }
    System.out.println();

    // Compare string-value entries for each and every key in the two lists:
    while(itrGar.hasNext()){
        strGar = (String) itrGar.next();  
        while(itr_clone.hasNext()){
            String str1 = garList.getProperty(strGar);
            strGar_clone = (String) itr_clone.next();  
            String str2 = garList_clone.getProperty(strGar_clone);
            boolean result = str1.equalsIgnoreCase(str2);
            System.out.println(strGar + " : " + str1 + ".");
            System.out.println(strGar_clone + " : " + str2 + ".");
            System.out.println(result);
            System.out.println();
            if(result != true){
            } else {
                Object garList_new = garList.remove(strGar);
                System.out.println("Removed element: " + garList_new);
                System.out.println();
            }
        }
        itr_clone = gars_clone.iterator();
    }

    Iterator itrGar_2 = gars.iterator();
    System.out.println("New list of Gars: ");
    while(itrGar_2.hasNext()){
        strGar = (String) itrGar_2.next();              
        System.out.println(strGar + " : " + garList.getProperty(strGar) + ".");  
    }
    System.out.println();
}

}

but it gives me an error for applying the methods "keySet()" and "getProperty()" to my clone... Why?

like image 887
user3729787 Avatar asked Sep 30 '22 09:09

user3729787


2 Answers

Your problem is this line:

Object garList_clone = garList.clone();

You probably assigned the clone to an Object typed variable as this is the return type of the clone method.

But you (as the programmer) know that you are cloning a Properties object. So it is safe to assign the clone to a Properties typed variable. This also requires a cast:

Properties garList_clone = (Properties) garList.clone();

Afterwards you can call the methods that are defined for class Properties. The nature of your compile errors is that the copmiler only knows the declared type of your variables (here: Object). And you only can call methods on such a variable that are declared in that type.

like image 57
Seelenvirtuose Avatar answered Oct 05 '22 21:10

Seelenvirtuose


Because Object garList_clone = garList.clone(); is an Object and not a Properties.

Change it from,

 Object garList_clone = garList.clone();

to

 Properties garList_clone = (Properties) garList.clone();
like image 39
Elliott Frisch Avatar answered Oct 05 '22 21:10

Elliott Frisch