持有对象-泛型和类型安全的容器
我们需要管理一批对象序列,但是又对实际运行的时候的对象类型和对象序列长度不确定的时候,用简单的对象引用无法满足,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