Capturing Duplicates from ArrayList - java

I'm having issues with removing duplicate objects from an ArrayList. Im parsing XML into what i call an IssueFeed object. This consists of a symptom, problem, solution.
Most of my objects are unique and don't share a symptom, problem, solution but some share the same symptom but have a different problem.
Im trying to accomplish several things.
Capture objects that share the same symptom as a duplicate Arraylist
Remove the duplicate items from the main list, leaving at least 1 item with that symptom to be displayed.
When the user clicks on the item that we know has duplicates, set the duplicate data Arraylist in my listview/adapter.
Steps i've taken.
I've tried sorting the objects and i am able to capture the duplicates, however not sure how to remove all but one from the main list.
2 Loops between the list and looking for objects that aren't themselves and symptom = symptom and then remove and update my duplicate array and main array.
Some code
IssueFeed - object
public IssueFeed(String symptom, String problem, String solution) {
this.symptom = symptom;
this.problem = problem;
this.solution = solution;
}
public String getSymptom() {
return symptom;
}
public String getProblem() {
return problem;
}
public String getSolution() {
return solution;
}
My ArrayList<IssueFeed>'s
duplicateDatalist = new ArrayList<IssueFeed>(); // list of objects thats share a symptom
list_of_non_dupes = new ArrayList<IssueFeed>(); // list of only objects with unique symptom
mIssueList = mIssueParser.parseLocally(params[0]); // returns ArrayList<IssueFeed> of all objects
I can obtain the duplicates by the following sort code below.
Collections.sort(mIssueList, new Comparator<IssueFeed>(){
public int compare(IssueFeed s1, IssueFeed s2) {
if (s1.getSymptom().matches(s2.getSymptom())) {
if (!duplicateDatalist.contains(s1)) {
duplicateDatalist.add(s1);
System.out.print("Dupe s1 added" + " " + s1.getSymptom() + ", " + s1.getProblem() + "\n");
}
if (!duplicateDatalist.contains(s2)) {
duplicateDatalist.add(s2);
System.out.print("Dupe s2 added" + " " + s2.getSymptom() + ", " + s2.getProblem() + "\n");
}
}
return s1.getSymptom().compareToIgnoreCase(s2.getSymptom());
}
});
Now i need to create the new list of non dupes, This code only added all of the objects. :/
for (int j = 0; j < mIssueList.size(); j++) {
IssueFeed obj = mIssueList.get(j);
for (int i = 0; i < mIssueList.size(); i++) {
IssueFeed obj_two = mIssueList.get(j);
if (obj.getSymptom().matches(obj_two.getSymptom())) {
if (!list_non_dupes.contains(obj_two)) {
list_non_dupes.add(obj_two);
}
break;
} else {
if (!list_non_dupes.contains(obj_two)) {
list_non_dupes.add(obj_two);
}
}
}
}

If you could modify the IssueFeed object the consider overrding the equals() and hashCode() methods and use a set to find duplicates. Eg
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
class IssueFeed {
private String symptom;
private String problem;
private String solution;
public IssueFeed(String symptom, String problem, String solution) {
this.symptom = symptom;
this.problem = problem;
this.solution = solution;
}
public String getSymptom() {
return symptom;
}
public String getProblem() {
return problem;
}
public String getSolution() {
return solution;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((symptom == null) ? 0 : symptom.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;
IssueFeed other = (IssueFeed) obj;
if (symptom == null) {
if (other.symptom != null)
return false;
} else if (!symptom.equals(other.symptom))
return false;
return true;
}
#Override
public String toString() {
return "IssueFeed [symptom=" + symptom + ", problem=" + problem
+ ", solution=" + solution + "]";
}
}
public class Sample {
public static void main(String[] args) {
List<IssueFeed> mainList = new ArrayList<IssueFeed>(
Arrays.asList(new IssueFeed[] {
new IssueFeed("sym1", "p1", "s1"),
new IssueFeed("sym2", "p2", "s2"),
new IssueFeed("sym3", "p3", "s3"),
new IssueFeed("sym1", "p1", "s1") }));
System.out.println("Initial List : " + mainList);
Set<IssueFeed> list_of_non_dupes = new LinkedHashSet<IssueFeed>();
List<IssueFeed> duplicateDatalist = new ArrayList<IssueFeed>();
for(IssueFeed feed : mainList){
if(!list_of_non_dupes.add(feed)) {
duplicateDatalist.add(feed);
}
}
mainList = new ArrayList<IssueFeed>(list_of_non_dupes); // Remove the duplicate items from the main list, leaving at least 1 item with that symptom to be display
list_of_non_dupes.removeAll(duplicateDatalist); // list of only objects with unique symptom
System.out.println("Fina main list : " + mainList);
System.out.println("Unique symptom" + list_of_non_dupes);
System.out.println("Duplicate symptom" + duplicateDatalist);
}
}

You should iterate through the ArrayList twice. Using this approach, you don't even need to sort the ArrayList by duplicates (Collections.sort is an O(n log n) operation) and can process the list in linear time. You also don't need to override equals() and hashCode() for IssueFeed objects.
In the first iteration, you should fill a HashMap of the symptoms hashed against the number of occurrences in the ArrayList. It would probably be
class SymptomInfo {
int incidence;
boolean used;
}
HashMap<String, SymptomInfo> symptomIncidence = new HashMap<String, SymptomInfo>();
However, you may want to use a thread-safe HashMap data structure if you are reading and writing to the HashMap from multiple threads.
In the second iteration through the ArrayList, you should look up the incidence value from the hashmap and find the total number of occurrences of that symptom. This is a quick and easy way to find out whether the object should be added to duplicateDataList or list_of_non_dupes. Also, the first time you encounter an object with a particular symptom value, you can set used to true. So, if you encounter an object where used is true, you know it is a duplicate occurrence and can remove it from the main list.

Related

Remove duplicates from an arraylist with strings

I have an arraylist that looks like this:
public static ArrayList<ArrayList<String[]>> x = new ArrayList<>();
I store groups of 2 persons in a pair. For example:
[Person1, Person2]
[Person3, Person4]
The algorithm I use right now still makes duplicates, I've tried out hashmaps and iterating through them with for loop but they just give me back the original list.
This is the code:
package com.company;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class createGroups
{
public static ArrayList<ArrayList<String[]>> x = new ArrayList<>();
public static void main(String[] args){
//Define names
String[] names = {"Person1", "Person2", "Person3", "Person4"};
try
{
//Create combinations. In a try catch because of the saveFile method.
combination(names, 0, 2);
//Print all the pairs in the Arraylist x
printPairs();
} catch (IOException e)
{
e.printStackTrace();
}
}
static void combination(String[] data, int offset, int group_size) throws IOException
{
if(offset >= data.length)
{
//Create new Arraylist called foo
ArrayList<String[]> foo = new ArrayList<>();
//Create a pair of 2 (data.length = 4 / group_size = 2)
for(int i = 0; i < data.length / group_size; i++)
{
//Add the pair to foo.
foo.add(Arrays.copyOfRange(data, 2 * i, 2 * (i + 1)));
}
//Add foo to x
x.add(foo);
//saveFile(foo);
}
for(int i = offset; i < data.length; i++){
for(int j = i + 1; j < data.length; j++){
swap(data, offset, i);
swap(data, offset + 1, j);
combination(data, offset + group_size, group_size);
swap(data, offset + 1, j);
swap(data, offset, i);
}
}
}
public static void printPairs(){
//Print all pairs
for(ArrayList<String[]> q : x){
for(String[] s : q){
System.out.println(Arrays.toString(s));
}
System.out.println("\n");
}
}
private static void swap(String[] data, int a, int b){
//swap the data around.
String t = data[a];
data[a] = data[b];
data[b] = t;
}
}
The output right now is this:
Output
Every group of 4 names is a 'list' of pairs (Not really a list but that's what I call it)
And this is the desired output:
Desired output
But then you can see that the first and the last list of pairs are basically the same how do I change that in my combination method
The question:
How can I change my combination method so that it doesn't create duplicate groups.
And how can I make the list smaller (The desired output) when printing the created lists.
If I wasn't clear enough or if I didn't explain what I want very well, let me know. I'll try to make it clearer.
Create an object similar to this. It takes 4 strings (2 pairs). Puts the strings into array and sorts this array. That means any combination of strings you put in will be converted into one sorted combination, but the object internaly remembers which person is person1, person2, ...
private class TwoPairs {
private final String person1;
private final String person2;
private final String person3;
private final String person4;
private final String[] persons;
TwoPairs(String person1, String person2, String person3, String person4) {
this.person1 = person1;
this.person2 = person2;
this.person3 = person3;
this.person4 = person4;
persons = new String[4];
persons[0] = person1;
persons[1] = person2;
persons[2] = person3;
persons[3] = person4;
// if we sort array of persons it will convert
// any input combination into single (sorted) combination
Arrays.sort(persons); // sort on 4 objects should be fast
// hashCode and equals will be comparing this sorted array
// and ignore the actual order of inputs
}
// compute hashcode from sorted array
#Override
public int hashCode() {
return Arrays.hashCode(persons);
}
// objects with equal persons arrays are considered equal
#Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
TwoPairs other = (TwoPairs) obj;
if (!Arrays.equals(persons, other.persons)) return false;
return true;
}
// add methods which you might need
// getters for individual persons
// String getPerson1() { return person1; }
// or perhaps pairs of persons
// String[] getPair1() { return new String[] {person1, person2}; }
// add sensible toString method if you need it
}
Your ArrayList x will change like this
ArrayList<TwoPairs> x = new ArrayList<TwoPairs>();
before adding new TwoPairs object into x check if this list already contains this object.
if (!x.contains(twoPairsObject)) {
x.add(twoPairsObject);
}

CompareTo incorrect return value

First and Foremost, I must mention after reading lots of questions and tutorials and watching some videos, still the problem is not resolved.
I am an intermediate programmer in Java, and I have written some codes for comparing elements in a priority queue, in which elements are kept like [Comparable element, int priority]. Obviously, the higher priority would be popped.
the problem is:
I modified the code to have the first element with highest priority, and it does NOT work! I have narrowed it down that when comparing the first 2 elements, the compareTo method returns 0, which it should NOT, consequently the code fails!
this is the class that has the push of O(1) and Pop of O(n):
public class PriorityQueueU<T>
{
public class PriorityPair implements Comparable
{
public Comparable element;
public Comparable priority;
public PriorityPair(Comparable element, int priority){
this.element = element;
this.priority = priority;
}
public Comparable<T> getElemet(){
return this.element;
}
public Comparable<T> getPriority(){
return this.priority;
}
public int compareTo(Comparable a)
{
PriorityPair p2 = (PriorityPair)a;
return ((Comparable)priority).compareTo(p2.priority);
}
public String toString(){
String s1 = this.element.toString();
String s2 = this.priority.toString();
String res = "[ " + s1 + ", " + s2 + " ]" ;
return res;
}
public int compareTo(Object o)
{
// TODO Auto-generated method stub
return 0;
}
}
private LinkedList data;
public PriorityQueueU()
{
data = new LinkedList();
}
public void pushUnsorted(Comparable<T> o, int priority)
{
PriorityPair paired = new PriorityPair(o, priority);
data.addLast(paired);
}
public Comparable popUnsorted()
{
int index = 0;
for (int i = 0; i < this.data.size() - 1; i++)
{
if (((PriorityPair) this.data.get(i)).compareTo(this.data.get(i + 1)) < 0)
{
index = i + 1;
}
}
PriorityPair pp = (PriorityQueueU<T>.PriorityPair) this.data.get(index);
this.data.deleteIt(index);
return pp.getElemet();
}
public String toString(){
return this.data.toString();
}
}
}
and this is the code that would test the behavior:
PriorityQueueU<T> unSortedPQ = new PriorityQueueU<>();
unSortedPQ.pushUnsorted( (Comparable<T>) "a", 1000);
unSortedPQ.pushUnsorted((Comparable<T>) "b", 200);
unSortedPQ.pushUnsorted((Comparable<T>) "j", 900);
unSortedPQ.pushUnsorted((Comparable<T>) "r", 9);
unSortedPQ.pushUnsorted((Comparable<T>) "z", 6);
System.out.println("the UNsorted priority Q: ");
System.out.println(unSortedPQ);
System.out.println("*#------------------END OF PUSH-----------------#*");
System.out.println();
System.out.println("the priority Q: " + unSortedPQ);
System.out.println("popped item is: " + unSortedPQ.popUnsorted());
// System.out.println("top of the priority queue is: " + uPriorityQueueU.top());
System.out.println();
System.out.println("the UNsorted priority Q: ");
System.out.println(unSortedPQ);
System.out.println("*#------------------END OF POP-----------------#*");
System.out.println();
MANY thanks in advance.
P.S. bear in mind that maybe I was wrong and the problem was somewhere else!
current behavior: it pops j, which is the second highest element in the list. I already know that when popping, the element must be deleted as well from the priority queue, which is stored in a linked list. I made sure deletion in linked list is correctly working, but don't hesitate to ask for the code if necessary.
In order for compareTo to work, you should implement the method provided by the Comparable interface.
public interface Comparable<T> {
public int compareTo(T o);
}
As we see from the code above, Copmarable supports generics. If you do not specify what type of objects you want to compare (leave it as it is), it will default to Object, which it does (that's from your code):
public int compareTo(Object o)
{
// TODO Auto-generated method stub
return 0;
}
So you either use generics or write logic for compareTo(Object o).

Using a method to sort a Singly Linked List in Java

Implementing a Linked List, store up to 10 names, ordered in first in First Out. Then implement two methods, one of which to sort it alphabetically by last names. This is where I am having some trouble. Here is what I tried:
Recursion. The method calls two nodes, compare them, swap if needed and then calls itself. Doesn't work with odd number of names and tends to be full bugs.
Collection but I don't know enough about it to use it effectively.
Sorting algorithms (ex. bubble sort): I can go though the list but have a hard time getting the nodes to swap.
My question is: What is the easiest way to do this?
public class List
{
public class Link
{
public String firstName;
public String middleName;
public String lastName;
public Link next = null;
Link(String f, String m, String l)
{
firstName = f;
middleName = m;
lastName = l;
}
}
private Link first_;
private Link last_;
List()
{
first_ = null;
last_ = null;
}
public boolean isEmpty()
{
return first_ == null;
}
public void insertFront(String f, String m, String l)
{
Link name = new Link(f, m, l);
if (first_ == null)
{
first_ = name;
last_ = name;
}
else
{
last_.next = name;
last_ = last_.next;
}
}
public String removeFront()
{
String f = first_.firstName;
String m = first_.middleName;
String l = first_.lastName;
first_ = first_.next;
return f + " " + m + " " + l;
}
public String findMiddle(String f, String l)
{
Link current = first_;
while (current != null && current.firstName.compareTo(f) != 0 && current.lastName.compareTo(l) != 0)
{
current = current.next;
}
if (current == null)
{
return "Not in list";
}
return "That person's middle name is " + current.middleName;
}
}
public class NamesOfFriends
{
public static void main(String[] args)
{
List listOfnames = new List();
Scanner in = new Scanner(System.in);
for(int i = 0; i < 3; i++)
{
if(i == 0)
{
System.out.println("Please enter the first, middle and last name?");
listOfnames.insertFront(in.next(), in.next(),in.next());
}
else
{
System.out.println("Please enter the next first, middle and last name");
listOfnames.insertFront(in.next(), in.next(),in.next());
}
}
System.out.println("To find the middle name, please enter the first and last name of the person.");
System.out.println(listOfnames.findMiddle(in.next(),in.next()));
}
}
Edit
After working on it a bit, I figured out how to go about sorting it. For that purpose, I am trying to implement a remove method that can remove a node anywhere on the list. While it does compile, it doesn't do anything when I run the program.
public Link remove(String lastName)
{
Link current_ = first_;
Link prior_ = null;
Link temp_ = null;
while (current_ != null && current_.lastName.compareTo(lastName) != 0)
{
prior_ = current_;
current_ = current_.next;
}
if (current_ != null)
{
if (current_ == last_)
{
temp_ = last_;
last_ = prior_;
}
else if (prior_ == null)
{
temp_ = first_;
first_ = first_.next;
}
}
else
{
temp_ = current_;
prior_.next = current_.next;
}
return temp_;
}
2: Collections is the easiest, but it seems to be not allowed in your homework
3: BubbleSort is easy but the worst known sorting algo, however for your homework it probably is ok
1: This is the same as bubble sort, but is prefered to be done without recursion
In BubbleSort you loop through your elements again and again till no swaps are neeeded anymore, then you are ready.
Collection is the easiest way to accomplish this.
Implement Comparable
Override hashcode and equals
Collection.sort()
You already has the linked list implemented, that is good.
Have you considered implementing MergeSort as the sorting algorithm? Being the divide&conquer algorithm, you will always end up with only two elements to form a list with.
The merge part is going to be trickier, but also easy. Basically you just create a new list and start filling it up with elements you get by comparing the first values of the two merging sets.
So for instance if you have two sets to merge:
[A]->[C]->[D]
[B]->[E]->[F]
the mergin process will go:
[A]
[C]->[D]
[B]->[E]->[F]
[A]->[B]
[C]->[D]
[E]->[F]
[A]->[B]->[C]
[D]
[E]->[F]
[A]->[B]->[C]->[D]
[E]->[F]
[A]->[B]->[C]->[D]->[E]
[F]
[A]->[B]->[C]->[D]->[E]->[F]

Null ArrayList returned by TreeMap Java

I have to do a synonyms dictionary using a TreeMap. The TreeMap is of <Word, ArrayList<Word>> type. That means that for each key represented by a Word there will be a list of synonyms. When I want to list the contents of the dictionary, by using the method below, I discover that the ArrayList returned is null. What can I do? I tried tracing the code but I don't seem to find the error.
The method is :
public String listContent() {
Set set = wordList.keySet();
Iterator it = set.iterator();
String result = new String();
ArrayList<Word> words = new ArrayList<Word>();
while (it.hasNext()) {
Word temp = (Word) it.next();
words = wordList.get(temp);
if (words != null) {
Iterator it2 = words.iterator();
result += temp.getContent();
result += " - ";
int size = words.size();
while (it2.hasNext()) {
result += ((Word) it2.next()).getContent();
if (size != 1)
result += ", ";
size--;
}
result += "\n";
}
}
return result;
}
The ArrayList returned by wordList.get(temp) is null for some of the inserted elements. I checked the watches but there, they're not. What should I do ?
wordList is a TreeMap<Word, ArrayList<Word>>;
EDIT - the addWord method
public void addWord(String content1, String content2)
{
Word w1 = new Word(content1);
Word w2 = new Word(content2);
Set set = wordList.entrySet();
Iterator it = set.iterator();
boolean ok=false;
while(it.hasNext())
{
Map.Entry<Word,ArrayList<Word>> temp = (Map.Entry<Word,ArrayList<Word>>) it.next();
if(temp.getKey().getContent().matches(content1))
{
ArrayList<Word> words = temp.getValue();
Iterator it2 = words.iterator();
if(words.isEmpty()) words.add(w2);
else
{
boolean ok2=true;
while(it2.hasNext())
{
Word tempy = (Word) it2.next();
if(tempy.getContent().equals(content2))
{
ok2=false;
break;
}
}
if(ok2) words.add(w2);
}
ok=true;
}
}
if(!ok) {
ArrayList<Word> tempys = new ArrayList<Word>();
tempys.add(w2);
wordList.put(w1,tempys);
}
}
EDIT 2 - Word Class
public class Word implements Serializable,Comparable {
private String content;
public Word (String content)
{
this.content = content;
}
public void setContent(String content)
{
this.content=content;
}
public String getContent()
{
return content;
}
#Override
public int compareTo(Object o) {
if(((Word)o).getContent().equals(this.getContent())) return 0;
return 1;
}
}
Your compareTo method is wrong. The contract is that if A > B, then you must have B < A. Your implementation always returns 1 if the contents are not equal.
You should implement it like this:
#Override
public int compareTo(Word w) {
return this.content.compareTo(w.content);
}
(and the Word class should implement Comparable<Word>, not Comparable).
Since a TreeMap uses this method to tell if some word is bigger or smaller than another one, and since the method returns incoherent results, the Map also returns incoherent results.
Did you check that when you insert a synonym everything is ok?
BTW you should use StringBuilder for concatenating strings (better in perf) and you'd better use worklist.entrySet() for iterating on key and value at the same time, instead of several get and iterators.
The addWord method is a horrible mess and I'm getting a headache when I try to look at it, but my educated guess is that the system does not work because the Word class implements neither the equals method nor the hashCode method. Try adding these to it:
#Override
public int hashCode() {
return this.content.hashCode();
}
#Override
public boolean equals(Object o) {
return this.content.equals(o);
}
With those methods the TreeMap and the other structures are able to identify that two instances of Word classes that represent the same word are, indeed, equal.
I've cleaned up your existing code to use proper Java idioms, like for-each loop, StringBuilder instead of concatenating a String, avoid that size-- hack, stuff like that.
public String listContent() {
final StringBuilder result = new StringBuilder();
for (Map.Entry<Word, List<Word>> e : wordList.entrySet()) {
final List<Word> words = e.getValue();
if (words != null) {
result.append(e.getKey().getContent()).append(" - ");
final Iterator<Word> it = words.iterator();
result.append(it.next().getContent());
while(it.hasNext()) result.append(", ").append(it.next().getContent());
result.append("\n");
}
}
return result.toString();
}
Here's also a cleaned-up version of addWord, but still a heavy mess of program logic. If anyone has patience for this, I encourage him to steal and improve on this.
public void addWord(String content1, String content2) {
final Word w1 = new Word(content1), w2 = new Word(content2);
final Set<Map.Entry<Word, List<Word>>> set = wordList.entrySet();
for (Map.Entry<Word, List<Word>> temp : set) {
if (!temp.getKey().getContent().matches(content1)) {
final List<Word> newList = new ArrayList<Word>();
newList.add(w2);
wordList.put(w1,newList);
break;
}
final List<Word> words = temp.getValue();
if (words.isEmpty()) words.add(w2);
else {
for (Word w : words) {
if (w.getContent().equals(content2)) {
words.add(w2);
break;
}
}
}
}
}

Implement reading of contact from phone book Using Hashtable in J2ME

I ran into a bind whereby I had to sort the data read from the phones PIM. In doing this I lost the other to which each contact field was referenced to the telephone number because I made use of 2 separate vectors as illustrated below
Before sorting
Nna - +445535533
Ex - +373773737
Ab - +234575757
After sorting.(Which shouldn't be)
Ab - +445535533
Ex - +373773737
Nna - +234575757
This gives an undesired behavior since the sort removes the index to index pointer of the vectors and a selected name (in a Multiple list Box) will get a wrong number.
Alternatively,
I used a hashtable, with the intention of using the names as keys and numbers as the values.
But this pairing means duplicate names being used as keys will not be allowed. Thus I made it a i.e the phone number as keys instead.
I don't want to sound like a cry baby so I stop here for a while and so you the code with a hope u guys would understand it
MY QUESTION
1. Is there a better way/algorithm to implement this?
2. How do I implement the getSelectedItems() in such a ways that it grabs the numbers of the selected indexes of a MULTIPLE CHOICE LIST from a hashTable
import java.util.Enumeration;
import java.util.Vector;
import java.util.Hashtable;
import javax.microedition.lcdui.List;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMException;
/**
*
* #author nnanna
*/
public class LoadContacts implements Operation {
private boolean available;
private Vector telNames = new Vector();
Vector telNumbers = new Vector();
Hashtable Listcontact = new Hashtable();
private String[] names;
public Vector getTelNames() {
return telNames;
}
public Hashtable getListcontact() {
return Listcontact;
}
public void execute() {
try {
// go through all the lists
String[] allContactLists = PIM.getInstance().listPIMLists(PIM.CONTACT_LIST);
if (allContactLists.length != 0) {
for (int i = 0; i < allContactLists.length; i++) {
System.out.println(allContactLists[i]);
System.out.println(allContactLists.length);
loadNames(allContactLists[i]);
System.out.println("Execute()");
}
} else {
available = false;
}
} catch (PIMException e) {
available = false;
} catch (SecurityException e) {
available = false;
}
}
private void loadNames(String name) throws PIMException, SecurityException {
ContactList contactList = null;
try {
contactList = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY, name);
// First check that the fields we are interested in are supported(MODULARIZE)
if (contactList.isSupportedField(Contact.FORMATTED_NAME) && contactList.isSupportedField(Contact.TEL)) {
Enumeration items = contactList.items();
Hashtable temp = new Hashtable();
while (items.hasMoreElements()) {
Contact contact = (Contact) items.nextElement();
int telCount = contact.countValues(Contact.TEL);
int nameCount = contact.countValues(Contact.FORMATTED_NAME);
if (telCount > 0 && nameCount > 0) {
String contactName = contact.getString(Contact.FORMATTED_NAME, 0);
// go through all the phone availableContacts
for (int i = 0; i < telCount; i++) {
System.out.println("Read Telno");
int telAttributes = contact.getAttributes(Contact.TEL, i);
String telNumber = contact.getString(Contact.TEL, i);
Listcontact.put(telNumber, contactName);
temp.put(contactName, telNumber);
}
names = getSortedList();
// Listcontact = temp;
System.out.println(temp + "-------");
System.out.println(Listcontact + "*******");
shortenName(contactName, 20);
}
available = true;
}
} else {
available = false;
}
} finally {
// always close it
if (contactList != null) {
contactList.close();
}
}
}
private void shortenName(String name, int length) {
if (name.length() > length) {
name = name.substring(0, 17) + "...";
}
}
public Vector getSelectedItems(List lbx) {
boolean[] arrSel = new boolean[lbx.size()];
Vector selectedNumbers = new Vector();
int selected = lbx.getSelectedFlags(arrSel);
String selectedString;
String result = "";
for (int i = 0; i < arrSel.length; i++) {
if (arrSel[i]) {
selectedString = lbx.getString(lbx.getSelectedFlags(arrSel));
result = result + " " + i;
System.out.println(Listcontact.get(selectedString));
// System.out.println(telNumbers.elementAt(i));
}
}
return selectedNumbers;
}
private String[] sortResults(String data[]) {
RecordSorter sorter = new RecordSorter();
boolean changed = true;
while (changed) {
changed = false;
for (int j = 0; j < (data.length - 1); j++) {
String a = data[j], b = data[j + 1];
if (a != null && b != null) {
int order = sorter.compare(a.getBytes(), b.getBytes());
if (order == RecordSorter.FOLLOWS) {
changed = true;
data[j] = b;
data[j + 1] = a;
}
}
}
}
return data;
}
public String[] getNames() {
return names;
}
Vector elements = new Vector();
private String[] getValueArray(Hashtable value) {
System.out.println(Listcontact + " c");
Enumeration e = value.elements();
while (e.hasMoreElements()) {
elements.addElement(e.nextElement());
}
String[] elementsArray = new String[elements.size()];
elements.copyInto(elementsArray);
elements.removeAllElements();
System.out.println(elementsArray + " k");
return elementsArray;
}
public void getDuplicates(Vector realValue) {
Vector duplicate = new Vector();
Enumeration e = realValue.elements();
for (int i = 0; e.hasMoreElements(); i++) {
if (duplicate.isEmpty() || !duplicate.elementAt(i).equals(e.nextElement())) {
break;
} else {
duplicate.addElement(e.nextElement());
}
}
}
public String[] getSortedList() {
return sortResults(getValueArray(Listcontact));
}
}
Let me reiterate you requirement: You want a method that will sort the contacts read from native phonebook, then alphabetically sort them on name.
Following is the approach,
Replace the vectors and hash-tables in your code with a single vector, say contactListVector, containing elements of type ContactItem, don't worry this class is explained below. Fundamentally the contact's name and number(s) are linked together in a ContactItem, hence you do not have to worry about there mappings which reduces the usage of redundant data structures.
class ContactItem {
private String name;
private String tnumber; //this can also be a data structure
//for storing multiple numbers
ContactItem( String name, String tnumber) {
this.name = name;
this.tnumber = tnumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTnumber() {
return tnumber;
}
public void setTnumber(String tnumber) {
this.tnumber = tnumber;
}
}
You can reuse the sorting algorithm on contactListVector by comparing the member variable ContactItem.name of the vector element. Also you can deploy different sorts on member variables numbers and/or names. Also there are lots of libraries for JavaME available that have better sorting algorithm's implemented if need be use them.
I would recommend you to perform the sorting once on the contactListVector elements at the end of your method loadNames(...) maybe in the finally block triggered by some boolean variable. The current sorting call in each iteration on items enumeration is expensive and time consuming.
Also you can serialize / deserialize the ContactItem thus persist your contact list.
Let me know if you need detailed explanation.
What about inserting the contact name and numbers inside a recordStore , so you can later make a sort by creating a class which implements RecordComparator.
This statement in your code makes no sense:
selectedString = lbx.getString(lbx.getSelectedFlags(arrSel))
Per lcdui List API documentation above will return the string located at the index equal to the number of selected elements why would you need that?
If you need to output selected text for debugging purposes, use lbx.getString(i) instead.
To implement the getSelectedItems() in such a ways that it grabs the numbers of the selected indexes of a MULTIPLE CHOICE LIST do about as follows:
public Vector getSelectedItems(List lbx) {
boolean[] arrSel = new boolean[lbx.size()];
Vector selectedNumbers = new Vector();
int selected = lbx.getSelectedFlags(arrSel);
System.out.println("selected: [" + selected + "] elements in list");
String selectedString;
String result = "";
for (int i = 0; i < arrSel.length; i++) {
if (arrSel[i]) {
// here, i is the selected index
selectedNumbers.addElement(new Integer(i)); // add i to result
String selectedString = lbx.getString(i);
System.out.println("selected [" + selectedString
+ "] text at index: [" + i + "]");
}
}
return selectedNumbers;
}
As for sorting needs, just drop the HashTable and use Vector of properly designed objects instead as suggested in another answer - with your own sorting algorithm or one from some 3rd party J2ME library.
I would suggest you to have Contact class with name and Vector of numbers. And instead of sorting names array sort the array of contacts.

Categories