Java集合涉及的类(代码)

Customer:

public class Customer implements Comparable{
        private Integer customerId;
        private String customerName;
                
        public Integer getCustomerId() {
            return customerId;
        }
        public void setCustomerId(Integer customerId) {
            this.customerId = customerId;
        }
        public String getCustomerName() {
            return customerName;
        }
        public void setCustomerName(String customerName) {
            this.customerName = customerName;
        }
        public Customer(Integer customerId, String customerName) {
            this.customerId = customerId;
            this.customerName = customerName;
        }          
    @Override
        public String toString() {
            return "Customer [customerId=" + customerId + ", customerName="
                    + customerName + "]";
        }
    public Customer() {
        }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + customerId;
        result = prime * result
                + ((customerName == null) ? 0 : customerName.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Customer other = (Customer) obj;
        if (customerId != other.customerId)
            return false;
        if (customerName == null) {
            if (other.customerName != null)
                return false;
        } else if (!customerName.equals(other.customerName))
            return false;
        return true;
    }
     //TreeSet类按照compareTo方法比较返回
     //按id或name排序
    @Override
    public int compareTo(Object o) {
        if(o instanceof Customer){
            Customer c=(Customer) o;
            //给整体添加‘-‘号改变正/逆序
//            return this.customerId-c.customerId;                             
            return this.customerName.compareTo(c.customerName);            
        }       
        return 0;
    }        
}

CustomerComparator:

import java.util.Comparator;
public class CustomerComparator implements Comparator{
    @Override
    public int compare(Object o1, Object o2) {
        if(o1 instanceof Customer && o2 instanceof Customer){
            Customer cust1=(Customer) o1;
            Customer cust2=(Customer) o2;
            return -cust1.getCustomerName().compareTo(cust2.getCustomerName());
        }
        return 0;
    }
}

时间: 2024-08-01 00:14:36

Java集合涉及的类(代码)的相关文章

java集合框架--ArrayList类、Vector和LinkedList类

1.ArrayList类概述 底层数据结构是数组,查询块,增删慢. 线程不安全,效率高. 2.ArrayList案例 2.1存储字符串并遍历 package com; import java.util.ArrayList; import java.util.Iterator; import java.util.ListIterator; public class ArrayListDemo { public static void main(String[] args) { //创建ArrayL

java集合(工具类Arrays)

/* * Arrays:用于操作数组的工具类,里面都是静态方法. * toString方法:返回指定数组内容的字符串表示形式. * asList方法:返回一个受指定数组支持的固定大小的列表 * toArray方法:将集合变成数组. */ import java.applet.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo3 { p

java集合框架--工具类Collections

1.Collections概述 是针对集合操作的工具类. 2.Collection和Collections的区别? Collection:是单列集合的顶层接口,而Collections是针对集合操作的工具类. Collection有子接口List和Set,而Collections有对集合进行排序和二分查找的方法. 3.Collections工具类的功能 public static <T> void sort(List<T> list):默认情况下是对集合的自然排序. public

JAVA集合接口及类

各接口及类关系图 Iterable 所有集合的初始接口,实现该接口可进行foreach操作,只有一个iterator()方法,并返回iterator类型: Iterable在java.lang下,Iterator在java.util下,Iterator主要包括hasNext(), next(), remove(); 所有实现了Iterable就可以操作迭代器iterator, 但能使用迭代器iterator就未必实现了Iterable接口,如数组? 为什么需要Iterable? 因为Iterat

Java集合中那些类是线程安全的

线程安全类 在集合框架中,有些类是线程安全的,这些都是jdk1.1中的出现的.在jdk1.2之后,就出现许许多多非线程安全的类. 下面是这些线程安全的同步的类: vector:就比arraylist多了个同步化机制(线程安全),因为效率较低,现在已经不太建议使用.在web应用中,特别是前台页面,往往效率(页面响应速度)是优先考虑的. statck:堆栈类,先进后出 hashtable:就比hashmap多了个线程安全 enumeration:枚举,相当于迭代器 除了这些之外,其他的都是非线程安全

java集合框架工具类Collections,集合的操作

1 import java.util.*; public class asList { public static void main(String args[]) { // int arr[] = {1,2,3,4,45}; // List<int[]> list = Arrays.asList(arr); // list.add("qq"); 这里不能添加,因为数组的长度是固定的 // Integer[] nums = {2,4,5,2}; // List<Int

Java集合的常用类以及特点

集合的两个顶级接口分别为:Collection和Map Collection下有两个比较常用的接口分别是List(列表)和Set(集),其中List可以存储重复元素,元素是有序的(存取顺序一致),可以通过List脚标来获取指定元素;而Set不可以有重复元素,元素是无序的. List接口中,比较常用的类有三个:ArrayList.Vactor.LinkedList. ArrayList :线程不安全的,对元素的查询速度快. Vector :线程安全的,多了一种取出元素的方式:枚举(Enumerat

关于Java集合hashCode的总结

Java集合的hashCode是一个重要的知识点,在我们学习Java的过程中,也许会比较迷茫,所以这篇文章就给大家总结了一下. hashCode 的作用 在 Java 集合中有两类,一类是 List,一类是 Set 他们之间的区别就在于 List 集合中的元素师有序的,且可以重复,而 Set 集合中元素是无序不可重复的.对于 List 好处理,但是对于 Set 而言我们要如何来保证元素不重复呢?通过迭代来 equals() 是否相等.数据量小还可以接受,当我们的数据量大的时候效率可想而知(当然我

黑马程序员---Java集合框架

---------------------- Android开发.java培训.期待与您交流! ---------------------- Java集合框架 集合我们都知道是用来储存对象的容器,那之前的数组不也可以储存对象么,为什么要出现集合呢? 面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对象进行存储,然而集合类中提供很多方便操作对象存储的方法,要比数组更容易操作对象,而且集合的长度是可变的,然而数组长度确实固定不变的,这样不利于对对象的间隔储存.  数组和集