开发者

What should be the logic of arranging data of same type in Set collection

开发者 https://www.devze.com 2023-03-17 19:57 出处:网络
What I am doing I am reading all contacts from Phone, and storing them in to a Set. Syntax of Set is Set<String> contactNumbers =new HashSet<String>(); . And I am using this code to read

What I am doing

I am reading all contacts from Phone, and storing them in to a Set. Syntax of Set is Set<String> contactNumbers = new HashSet<String>(); . And I am using this code to read contacts

phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    new String[]{Con开发者_如何学PythontactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE},
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, // We need to add more selection for phone type
                    null,
                    null);
            if(phones != null) {
                while(phones.moveToNext()){
                    switch(phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))){
                        case Phone.TYPE_MOBILE :
                            contactNumbers.add(phones.getString(0).trim() + "M");
                            break;
                        case Phone.TYPE_HOME :
                            contactNumbers.add(phones.getString(0).trim() + "H");
                            break;
                        case Phone.TYPE_WORK :
                            contactNumbers.add(phones.getString(0).trim() + "W");
                            break;
                        case Phone.TYPE_OTHER :
                            contactNumbers.add(phones.getString(0).trim() + "O");
                            break;
                    }
                }
            }

Problem definition :

I want to arrange number with their category. Like if 5 numbers are with Home category 3 numbers of Work and 4 is from Other category, then I need to arrange all numbers in sort as "All numbers of a particular type should be in sequence". for example my Set contains first 5 numbers related to Home, then all 3 numbers of Work and then all 4 numbers of Other category. How can I implement this logic?


Use a TreeSet which is capable of sorting elements (either based on the natural order/using the comparator)

    Set<String> sortedSet=new TreeSet<String>();
    sortedSet.add("a");
    sortedSet.add("e");
    sortedSet.add("g");
    sortedSet.add("b");
    sortedSet.add("c");
    Iterator<String> it=sortedSet.iterator();
    while(it.hasNext())
    {
      String value=it.next();
      System.out.println("Value :"+value);
    }

Next Step, implement your own comparator which can sort the elements(contacts) based on the category and then supply this comparator to your TreeSet. Something like this.

Set sortedSet=new Treeset(new MyComparator());


Your phone numbers may be better represented as a class (see below).

public class PhoneNumber implements Comparable<PhoneNumber> {

    private final Phone type;
    private final String number;

    public PhoneNumber(final Phone type, final String number) {
        this.type = type;
        this.number = number;
    }

    public Phone getType() {
        return type;
    }

    public String getNumber() {
        return number;
    }

    /**
     * Sort by type, then number
     */
    @Override
    public int compareTo(final PhoneNumber other) {
        int typeComparison = getType().compareTo(other.getType());
        return typeComparison == 0 ? getNumber().compareTo(other.getNumber()) : typeComparison;
    }

    @Override
    public String toString() {
        return getType() + ":" + getNumber();
    }

}

If that class implements Comparable, you'll get sorting for free if you use TreeSet, as follows:

final Set<PhoneNumber> contactNumbers = new TreeSet<PhoneNumber>();
contactNumbers.add(new PhoneNumber(Phone.TYPE_HOME, "1"));
contactNumbers.add(new PhoneNumber(Phone.TYPE_MOBILE, "6"));
contactNumbers.add(new PhoneNumber(Phone.TYPE_OTHER, "3"));
contactNumbers.add(new PhoneNumber(Phone.TYPE_HOME, "2"));
contactNumbers.add(new PhoneNumber(Phone.TYPE_WORK, "4"));
contactNumbers.add(new PhoneNumber(Phone.TYPE_MOBILE, "5"));

// Prints [TYPE_MOBILE:5, TYPE_MOBILE:6, TYPE_HOME:1, TYPE_HOME:2, TYPE_WORK:4, TYPE_OTHER:3]
System.out.println(Arrays.toString(contactNumbers.toArray()));


A Set is not a Collection that supports order - you should use a List of some sort (e.g. ArrayList, or LinkedList)

As for sorting itself - if you use a List - you need to define a Comperator and use it to sort the List using Collections.sort(List, Comperator).

0

精彩评论

暂无评论...
验证码 换一张
取 消