查询快,增删慢
查询慢,增删快
//ArrayList实现 import java.util.*; public class Main { public static void main(String[] args) { ArrayList<String> a=new ArrayList<String>(); a.add("hello"); a.add("world"); a.add("java"); Iterator<String> it=a.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } } //LinkedList实现 import java.util.*; public class Main { public static void main(String[] args) { LinkedList<String> ll=new LinkedList<String>(); ll.add("hello"); ll.add("world"); ll.add("java"); for(String s:ll){ System.out.println(s); } } }
import java.util.*; public class Main { public static void main(String[] args) { ArrayList<DateUtils> array=new ArrayList<DateUtils>(); DateUtils s1=new DateUtils("lili",19); DateUtils s2=new DateUtils("zhangliang",20); DateUtils s3=new DateUtils("wangwu",18); array.add(s1); array.add(s2); array.add(s3); //增强for循环 for(DateUtils s:array){ System.out.println(s.getName()+","+s.getAge()); } //普通for循环 for(int i=0;i<array.size();i++){ System.out.println(i); } //迭代器 while(it.hasNext()){ DateUtils s=it.next(); System.out.println(s.getName()+","+s.getAge()); } } }
import java.util.*; public class Main { public static void main(String[] args) { LinkedList<String> ll=new LinkedList<String>(); //在第一位添加指定元素 ll.addFirst("hello"); //在末尾添加指定元素 ll.addLast("world"); //在第一位删除元素 ll.removeFirst(); //删除末尾元素 ll.removeLast(); //返回首位元素 System.out.println(ll.getFirst()); //返回末尾元素 System.out.println(ll.getLast());
import java.util.*; public class Main { public static void main(String[] args) { Set<String> ss=new HashSet<String>(); ss.add("hello"); ss.add("world"); ss.add("java"); //不可重复 ss.add("hello"); //加强for循环遍历 for(String s:ss){ System.out.println(s); } } }
public int hashCode();//返回哈希值 //同一个对象多次调用返回的哈希值是一样的 //默认情况下,不同对象,即便内容相同,返回的哈希值是不一样的 //在重写了hashCode方法的情况下,可以使得哈希值相同 System.out.println(s.hashCode());//得到某位对象的哈希值
import java.util.*; public class Main { public static void main(String[] args) { HashSet<String> hh=new HashSet<String>(); hh.add("hello"); hh.add("world"); hh.add("java"); //增强for循环遍历 for(String s:hh){ System.out.println(s); } } }
//Main.java import java.util.*; public class Main { public static void main(String[] args) { HashSet<Student> hh=new HashSet<Student>(); Student s1=new Student("lili",19); Student s2=new Student("zhangliang",20); Student s3=new Student("wangwu",18); hh.add(s1); hh.add(s2); hh.add(s3); for(Student s:hh){ System.out.println(s.getName()+","+s.getAge()); } } } //Student.java public class Student { private String name; private int age; public Student(){} public Student(String name, int age){ this.name=name; this.age=age; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; }
import java.util.*; public class Main { public static void main(String[] args) { LinkedHashSet<String> ll=new LinkedHashSet<String>(); ll.add("hello"); ll.add("world"); ll.add("java"); for(String s:ll){ System.out.println(s); } } }
import java.util.*; public class Main { public static void main(String[] args) { TreeSet<Integer> tt=new TreeSet<Integer>(); tt.add(10); tt.add(100); tt.add(1000); for(Integer mm:tt){ System.out.println(mm); } } }
//Main.java import java.util.*; public class Main { public static void main(String[] args) { TreeSet<Student> ss=new TreeSet<Student>(); Student s1=new Student("lili",19); Student s2=new Student("zhangliang",20); Student s3=new Student("wangwu",18); ss.add(s1); ss.add(s2); ss.add(s3); for(Student s:ss){ System.out.println(s.getName()+","+s.getAge()); } } } //Student.java //按照存储顺序 public class Student implements Comparable<Student>{ private String name; private int age; public Student(){} public Student(String name, int age){ this.name=name; this.age=age; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; } @Override public int compareTo(Student o) { return 1; } } //反向存储顺序 public class Student implements Comparable<Student>{ private String name; private int age; public Student(){} public Student(String name, int age){ this.name=name; this.age=age; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; } @Override public int compareTo(Student o) { return -1; } } //按照年龄顺序 public class Student implements Comparable<Student>{ private String name; private int age; public Student(){} public Student(String name, int age){ this.name=name; this.age=age; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; } @Override public int compareTo(Student o) { int num=this.age-o.age; return num; } } //反向年龄顺序 public class Student implements Comparable<Student>{ private String name; private int age; public Student(){} public Student(String name, int age){ this.name=name; this.age=age; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; } @Override public int compareTo(Student o) { int num=o.age-this.age; return num; } } //年龄相同时,按照名字顺序排序 public class Student implements Comparable<Student>{ private String name; private int age; public Student(){} public Student(String name, int age){ this.name=name; this.age=age; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; } @Override public int compareTo(Student o) { int num=this.age-o.age; int num1=num==0?this.name.compareTo(o.name):num; return num1; } }
//Main.java import java.util.*; public class Main { public static void main(String[] args) { TreeSet<Student> ss=new TreeSet<Student>(new Comparator<Student>(){ @Override public int compare(Student s1,Student s2){ int num=s1.getAge()-s2.getAge(); int num2=num==0?s1.getName().compareTo(s2.getName()):num; return num2; } }); Student s1=new Student("lili",19); Student s2=new Student("zhangliang",20); Student s3=new Student("wangwu",18); ss.add(s1); ss.add(s2); ss.add(s3); for(Student s:ss){ System.out.println(s.getName()+","+s.getAge()); } } } //Student.java public class Student implements Comparable<Student>{ private String name; private int age; public Student(){} public Student(String name, int age){ this.name=name; this.age=age; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; } @Override public int compareTo(Student o) { int num=this.age-o.age; int num1=num==0?this.name.compareTo(o.name):num; return num1; } }
//Main.java import java.util.*; public class Main { public static void main(String[] args) { TreeSet<Student> ss=new TreeSet<Student>(new Comparator<Student>(){ @Override public int compare(Student s1,Student s2){ //主要条件 int num=(s2.getYuwen()+s2.getMath())-(s1.getYuwen()+s1.getMath()); //次要条件 int num2=num==0?s1.getYuwen()-s2.getYuwen():num; int num3=num2==0?s1.getName().compareTo(s2.getName()):num2; return num3; } }); Student s1=new Student("lili",100,98); Student s2=new Student("zhangliang",20,79); Student s3=new Student("wangwu",98,76); ss.add(s1); ss.add(s2); ss.add(s3); for(Student s:ss){ System.out.println(s.getName()+","+s.getYuwen()+","+s.getMath()); } } } //Student.java public class Student implements Comparable<Student>{ private String name; private int age; private int yuwen; private int math; public Student(){} public Student(String name, int age){ this.name=name; this.age=age; } public Student(String name,int yuwen,int math){ this.name=name; this.yuwen=yuwen; this.math=math; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; } public void setYuwen(int yuwen){ this.yuwen=yuwen; } public int getYuwen(){ return yuwen; } public void setMath(int math){ this.math=math; } public int getMath(){ return math; }
//HashSet--->不进行排序 import java.util.*; public class Main { public static void main(String[] args) { Set<Integer> tt=new TreeSet<Integer>(); Random r=new Random(); while(tt.size()<10){ int number=r.nextInt(20)+1; tt.add(number); } for(Integer i:tt){ System.out.println(i); } } } //TreeSet--->进行大小排序 import java.util.*; public class Main { public static void main(String[] args) { Set<Integer> tt=new HashSet<Integer>(); Random r=new Random(); while(tt.size()<10){ int number=r.nextInt(20)+1; tt.add(number); } for(Integer i:tt){ System.out.println(i); } } }
基础练习:
import java.util.*; public class Main { public static void main(String[] args) { Collection c=new ArrayList(); c.add("hello"); c.add("world"); c.add("java"); c.add(100); Iterator it=c.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } }
//定义格式 修饰符 class 类名<类型>{}
//DateTest.java public class DateTest<T>{ private T t; public T getT(){ return t; } public void setT(T t){ this.t=t; } } //Main.java public class Main { public static void main(String[] args) { DateTest<String> ll=new DateTest<String>(); ll.setT("lili"); System.out.println(ll.getT()); DateTest<Integer> i=new DateTest<Integer>(); i.setT(100); System.out.println(i.getT()); } }
//格式 修饰符<类型> 返回值类型方法名(类型 变量名){}
//Main.java public class Main { public static void main(String[] args) { DateTest s=new DateTest(); s.show("hello"); s.show(100); } } //DateTest.java public class DateTest{ public <T>void show(T t){ System.out.println(t); } }
//格式 修饰符 interface 接口名<类型>{}
//Main.java public class Main { public static void main(String[] args) { Fanxing<String> ff=new DateTest<String>(); ff.show("wangwu"); Fanxing<Integer> ii=new DateTest<Integer>(); ii.show(100); Fanxing<Boolean> bb=new DateTest<Boolean>(); bb.show(true); } } //Fanxing.java public interface Fanxing<T> { void show(T t); } //DateTest.java public class DateTest<T> implements Fanxing<T>{ public void show(T t){ System.out.println(t); } }
//类型通配符 <?> //类型通配符的上限(谨代表某一类泛型list的父类) <?extends类型> //类型通配符的下限,代表某一类的子类 <?super类型>
//类型通配符 List<?> ll=new ArrayList<>(); List<?> ll2=new ArrayList<Number>(); List<?> ll3=new ArrayList<Integer>(); //类型通配符的上限 List<?extends Number> ll4=new ArrayList<Number>(); List<?extends Number> ll5=new ArrayList<Integer>(); //类型通配符的下限 List<?super Number> ll6=new ArrayList<>(); List<?super Number> ll7=new ArrayList<Number>();
//格式 修饰符 返回值类型 方法名(数据类型...变量名){}
public class Main { public static void main(String[] args) { System.out.println(sum(10)); System.out.println(sum(10,20)); System.out.println(sum(10,20,30)); } public static int sum(int ...a){ int sum=0; for(int i:a){ sum+=i; } return sum; } }
注意:如果需要将某个普通参数与可变参数放到一起,则需要将可变参数放到普通参数的后面。
public static <T>list<T> asList(T...a);//返回由指定数组支持的固定大小的列表 List<String> list=Arrays.asList("hello","world","java"); //该方法构造的列表,不能通过add和remove的操作,能进行set操作。 public static <E>list<E> of(E...elements);//返回包含任意数量元素的不可变列表 List<String> ll=List.of("hello","world","java"); //该方法构造的列表,不能通过add和remove的操作,set操作也不行。 public static <E>Set<E> of(E..elements);//返回一个包含任意数量元素的不可变集合 Set<String> ll1=Set.of("hello","world","java"); //该方法构造的列表,不能通过add和remove的操作,没有set操作;不能出现重复的元素
interface Map<K,V> //K:键的类型 V:值的类型 //不可重复,一一对应,呈映射关系
//添加元素 Map<String,String> mm=new HashMap<String,String>(); mm.put("20214063","liuzijin");//添加元素 mm.put("20214056","liuyichen");//添加元素 mm.put("20214063","wangwu");//修改已有键的元素--->代替 System.out.println(mm);
V put(K key,V value);//添加元素 V remove(Object key);//根据键删除键值对元素 void clear();//移除所有的键值对元素 boolean containsKey(Object key);//判断集合是否包含指定的键 boolean containsValue(Object value);//判断集合是否包含指定的值 boolean isEmpty();//判断集合是否为空 int size();//集合的长度,也就是集合中键值对的个数
Map<String,String> map=new HashMap<String,String>(); //添加元素 map.put("1","wo"); //移除元素 map.remove("1");//System.out.println("1");---->会输出wo //清空元素 map.clear(); //判断集合是否包含指定的键 System.out.println(map.containsKey("1")); //判断集合是否包含指定的值 System.out.println(map.containsValue("1")); //判断集合是否为空 System.out.println(map.isEmpty()); //得到集合长度 System.out.println(map.size());