Set:零个或多个不重复组成的无序数组组成的集合
表达方式
- HashSet:元素存放顺序和添加进去时候的顺序没有任何关系
- TreeSet:对我们的Set中的元素进行排序存放
- LinkedHashSet :作为HashSet的子类:保持元素的添加顺序
- EnumSet
set.add(5)//adding 添加(无顺序概念)
membership set.contains(5)//checking 是否存在
set.remove(5)//remove
set.addAll(c)//union set ∪ c
set.retainAll(c)//intersection set ∩ c
set.removeAll(c)//difference set \ c
set.containsAll(c)//contain or subset check whether set ⊇ c
set.size()//size
contents set.clear()//clear
用HashSet的方式表达
Set<Integer> set = new HashSet<>();
import java.util.HashSet;
import java.util.Set;
/**
*
* @author
*/
public class STE {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("B");
set.add("C");
set.add("A");
System.out.println(set.contains("D"));
}
}
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class set1 {
public static void main(String[] args) {
Set<Integer> set1 = new HashSet<>();
set1.addAll(Arrays.asList(new Integer[]{1, 3}));//从列表创建集合(从数组创建)
Set<Integer> set2 = new HashSet<>();
set2.addAll(Arrays.asList(new Integer[]{2, 5, 4}));
set1.addAll(set2);
System.out.println("set1 = set1 ∪ set2 = " + set1);
System.out.println(set1.containsAll(set2));
Set<Integer> intersectionSet = new HashSet<>(set1);//从另一个集合创建集合
Set<Integer> set3 = new HashSet<Integer>();
set3.addAll(Arrays.asList(new Integer[]{1, 5, 6, 7}));
intersectionSet.retainAll(set3);
System.out.println("set1 ∩ set3 = " + intersectionSet);
}
}
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Set2 {
public static void main(String[] args) {
Set<Integer> set4 = new HashSet<>();
set4.addAll(Arrays.asList(new Integer[]{1, 2, 3, 4, 5}));
Set<Integer> set5 = new HashSet<>();
set5.addAll(Arrays.asList(new Integer[]{2, 5}));
Set<Integer> setDifference = new HashSet<>(set4);
setDifference.removeAll(set5);
System.out.println("set4 \\ set5 = " + setDifference);
setDifference.remove(1);
System.out.println(setDifference);
}
}
Map:键-值对(key-value)集合
Map 集合中的每一个元素都包含一个键对象和一个值对象。其中,键对象不允许重复,而值对象可以重复,并且值对象还可以是 Map 类型的,就像数组中的元素还可以是数组一样。
表达方式
- TreeMap
- HashMap
- LinkedHashMap
map.put(key, val) //adding the mapping key → val 添加映射键→ 值
map.get(key) //get the value for a key 获取键的值
map.containsKey(key) //test whether the map has the key 测试映射是否具有密钥
map.containsValue(value) //test whether the map has the value 测试映射是否具有该值
map.remove(key) //delete a mapping 删除映射
map.keySet() //returns a set containing all keys 返回包含所有键的集合
eg.
假设我们有一个包含多个重复单词的字符串列表,例如:[“a”,“b”,“a”、“c”、“b”、“单词”、“a”和“单词”],我们想知道哪些单词出现在列表中,每个单词出现多少次,下面是使用Map的NumWords算法:
- 创建映射<String,Integer>
- 循环遍历列表中的所有字符串
- 将每个字符串用作地图中的键
- 使用Integer值存储该字符串的显示次数
- 有两种情况:
- 当我们第一次看到一个单词时,它还没有出现在地图上,我们就添加它
- 当我们稍后看到这个单词时,它已经在地图中,我们会更新它
import java.util.HashMap;
import java.util.Map;
public class NumWords {
public Map<String, Integer> wordCount(List<String> list) {
Map<String, Integer> map = new HashMap<>();
int num;
for (String word : list) {
if (!map.containsKey(word)) {//我们第一次看到字符串
map.put(word, 1);
}
else {
num = map.get(word);
map.put(word, num + 1);//我们增加单词的数量
}
}
return map;
}
}