Set接口
set接口的实现类特点
1.无序(取出来的顺序和存进去的数据的顺序不一致)
2.唯一(数据不能存相同的)
底层是用Map集合写的
HashSet类 实现了 set接口
唯一性
public class HashSetTest { public static void main(String[] args) { HashSet<String> hs=new HashSet<String>(); hs.add("hello"); hs.add("world"); hs.add("java"); hs.add("java"); for (String str : hs) { System.out.println(str); } } }
输出结果: hello java world
最后一个 没有添加进去 因为已经存在了
hashcoad(),
为什么要重写:在没有重写前 我们在加入对象中时对象的地址值不同,但内容相同 。这样满足不了我们的需求,我们比较对象时一般会比较内容是否相同,很少的情况下会比较的是地址值 ,所以我们为了比较对象的内容 我们需要调用重写之后hashCoad();
public class Student { private String name; private int age; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, int age) { super(); this.name = name; this.age = age; } 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 "Student [name=" + name + ", age=" + age + "]"; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
public class Demo01 { public static void main(String[] args) { Set<Student> set=new HashSet<Student>(); Student s1=new Student("赵云",15); Student s2=new Student("关羽",16); Student s3=new Student("张飞",12); Student s4=new Student("张飞",12); set.add(s1); set.add(s2); set.add(s3); set.add(s4); //Collections.sort(set); for (Student student : set) { System.out.println(student); } } }
输出结果: Student [name=赵云, age=15] Student [name=张飞, age=12] Student [name=张飞, age=12] Student [name=关羽, age=16]
在Student类中重写hashCoad()之后 它会比较对象的内容
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; }
输出结果: Student [name=赵云, age=15] Student [name=张飞, age=12] Student [name=关羽, age=16]
Collections工具类,
对集合进行操作的工具类
//使用sort();进行排序
三种方法
1. 让类实现comparable接口 并且实现 compareTo();
使用规则Collections.sort(List<T> list);
2.自己定义一个比较器 让这个类实现comparator<T>
public class MyCompare implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return o1.getAge()-o2.getAge(); } }
调用方法 Collections.sort(List<T> list,new Comparator);
3.使用匿名内部类
Collections.sort(set,new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.getAge()-o2.getAge(); } });
//使用二分查找法进行集合中的元素(必须是排完序之后的集合)binarySearch(List list,T Key)
首先在让Student实现compatable接口 然后实现compareTo();
public class Student implements Comparable<Student>{
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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 "Student [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Student o) {//按照名字进行排序 名字相同再进行年龄进行排序。
Collator cot=Collator.getInstance(Locale.CHINA);
if (this.getName().equals(o.getName())) {
return this.getAge()-o.getAge();
}else {
return cot.compare(this.getName(),o.getName());
}
}
}
public class Demo01 { public static void main(String[] args) { List<Student> set=new ArrayList<Student>(); Student s1=new Student("赵云",15); Student s2=new Student("关羽",16); Student s3=new Student("张飞",12); Student s4=new Student("张飞",12); set.add(s1); set.add(s2); set.add(s3); set.add(s4); Collections.sort(set);//进行排序 int i = Collections.binarySearch(set,new Student("赵云",15)); System.out.println("索引位置为:"+i); } }
输出结果: 索引位置为:3
//集合的复制(copy(des,src))
public class Demo01 { public static void main(String[] args) { List<Student> set=new ArrayList<Student>(); Student s1=new Student("赵云",15); Student s2=new Student("关羽",16); Student s3=new Student("张飞",12); Student s4=new Student("张飞",12); set.add(s1); set.add(s2); set.add(s3); set.add(s4); Collections.sort(set); List<Student> list=new ArrayList<Student>(); list.add(s1); list.add(s1); list.add(s1); list.add(s1); Collections.copy(list, set);//将set集合中的值复制到list集合中 for (Student student : list) {//遍历新集合 System.out.println(student); } } }
//使用指定对象填充指定集合的所有元素 fill();
public class Demo01 {
public static void main(String[] args) {
List<Student> set=new ArrayList<Student>();
Student s1=new Student("赵云",15);
Student s2=new Student("关羽",16);
Student s3=new Student("张飞",12);
Student s4=new Student("张飞",12);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
Collections.sort(set);
List<Student> list=new ArrayList<Student>();
list.add(s1);
list.add(s1);
list.add(s1);
list.add(s1);
Collections.fill(list, s2);
for (Student student : list) {//遍历新集合
System.out.println(student);
}
}
}
输出结果: Student [name=关羽, age=16] Student [name=关羽, age=16] Student [name=关羽, age=16] Student [name=关羽, age=16]
// (打乱:不是按照存储顺序输出 )
public class Demo01 { public static void main(String[] args) { List<Student> set=new ArrayList<Student>(); Student s1=new Student("赵云",15); Student s2=new Student("关羽",16); Student s3=new Student("张飞",12); Student s4=new Student("张飞",12); set.add(s1); set.add(s2); set.add(s3); set.add(s4); Collections.shuffle(set); for (Student student : set) {//遍历新集合 System.out.println(student); } } }
输出结果一: Student [name=张飞, age=12] Student [name=赵云, age=15] Student [name=张飞, age=12] Student [name=关羽, age=16] 输出结果二: Student [name=关羽, age=16] Student [name=张飞, age=12] Student [name=赵云, age=15] Student [name=张飞, age=12]
两次打乱顺序不一致。
//集合翻转
public class Demo01 {
public static void main(String[] args) {
List<Student> set=new ArrayList<Student>();
Student s1=new Student("赵云",15);
Student s2=new Student("关羽",16);
Student s3=new Student("张飞",12);
Student s4=new Student("张飞",12);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
Collections.reverse(set);
for (Student student : set) {//遍历新集合
System.out.println(student);
}
}
}
输出结果: Student [name=张飞, age=12] Student [name=张飞, age=12] Student [name=关羽, age=16] Student [name=赵云, age=15]
Map集合
键值对,元素是成对存在的,每个元素由两个部分组成,键 和 值组成。通过键可以找对应的值
特点(注意事项):键是唯一的 值可以重复
功能:
获取功能:
get(Object key);//获取指定键对应的值
Collection<T> values();//获取所有值
eg:01
public class MapTest { public static void main(String[] args) { Map<String,String> map=new HashMap<String,String>(); map.put("it001", "赵云"); map.put("it002", "关羽"); map.put("it001", "我替换了it001的值"); String str = map.get("it001"); System.out.println(str); } }
输出结果: 我替换了it001的值
eg:02
public class MapTest { public static void main(String[] args) { Map<String,String> map=new HashMap<String,String>(); map.put("it001", "赵云"); map.put("it002", "关羽"); map.put("it001", "我替换了it001的值"); Collection<String> values= map.values(); for (String value : values) { System.out.println(value); } } }
输出结果: 关羽 我替换了it001的值
映射:
put(Object o1,Object o2)
如果键不重复,则视为添加。如果键重复,则视为修改
public class MapTest { public static void main(String[] args) { Map<String,String> map=new HashMap<String,String>(); map.put("it001", "赵云");//添加成功返回null map.put("it002", "关羽"); map.put("it001", "我替换了it001的值");//替换会返回被替换的键对应的值 即("赵云") System.out.println(map); } }
输出结果: {it002=关羽, it001=我替换了it001的值}
判断:
public class MapTest { public static void main(String[] args) { Map<String,String> map=new HashMap<String,String>(); map.put("it001", "赵云"); map.put("it002", "关羽"); map.put("it001", "我替换了it001的值"); boolean b = map.isEmpty();//判断是否为空 System.out.println(b); boolean c = map.containsKey("it001");//判断是否存在该键 System.out.println(c); boolean d = map.containsValue("关羽");//判断是否存在该值 System.out.println(d); } }
输出结果: false true true
删除:
public class MapTest { public static void main(String[] args) { Map<String,String> map=new HashMap<String,String>(); map.put("it001", "赵云"); map.put("it002", "关羽"); map.put("it001", "我替换了it001的值"); String str = map.remove("it001");//返回为被移除的键对应的值 System.out.println(str); System.out.println(map); } }
输出结果: 我替换了it001的值 {it002=关羽}
遍历:
两种方式: keyset(); 返回所有键 entrySet();返回所有的键值集合
public class MapTest { public static void main(String[] args) { Map<String,String> map=new HashMap<String,String>(); map.put("it001", "赵云"); map.put("it002", "关羽"); map.put("it001", "我替换了it001的值"); Set<String> keys = map.keySet(); for (String key : keys) { System.out.println(key+"对应的值:"+map.get(key)); } System.out.println("--------------------------"); Set<Entry<String,String>> entrys = map.entrySet(); for (Entry<String, String> entry : entrys) { String key=entry.getKey(); String value=entry.getValue(); System.out.println(key+"对应的值:"+value); } } }
输出结果: it002对应的值:关羽 it001对应的值:我替换了it001的值 -------------------------- it002对应的值:关羽 it001对应的值:我替换了it001的值