主要的集合类: ArrayList LinKeList HashSet(HashMap的key,存储在HashMap集合的元素需要同时重写hashcode + equals) TreeSet HashMap TreeMap Properties
Collection集合的基本操作:
List:
public class ArrayListTest {
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<>();
al.add("I");
al.add("Just");
al.add("HAHA");
//通过下标取出元素
System.out.println(al.get(0));//输出I
//遍历(通过下标遍历)
for(int i = 0;i < al.size();i++){
String elt = al.get(i);
System.out.println(elt);
}
//遍历(迭代器,所有的Collection集合通用)
Iterator it = al.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//增强for循环
for(String s : al){
System.out.println(s);
}
}
}
HashSet:
/**
* Set集合:无序不可重复
*/
public class HashSetTest {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("haha");
set.add("heihei");
set.add("xixi");
//set集合中的元素没有下标,无法通过下标取出,若想取出可将Set转换成List
//遍历(迭代器)
Iterator it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//遍历(增强for循环)
for(String s : set){
System.out.println(s);
}
Set<Student> students = new HashSet<>();
students.add(new Student(1,"xiaoming"));
students.add(new Student(3,"xiaohong"));
students.add(new Student(2,"xiaohei"));
students.add(new Student(2,"xiaohei"));
System.out.println(students.size());//输出3,无序不可重复
//遍历
for(Student s : students){
System.out.println(s);
}
}
}
class Student{
int no;
String name;
public Student(int no, String name){
this.no = no;
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return no == student.no && name.equals(student.name);
}
@Override
public int hashCode() {
return Objects.hash(no, name);
}
}
TreeSet:
**
* Treeset:无序不可重复
*/
public class TreeSetTest {
public static void main(String[] args) {
//TreeSet泛型中写入String,Integer等类型时,遍历输出时默认为升序排序
//若想修改排序规则,可在创建对象时传入比较器对象
TreeSet<Integer> ts = new TreeSet<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;//降序
}
});
ts.add(123);
ts.add(3211);
ts.add(21);
ts.add(22);
//遍历
for(Integer i : ts){
System.out.println(i);
}
//测试自定义类
TreeSet<A> treeset = new TreeSet<>();
treeset.add(new A(12,"xi"));
treeset.add(new A(11,"sdw"));
treeset.add(new A(2,"dsw"));
System.out.println(treeset.size());//输出3
for(A a : treeset){
System.out.println(a);
}
}
}
class A implements Comparable<A>{
int i;
String name;
public A(int i,String name){
this.i = i;
this.name = name;
}
@Override
public String toString() {
return "A{" +
"i=" + i +
", name='" + name + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
A a = (A) o;
return i == a.i && name.equals(a.name);
}
@Override
public int hashCode() {
return Objects.hash(i, name);
}
@Override
public int compareTo(A o) {
return this.i - o.i;//升序
}
}
HashMap:
public class HashMapTest {
public static void main(String[] args) {
Map<Integer,String> map = new HashMap<>();
map.put(1,"xiaoming");
map.put(2,"xiaoh");
map.put(3,"xiaobai");
map.put(3,"lihua");//key与上面那个相同,lihua覆盖xiaobai
//通过key取出value
System.out.println(map.get(3));//输出 lihua
//遍历
//第一种方式(效率较低),通过获取全部key,然后遍历出value
Set<Integer> set = map.keySet();
for(Integer key : set){
System.out.println(key +"="+ map.get(key));
}
//第二种方式,将Map集合转换为Set集合
Set<Map.Entry<Integer,String>> nodes = map.entrySet();
for(Map.Entry<Integer,String> nood : nodes){
Integer keys = nood.getKey();
String values = nood.getValue();
System.out.println(keys +"="+ values);
}
}
}