我們需要管理一批對象序列,但是又對實際運行的時候的對象類型和對象序列長度不確定的時候,用簡單的對象引用無法滿足,java有ArrayList,Map,Set等這些容器類提供,這些都實現了Collections接口,所以都屬于Collections類。
package com.example.demo.demos.web;public class apple extends fruit{
}
package com.example.demo.demos.web;public class orange extends fruit{
}
package com.example.demo.demos.web;public class fruit {
}
package com.example.demo;import com.example.demo.demos.web.apple;
import com.example.demo.demos.web.fruit;
import com.example.demo.demos.web.orange;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class TestClass {public static void main(String[] args) {List<fruit> fruits = new ArrayList<fruit>();fruits = Arrays.asList(new apple(),new orange());for (fruit fruit : fruits) {System.out.println(fruit);}}}
com.example.demo.demos.web.apple@452b3a41
com.example.demo.demos.web.orange@4a574795
輸出了具體類名和對應的散列碼(通過hashCode生成再轉為十六進制)
package com.example.demo;import com.example.demo.demos.web.apple;
import com.example.demo.demos.web.fruit;
import com.example.demo.demos.web.orange;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class TestClass {public static void main(String[] args) {List<fruit> fruits = new ArrayList<fruit>();fruits = Arrays.asList(new apple(),new orange());for (fruit fruit : fruits) {System.out.println(fruit);System.out.println(Integer.toHexString(fruit.hashCode()));}}}
com.example.demo.demos.web.apple@452b3a41
452b3a41
com.example.demo.demos.web.orange@4a574795
4a574795