此篇所写 不知道你们是否在网页开发的时候 当看到要写Map集合什么HashMap之类的突然蒙了 虽然之前学过 突然让你调用方法 懵了 所以在此总结一下 以备后需
对比数组 可存储任意类型对象 且存储长度是可以变的 集合类乃内存层面对数据进行存储 数据库是持久层 断电后仍长期存在
都来自这个包:java.util.*
List:
package List;
import pojo.Student;
import java.util.ArrayList;
public class ArrayListTest {
public static void main(String[] args) {
/*
* 有序不唯一
* ArrayList适合元素的查找
* */
ArrayList<Object> one = new ArrayList<>();
one.add("高远");
one.add("哈哈");
one.add("文雅");
one.add("文雅");
Student student = new Student(1,"草地");
one.add(student);
System.out.println(one);
System.out.println(one.get(0));
}
}
package List;
import pojo.Student;
import java.util.Arrays;
import java.util.LinkedList;
public class LinkedListTest {
public static void main(String[] args) {
/*
* 有序不唯一
* LinkedList适合大量的增删操作
* foreach循环不可以对元素进行修改
*/
LinkedList<Object> one = new LinkedList<>();
Student student = new Student(1, "高远");
one.add(student);
one.add("student");
one.add("哈哈");
one.add("哈哈");
System.out.println(one);
for (Object o : one) {
System.out.println(o);
}
}
}
Set:
package Set;
import pojo.Student;
import java.util.HashSet;
public class HashSetTest {
public static void main(String[] args) {
/*
* 无序唯一
* HashSet add()->调用hashCode()方法->根据元素的哈希值计算元素存储位置->判断是否有元素/是否相等
* 若使用Student对象 必须重写hashCode()和equals()方法
*/
HashSet<Object> one = new HashSet<>();
one.add("ok");
one.add("ok");
one.add("高远");
System.out.println(one);
HashSet<Object> two = new HashSet<>();
Student student1 = new Student(1, "高远");
Student student2 = new Student(2, "哈哈");
Student student3 = new Student(2, "高远");
two.add(student1);
two.add(student2);
two.add(student3);
System.out.println(two);
}
}
package Set;
import java.util.LinkedHashSet;
public class LinkedHashSetTest {
public static void main(String[] args) {
/*
* 唯一 LinkedHashSet让存取顺序一致(有序)
*/
LinkedHashSet<Object> one = new LinkedHashSet<>();
one.add("高远");
one.add("哈哈");
one.add("ok");
one.add("高远");
System.out.println(one);
}
}
package Set;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
/*
* 无序 唯一
* TreeSet利用二叉树存储元素 来去掉重复元素
*/
TreeSet<Object> one = new TreeSet<>();
one.add("高远");
one.add("哈哈");
one.add("高远");
System.out.println(one);
}
}
Map:
package Map;
import pojo.Student;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class HashMapTest {
public static void main(String[] args) {
/*
* 键唯一 值可以重复
* 若键相同 值覆盖 HashMap 元素无序
*/
HashMap<String, Object> one = new HashMap<>();
one.put("1", new Student(1, "高远"));
one.put("2", new Student(2, "哈哈"));
one.put("3", new Student(3, "白台"));
one.put("4", new Student(1, "高远"));
one.put("1", new Student(4, "高远2"));
Set<String> keySet = one.keySet();
Iterator<String> it = keySet.iterator();
while (it.hasNext()) {
String key = it.next();
Object value = one.get(key);
System.out.println(key + " " + value);
}
}
}
package Map;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class LinkedHashMapTest {
public static void main(String[] args) {
/*
* 键唯一 值可以重复
* 若键相同 值覆盖
* LinkedHashMap有序(存入顺序)
*/
Map one = new HashMap<>();
one.put("2", "高远");
one.put("4", "哈哈");
one.put("3", "高远");
System.out.println(one);
Map two = new LinkedHashMap();
two.put("2", "高远");
two.put("4", "哈哈");
two.put("3", "高远");
two.put("2", "高远2");
System.out.println(two);
}
}
package Map;
import java.util.Map;
import java.util.TreeMap;
public class TreeMapTest {
public static void main(String[] args) {
/*
* 键唯一 值可以重复
* 若键相同 值覆盖
* TreeMap 键有序
*/
Map one = new TreeMap<>();
one.put(2, "高远");
one.put(5, "哈哈");
one.put(3, "ok");
one.put(2, "高远2");
System.out.println(one);
}
}
还有一个比较常用的Arrays工具类:
参考 https://www.cnblogs.com/soberw/p/15876454.html#asList_28
标签:Map,Set,Java,高远,add,Student,import,new,public From: https://www.cnblogs.com/gaodiyuanjin/p/18416552