简体   繁体   中英

Java HashSet preserving first and last element

I'm trying to remove duplication from a list of list of characters using java.

Example:

[a, c, b] ---> List of characters 1

[a, c, e, b] ---> List of characters 2

[a, c, a , c , e, b] ---> List of characters 3

For the first and the second list because we do not have a duplication so need to modify them, but for the 3rd list we do have a duplication so i need to remove the duplication without touching the first and the last element of the list so the final result will be [a, c, e, b] .

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;


public class Execution {

    public static void main(String[] args) {
        execution();

    }

    private static <V> void execution() {
        Graph<Character> graph = new Graph<>(
                new Edge<>('a', 'c', 1.0),
                new Edge<>('c', 'a', 1.0),

                new Edge<>('b', 'c', 2.0),
                new Edge<>('c', 'b', 2.0),

                new Edge<>('c', 'e', 1.0),
                new Edge<>('e', 'c', 1.0),

                new Edge<>('b', 'e', 1.0),
                new Edge<>('e', 'b', 1.0),

                new Edge<>('e', 'd', 3.0),
                new Edge<>('d', 'e', 3.0),

                new Edge<>('d', 'b', 2.0),
                new Edge<>('b', 'd', 2.0)

        );

        List<Path<Character>> paths = new DefaultKShortestPathFinder<Character>().findShortestPaths('a', 'b', graph, 3);
        List<List<Character>> nodes = new ArrayList<List<Character>>();
        List<HashSet<Character>> modified = new ArrayList<HashSet<Character>>();

        for(Path<Character> path:paths) {
            nodes.add(path.getNodeList());

        }


        for(List<Character> p:nodes) {
          modified.add(new HashSet<>(p));

        }

        for(HashSet<Character> n:modified) {
          System.out.println(n);

        }


    }

}

Output of my code:

[a, b, c] [a, b, c, e] [a, b, c, e]

I want to remove the duplication but when i use the HashSet it removes my first and last element

HashSet doesn't remove the first or last element. HashSet prevents duplicates and has no ordering, so there is no meaning to the first or last element in a HashSet .

If I understood the question, you want to remove duplicates while preserving the order of the elements of the original List s. Use LinkedHashSet (which preserves insertion order):

modified.add(new LinkedHashSet<>(p));

Actually, that would only take care of keeping the first element in the first position, so if the last element of the original List has multiple occurrences, it won't stay in the last position (since the last position of the List would contain a character already added to the Set ). You'll have to remove and re-add it to the Set after creating the modified.add(new LinkedHashSet<>(p)) call.

for(List<Character> p:nodes) {
    LinkedHashSet<Character> set = new LinkedHashSet<>(p);
    set.remove(p.get(p.size()-1));
    set.add(p.get(p.size()-1));
    modified.add(set);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM