This question already has answers here:
Sorting Java objects using multiple keys
(7 answers)
Closed 9 years ago.
I have an arraylist of movies that holds the movie title and director. I need to create a comparator that will first sort the directors in alphabetical order, and will sor the movies made by each director in alphabetical order. I also want to sort this data in ascending OR descending order.
e.g.
James Cameron:
Steven Spielberg
after you've sorted the director list....
just take one director as an key value in outer loop and then start comparing each and every movie name in inner loop...
How can I sort a List alphabetically?
this would be helpful...
do some searching next time....it is easy to find such solutions... :) ;)
Related
This question already has answers here:
How to calculate the intersection of two sets? [duplicate]
(2 answers)
Closed 2 years ago.
There are two collections :-
c1 = which include all female employee
c2= all employee whose age greater than 40(age>40).
How can i find all female employee whose age greater than 40?
c1.stream().filter(c2::contains).collect(Collectors.toList());//Last step is optional. Only use it, if you want a list of it.
stream(): Create a stream out of the first collection. (Java 8+)
Then filter() by those that are in c2,too.
And then optionally make a list from it.
This question already has answers here:
Java Class that implements Map and keeps insertion order?
(8 answers)
Closed 2 years ago.
I have a HashMap which is made up of parsed values from a textfile. Whenever I extract the contents from this HashMap, they come out in abc order. How do I keep the order that they were parsed, basically in a FIFO order.
Use a LinkedHashMap, which can preserve insertion order.
HashMap itself is not capable of preserving the order information. Have a look at other implementations, the standard library contains LinkedHashMap for example.
This question already has answers here:
Java Set retain order?
(13 answers)
Closed 3 years ago.
Is there a way to make the TreeSet sorted by the time of entering the keys not by the value itself
i mean if
TreeSet a= new TreeSet() ;
a.add("Zenda") ;
a.add("Apple") ;
then i printed the treeSet
i want to have "Zenda" then "Apple"
not sorted lexicographically .
You could use LinkedHashSet. That is a HashSet that keeps insertion order.
You can use LinkedList or ArrayList in java that will maintain the insertion order.
You can also print reverse order in LinkedList using ListIterator interface.
HashSet or LinkedHashSet does not allow duplicate values.
This question already has answers here:
When to use LinkedList over ArrayList in Java?
(33 answers)
Closed 5 years ago.
If you have to add or delete the elements at the beginning of a list, should you use ArrayList or LinkedList? And, if most of the operations on a list involve retrieving an element at a given index, should you use ArrayList or LinkedList? Why one over the other?
This sounds like a homework question. In that case, the expected answer is probably a linked list. Adding an element to the beginning of an array list requires moving all other elements. In a link list, you just add a new node at the beginning and that's it.
This question already has answers here:
How can I sort an ArrayList of Strings in Java?
(5 answers)
Closed 7 years ago.
I'm trying to write a method that receives an ArrayList of Strings and puts it into alphabetical order. Is it possible to use the sort method for strings and not ints?
This is different from the other question because the way he did it is much more complicated than I'm aiming for. I just wanted a line of code rather than a whole block.
I am not completely sure what are you trying to say by
not int s
But if you want to sort A arraylist of string alphabetically, this can be a way.
java.util.Collections.sort(arrayListOfString);