Set
入门案例
Set特点
无序(存储和读取的顺序由可能不一样)
不允许重复(要求元素唯一)
没有索引
/**
* 使用HashSet存储字符串并遍历
* Set集合的特点:
* 无序(存储和读取的顺序由可能不一样)
* 不允许重复(要求元素唯一)
* 没有索引
*/
public class HashSetDemo {
public static void main(String[] args) {
//创建集合对象
//Set<String> set = new HashSet<String>();
Set<String> set = new HashSet<String>();//父接口引用指向子类对象,多态
//添加元素对象
set.add("hello");
set.add("world");
set.add("java");
//遍历集合对象
//三种遍历方式
//转数组
System.out.println("-------------------转数组方式遍历-----------------------");
HashSetDemo.method(set);
System.out.println("-------------------增强for方式遍历-----------------------");
HashSetDemo.method2(set);
System.out.println("-------------------迭代器方式遍历-------------------");
HashSetDemo.method3(set);
//迭代器
//增强for
}
//遍历集合对象
private static void method(Set<String> set) {
Object[] array = set.toArray();
for (Object o : array) {
System.out.println(o);
}
}
//迭代器方式遍历
private static void method2(Set<String> set) {
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String next = it.next();
System.out.println(next);
}
}
//增强for
private static void method3(Set<String> set) {
for (String s : set) {
System.out.println(s);
}
}
}
使用HashSet存储自定义对象并遍历
发现,HashSet没有去重
add方法
首先会使用当前集合中的每一个元素和新添加的元素进行hash值比较,
如果hash值不一样,则直接添加新的元素
如果hash值一样,则比较地址或者使用equals方法进行比较
比较结果一样,则认为是重复不添加
所有的比较结果都不一样则添加
add方法的值,是Map中put方法的key
可以重新equals方法和hashcode方法解决
Collections 工具类
面试题:Collections和Collection有什么区别
Collection是集合体系的最顶层,包含的集合体系的共性
Collections是一个工具类,方法都是用于操作Collection
/**
* Collections:
* 面试题:Collections和Collection有什么区别
* Collection是集合体系的最顶层,包含的集合体系的共性
* Collections是一个工具类,方法都是用于操作Collection
*/
public class CollectionsDemo {
public static void main(String[] args) {
// static <T> int binarySearch(List list, T key)使用二分查找查找指定元素在指定列表的索引位置
//二分查找
// method1();
// method2();
// static void fill(List list ,Object obj):使用指定的对象填充列表的所有元素
// method3();
// static reverse(List list) :反转
// method4();
//sort安照列表中的自然顺序排序
// sort安照列表中的自然顺序排序();
// shuffle随机置换();
// static void swap(List list ,int i,int j)将列表中的两个索引进行位置互换
// swap将指定列表中的两个索引进行位置互换();
}
private static void swap将指定列表中的两个索引进行位置互换() {
ArrayList<String> strings = new ArrayList<>();
strings.add("1");
strings.add("3");
strings.add("2");
strings.add("4");
Collections.swap(strings, 0, 3);
System.out.println(strings);
}
private static void shuffle随机置换() {
//shuffle()随机置换
ArrayList<String> strings = new ArrayList<>();
strings.add("1");
strings.add("3");
strings.add("2");
strings.add("4");
Collections.shuffle(strings);
System.out.println(strings);
}
private static void sort安照列表中的自然顺序排序() {
ArrayList<String> strings = new ArrayList<>();
strings.add("1");
strings.add("3");
strings.add("2");
strings.add("4");
Collections.sort(strings);
System.out.println(strings);
}
private static void method4() {
ArrayList<String> strings = new ArrayList<>();
strings.add("1");
strings.add("2");
strings.add("3");
strings.add("4");
Collections.reverse(strings);
System.out.println(strings);
}
private static void method3() {
ArrayList<String> strings = new ArrayList<>();
strings.add("1");
strings.add("2");
strings.add("3");
strings.add("4");
Collections.fill(strings, "a");
for (String string : strings) {
System.out.println(string);
}
}
private static void method2() {
//static void copy(List dest,List src):是把源列表中的数据覆盖到目标列表
//创建源列表
ArrayList<String> src = new ArrayList<>();
src.add("hello");
src.add("world");
src.add("java");
//创建目标列表
ArrayList<String> dest = new ArrayList<>();//目标列表长度必须大于或者大于源列表
dest.add("hehe");
dest.add("hehe");
dest.add("hehe");
dest.add("hehe");
Collections.copy(dest, src);
for (String s : dest) {
System.out.println(s);
}
}
private static void method1() {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
int i = Collections.binarySearch(list, 3);//第二个参数为要找的值
System.out.println(i);
}
}
Map
* Map常用的功能:
* 添加功能:
* V put(K key,V value)
* 获取功能:
* V get(Object key)
* int size()
* 判断功能:
* boolean containsKey(Object key)
* boolean containsValue(Object value)
* boolean isEmpty()
* 删除功能:
* void clear()
* V remove(Object key)
* 遍历功能:
* void clear()
* V remove(Object key)
* 遍历功能:
* Set<Map.Entry<K,V>> entrySet()
* Set<K> keySet()
* Collection<V> values()
*
*/
public class MapDemo {
public static void main(String[] args) {
//创建Map对象
Map<String, String> map = new HashMap<>();
map.put("1", "张三");
map.put("1", "张三");
map.put("2", "李四");
// map.put("1", "李四");//这样子会被覆盖掉,map的key不能重复
Set<String> set = map.keySet();
for (String s : set) {
System.out.println(s+"\t\t\t"+map.get(s));
}
map.containsKey("0001");//判断指定key是否存在
map.containsValue("张三");//判断Value值是否存在
// 第二种遍历
System.out.println("-----------------------------------------------");
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
System.out.println(entry.getKey()+"\t\t\t"+entry.getValue());
}
System.out.println("-----------------------------------------------");
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next().getKey();
System.out.println(key+"\t\t"+map.get(key));
}
}
}
总结
set无序且值不能相同,map的key不能相同