Java常用类2


Collection

ArrayList<String> list = new CollectionS();
list.add("cc");
list.add("bb");
Collections.addAll(list,"ee","dd","ff");
Collections.addAll(list,new String[]{"gg","hh"});

Collections.sort(list);
Collections.binarySearch(list, key:"cc");

Collections.fill(list2,obj:"yyy");

ArrayList

//接口不能创建对象,利用实现类创建对象
Collections col = new ArrayList();

//增
col.add(18);
col.add(12);
col.add(11);
col.add(17);
System.out.println(col);//[18,12,11,17]

List<Integer> list = Arrays.asList(new Integer[]{11,15,3,7,1});
col.addAll(list);
System.out.println(col);//[11,15,3,7,1]

//删
col.clear();
col.remove(element : 15);

//改


//查
col.size()

for(Object o : col){
    System.out.println(o);
}

Iterator it = col.Iterator();
while(it.hasNext()){
    System.out.println(it.next());
}


//判断
col.contains(element : 15);
col.equals(col2);
col.isEmpty();

List

List<Integer> list = new ArrayList();

//增
list.add(13);
list.add(17);
list.add(6);
list.add(index : 3, element : -1);

//删
list.remove(index : 2);
list.remove(element : "abc");

//改
list.set(index : 3, element : 77);

//查
list.get(index : 0);


LinkedList

LinkedList<String> list = new LinkedList<>();

//增
list.add("aaaa");
list.add("bbbb");
list.add("cccc");
list.add("dddd");

list.addFirst("jjjj");
list.addLast("hhhh");

list.offer("kkkk");
list.offerFirst("pppp");
list.offerLast("rrrr");
//删
list.poll();
list.pollFirst();
list.pollLast();

list.removeFirst();
list.removeLast();
//改

//查
list.element();//检索第一个元素

list.getFirst();
list.getLast();

list.indexOf(Object o);//返回指定元素首次出现的索引,没有则返回-1
list.lastIndexOf(Object o);//返回指定元素最后一次出现的索引,没有则返回-1

list.peek();//查看第一个元素
list.peekFirst();
list.peekLast();

Set

Set <String> strs=new HashSet<>();

//增
strs.add("aaa");
strs.addAll(list);

//删
strs.clear();
strs.remove("aaa");

//改
strs.toArray();

//查
strs.isEmpty();
strs.size();

TreeSet

Comparator<Student> com = new Comparator<Student>(){
    @Override
    public int compare(Student o1, Student o2){
        return o1.getName().compareTo(o2.getName());
    }
};
TreeSet<Student> ts = new TreeSet(com);
ts.add(new Student(age:10, name:"alili"));
ts.add(new Student(age:10, name:"blili"));
ts.add(new Student(age:10, name:"clili"));
ts.add(new Student(age:10, name:"dlili"));
ts.add(new Student(age:10, name:"elili"));

Map

Map<String, Integer> map = new HashMap<>();

//增
map.put(K key,V value);

//删
map.clear();
map.remove(Obkect key);

//改

//查
map.entrySet();//返回<key,value>集合
Set<Map.Entry<String, Integer>> entries = map.entrySet();
for(Map.Entry<String, Integer> e:entries){
    e.getKey();
    e.getValue();
}

map.get(Object key);
map.keySet();//返回key集合
map.size();
map.values();//返回values集合

//判断
map.containsKey(Object key);
map.containsValue(Object value);
map.equals(Object o);
map.isEmpty();

TreeMap

Map<Student, Integer> map = new TreeMap<>(new Comparator<Student>(){
    @Override
    Public int compare(Student o1, Student o2){
        return ((Double)(o1.getHeight())).compareTo((Double)(o2.getHeight()));
    }
})

map.put(new Student(age:19,name:"ailili",height:170.6),1001);
map.put(new Student(age:18,name:"bilili",height:181.6),1002);
map.put(new Student(age:17,name:"cilili",height:175.6),1003);
map.put(new Student(age:16,name:"dilili",height:166.6),1004);

Author: WTY
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source WTY !
  TOC