HashSet
hashset是set接口的一种实现。set中不允许有重复的元素。treeset也是set的一种实现,区别在于treeset是有序的。
例子:用hashset求两个数组的交集用retainAll的方法。
public int[] intersection(int[] nums1, int[] nums2) {
//用set1保存nums1数组种的不重复元素
Set<Integer> set1 = new HashSet<>();
//用set2保存nums2数组种的不重复元素
Set<Integer> set2 = new HashSet<>();
for (int num1:
nums1) {
set1.add(num1);
}
for (int num2:
nums2) {
set2.add(num2);
}
//求set1和set2的交集
set1.retainAll(set2);
return Arrays.stream(set1.toArray(new Integer[0]))
.mapToInt(Integer::valueOf)
.toArray();
}
ArrayList
基于动态数组的数据结构,对于随机的get和set较多的场景比较适用。对于add和remove较多的场景。可以适用LinkedList。
public static void main(String[] args) {
List<String> arrList = new ArrayList<>();
arrList.add("苹果");
arrList.add("梨子");
arrList.add("香蕉");
arrList.add("苹果");
arrList.add("菠萝");
arrList.add("香蕉");
arrList.remove("梨子");
System.out.println("货架上有商品??");
System.out.println(arrList.toString());
System.out.println("货架第三格摆的是??");
System.out.println(arrList.get(2));
}
LinkedList
linkedList是一个双向链表。当插入删除频繁时,适合采用linkedList。
public static void main(String[] args) {
List<String> lkList = new LinkedList<>();
lkList.add("苹果");
lkList.add("梨子");
lkList.add("香蕉");
lkList.add("苹果");
lkList.add("菠萝");
lkList.add("香蕉");
lkList.remove("梨子");
System.out.println("货架上有商品??");
System.out.println(lkList.toString());
System.out.println("--------------------------------");
((LinkedList<String>) lkList).push("哈密瓜");
System.out.println("(push进哈密瓜之后)货架上有商品??");
System.out.println(lkList.toString());
System.out.println("--------------------------------");
((LinkedList<String>) lkList).offer("苹果");
System.out.println("(offer苹果之后)货架上有商品??");
System.out.println(lkList.toString());
}
HashMap
HashMap由Node数组和链表共同构成,JDK8对HashMap做了优化,采用了红黑树的数据结构来做优化,使得HashMap存取速度更快。
当需要存储键值对,键值不重复的场景下,需要根据键值快速查找的情况下,HashMap很好用。
/**
* 随机产生一万个(0~3)的数字,统计各个数字出现的次数。
* @param args
*/
public static void main(String[] args) {
Map<Integer,Integer> map01 = new HashMap<>();
int[] nums = new int[10_000];
Random rm = new Random();
for (int i = 0; i < nums.length; i++) {
nums[i] = rm.nextInt(4);
}
for (int i:
nums) {
if(map01.containsKey(i)){
map01.put(i,map01.get(i)+1);
}else{
map01.put(i,1);
}
}
Iterator<Map.Entry<Integer,Integer>> it = map01.entrySet().iterator();
while (it.hasNext()){
Map.Entry<Integer,Integer> entry = it.next();
System.out.println(entry.getKey()+"出现的次数:"+entry.getValue());
}
}
Collections工具类
public static void main(String[] args) {
// int[] nums = new int[]{23,989,77,73,9,8,2};
// List list=new ArrayList<Integer>();
// list.addAll(
// IntStream.of(nums).boxed().collect(Collectors.toList())
// );
List list=new ArrayList<Integer>(){{
add(23);
add(989);
add(77);
add(73);
add(9);
add(8);
add(2);
}};
System.out.println("排序后:");
Collections.sort(list);//自然排序
System.out.println(Arrays.toString(list.toArray()));
System.out.println("------------------------------------------------------");
System.out.println("重新洗牌后:");
Collections.shuffle(list);//自然排序
System.out.println(Arrays.toString(list.toArray()));
System.out.println("------------------------------------------------------");
System.out.println("数组反转之后:");
Collections.reverse(list);
System.out.println(Arrays.toString(list.toArray()));
System.out.println("------------------------------------------------------");
System.out.println("数组所有元素向右移动2位");
Collections.rotate(list,2);
System.out.println(Arrays.toString(list.toArray()));
System.out.println("------------------------------------------------------");
System.out.println("数组中的最大值:");
System.out.println(Collections.max(list));
System.out.println("------------------------------------------------------");
}
Arrays工具类
public static void main(String[] args) {
int[] nums = new int[]{13,5,6,8,3,7,10};
System.out.println("排序数组:");
Arrays.sort(nums);
System.out.println(Arrays.toString(nums));
System.out.println("------------------------------------------------------");
System.out.println("拷贝到新的数组:");
int[] nums2 = Arrays.copyOf(nums, nums.length);
System.out.println(Arrays.toString(nums2));
System.out.println("------------------------------------------------------");
System.out.println("查找13这个元素的位置:");
System.out.println(Arrays.binarySearch(nums,13));
}