1: Collection与Collections的区别
Collection 是集合类的上级接口,集成他的接口主要有Set和List
Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索, 排序, 线程安全等操作
2: Set里的元素是不能重复的, 那么用什么方法来区分重复与否呢? 使用== 还是用equals()?他们有何区别
Set里的元素是不可重复的,用equals()方法判断两个Set是否相等
equals()和== 方法决定引用的值是否指向同一对象equals()在类中被覆盖,为的是两个分离的对象的内容和类型相同的话返回真值
3: List ,Set, Map是否集成Collection接口
List, Set集成Collection Map不继承
4: 两个对象值相同(x.equals(y) == true ),但却可以有不同的hashcode这句话对吗
不对,两个equals()相同 那么hashcode一定相同
5: 说出ArrayList 与Vector, LinkedList的存储性和特性
10: 创建一个Set接口的实现类 添加10个元素通过iterator遍历此几个元素
public static void main(String[] args) { Collection c1 = new HashSet(); int num = 1; while (num <= 10){ c1.add(num); num++; } System.out.println(c1.size()); Iterator t1 = c1.iterator(); while (t1.hasNext()){ System.out.println(t1.next()); } }
11: 创建一个Set接口的实现类 添加10个元素通过foreach遍历此集合内元素
public static void main(String[] args) { Collection c1 = new HashSet(); int num = 1; while (num <= 10){ c1.add(num); num++; } System.out.println(c1.size()); Iterator t1 = c1.iterator(); for(Object o : c1){ System.out.println(o); } }
12:创建一个Set接口的实现类 添加元素, 要求能排序
public static void main(String[] args) { Collection c1 = new HashSet(); c1.add(new PersTestOne("老王",13)); c1.add(new PersTestOne("老李",14)); c1.add(new PersTestOne("老张", 10)); System.out.println(c1); // [PersTestOne{name=‘老张‘, age=10}, PersTestOne{name=‘老李‘, age=14}, PersTestOne{name=‘老王‘, age=13}] } } class PersTestOne implements Comparable{ private String name; private int age; @Override public int compareTo(Object o) { if(o instanceof PersTestOne){ PersTestOne p1 = (PersTestOne) o; return this.name.compareTo(p1.name); }else{ return 0; } } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "PersTestOne{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } public PersTestOne(String name, int age) { this.name = name; this.age = age; } public PersTestOne() { } }
。
原文:https://www.cnblogs.com/zhaoyunlong/p/12214461.html