-1

I have some elements in a linked hash set.Example
pencil,pen,eraser,scale
First I want to access (pencil,pen) and perform some operation. Then I want to access (pencil,eraser) then (pencil,scale) and so on.
I have written a code which can access 1st two items but in next iteration it accesses eraser,scale instead of pencil,eraser. Please help me to find out this solution. Thanks. This is a part of the code what i have done so far :

    LinkedHashSet<String> freq = new LinkedHashSet<String>();  
    /* Linked hashset has these items :  
    [pen,pencil,eraser,scale] */  
    String item;  
    String item2;  
    Iterator<String> itr = freq.iterator();  
    while (itr.hasNext()) {  
        item = itr.next();  
        System.out.println(item);  
        item2=itr.next();  
        System.out.println(item2);
        // perform some task  
    }
Marcel Bro
  • 4,907
  • 4
  • 43
  • 70
dayl fds
  • 11
  • 1

3 Answers3

2

I don't know if I understand what you want well, But the looping you want must be done using nested loop, consider this code:

public static void main(String[] args) {
    LinkedHashSet<String> freq = new LinkedHashSet<String>();
    freq.add("pen");
    freq.add("pencil");
    freq.add("eraser");
    freq.add("scale");

    for (String item : freq) {
        performeOperation(item, freq);
    }
}

private static void performeOperation(String primeItem, LinkedHashSet<String> freq) {
    for (String secondItem : freq) {
        if (primeItem != secondItem) {
            System.out.println(primeItem + ", " + secondItem);
        }
    }
}

Output sample:

pen, pencil
pen, eraser
pen, scale
pencil, pen
pencil, eraser
pencil, scale
eraser, pen
eraser, pencil
eraser, scale
scale, pen
scale, pencil
scale, eraser

If you don't need duplicate pairs, use this:

public static void main(String[] args) {
    LinkedHashSet<String> freq = new LinkedHashSet<String>();
    LinkedHashSet<String> param = new LinkedHashSet<String>();
    freq.add("pen");
    freq.add("pencil");
    freq.add("eraser");
    freq.add("scale");
    param.addAll(freq);

    for (String item : freq) {
        param.remove(item);
        performeOperation(item, param);
    }
}

private static void performeOperation(String primeItem, LinkedHashSet<String> param) {
    for (String secondItem : param) {
        System.out.println(primeItem + ", " + secondItem);
    }
}

Output sample:

pen, pencil
pen, eraser
pen, scale
pencil, eraser
pencil, scale
eraser, scale

Yes, it can be done using one LinkedHashSet, but you have to use the LinkedHashSet to maintains the insertion order of elements, so that this solution can work.

public static void main(String[] args) {
    LinkedHashSet<String> freq = new LinkedHashSet<String>();
    freq.add("pen");
    freq.add("pencil");
    freq.add("eraser");
    freq.add("scale");

    for (String item : freq) {
        performeOperation(item, freq);
    }
}

private static void performeOperation(String primeItem, LinkedHashSet<String> param) {
    boolean flag = false;
    for (String secondItem : param) {
        if (flag) {
            System.out.println(primeItem + ", " + secondItem);
        } else if (primeItem.equals(secondItem)) {
            flag = true;
        }
    }
}
Ebraheem Alrabeea
  • 2,130
  • 3
  • 23
  • 42
  • this you have done using 2 linkedhash set where both contains the same elements. But i want to do that using only one linkedhashset (print without duplicate pairs) how do i do that? – dayl fds Sep 08 '18 at 01:35
  • Alrabee, Thanks. the updated answer worked fine and that's what i wanted. Thank you so much. :) :) :) – dayl fds Sep 08 '18 at 03:27
0

Code:

    Iterator<String> iterator = set.iterator();
    String pencil = iterator.next();
    while (iterator.hasNext()) {
        System.out.println(pencil);
        System.out.println(iterator.next());
    }

Output:

pencil
pen
pencil
eraser
pencil
scale
Oleg Sokolov
  • 1,134
  • 1
  • 12
  • 19
  • I am assuming the full list that is required is {pencil, pen}, {pencil, eraser}, {pencil, scale}, {pen,eraser}, {pen,scale}, {eraser,scale}. It needs a nested iteration – Nishit Sep 04 '18 at 19:14
0

Not sure how to do it with ArrayLists in java, but you could convert it to an from an arraylist to a hashmap since you can count occurences using that method. here is what I would do.

import java.util.ArrayList;
    import java.util.List;
    import java.util.HashMap; 
    import java.util.Map; 
    import java.util.Map.Entry; 
    import java.util.*; 

    public class Sample
    {

         public static void main(String []args){
            ArrayList<String> arrayList = new ArrayList<>();
            arrayList.add("Pencil");
            arrayList.add("Pencil");
            arrayList.add("Bulb");
            arrayList.add("Scale");
            arrayList.add("Pencil");
            arrayList.add("Pencil");
            arrayList.add("Scale");
            arrayList.add("Eraser");

            countListItems(arrayList);

         }

        public static void countListItems(ArrayList<String> list) 
        { 
            Map<String, Integer> map = new HashMap<String, Integer>(); 

            for (String x : list) 
            { 
                Integer y = map.get(x); 
                map.put(x, (y == null) ? 1 : y + 1); 
            } 

            for (Entry<String, Integer> item : map.entrySet()) { 
                System.out.println(item.getKey()+" -> count="+ item.getValue()); 
            }
        } 
    }
Goku
  • 1,565
  • 1
  • 29
  • 62