I need 2 denominational array sorted [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am working on a project that has a list of student name and numbers, for example
James Bloggs,1
Paul Jonson,43
Andt Peters,23
Once I have them in an array I then need them sorted.
What is the best way of going about this. Its not the sort Im stuck on its the referencing the names to the numbers. I would have thought if I do a 2 denominational array only one would be sorted.
Any help would be great,

EDIT: I just realized this question was asking about a 2-dimensional array and my answer doesn't directly deal with that. I am skeptical that arrays should be involved at all. Arrays are usually for dealing with primitive data, and maybe if you are coming from a C background you'd think they'd be the natural thing to use. If you really honestly have to use arrays then this probably isn't the way to go.
https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
public void foo(){
// Use a TreeMap. It will sort keys on insertion.
Map<Integer,String> nameByNumber = new TreeMap<>();
nameByNumber.put(1, "James Boggs");
// etc. put all the entries in however you need to
List<Integer> sortedNumbers = personByNumber.getKeys();
List<String> namesSortedByNumber = personByNumber.getNames();
}
If you need it to be more organized and complex, you can encapsulate the name and number into a Class with a name and number property. Then you'd still use the number as the key, but you'd have the full class as the value. Do this if you need to have more than just a name, like last name, first name, address, etc.

Related

Why the use of open bracket and close bracket to declare a variable in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Can someone please explain the use of the following code in Java
private TableColumn<Books, Integer> colId;
I want to know the reason of using <?,?> to declare a variable in Java.
Those are 'Generics'. Specifically, the things inside the brackets are called 'Type Parameters'. It allows different types to be chosen when you create the object or 'variable' as you call it.
So you could have a list of numbers with: new ArrayList<Integer>()
or a list of strings with: new ArrayList<String>()
https://www.tutorialspoint.com/java/java_generics.htm
In your example, it's creating a column to keep track of ids for books. The ids will be ints and the object they are identifying is books. But, you may want another TableColumn (which behaves the same as the Book Id column) but for tracking the titles of the books: TableColumn<Books, String> colTitle

Is it a good practice to write code like List<String[]> in java? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Is it a good practice to write code like List<String[]> in java? Would there be a problem?
If not. Which way is better: List<List<String>> or List<String[]> . And why?
Edit:
the question may seems stupid. Today one of my colleague asks me this question, and I realized that I seldom see code like List<String[]> in great java projects. I just want to know why.
It completely depends on your requirement.
If you want to make a list of String array where number of string present in each array is fixed and you know the number beforehand, then you can go with List <String[]>
But if you doesn't have any idea regarding the number of string present in each list inside the list beforehand and the number of string will be decided in run time, you have to go with List<List<String>>
Thinks it will help you.
There won't be a problem if you write List<String[]> and use it correctly.
Let me explain their differences:
List<String[]> is a List of an array of String
List<List<String>> is a List of a List of String
I prefer List<List<String>> over the other since it's easier to work with List<String> than String[]. After all, List was created to solve the usability problems with arrays. List allows you to easily get, put, delete, and do all sort of other operations possible with an array, thanks to its ready-to-use functions (check https://docs.oracle.com/javase/8/docs/api/java/util/List.html).

What are the pros and cons of using Stream sorting over Collections sorting in Java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Lets say I have a pojo Model as:
class Model{
String id;
String name;
}
List in Java, and I want to sort an already filled list List models.
For now, I'm considering two options:
Using Colletions.List :
models.sort(.sort(Comparator.comparing(Model::getId)))
Using sorted function of Java8 Stream API:
models..stream().sorted(Comparator.comparing(Model::getId)).collect(Collectors.toList())
Can anyone please explain pros and cons of using Method 2 over Method 1?
I believe the biggest difference is that if you use list.sort() it actually sorts the list. If you use list.stream().sorted() that returns a sorted list but doesn't actually sort the list you start from. There might be cases for both - depending on what you prefer.

get subset of elements from set in circular way [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Consider a set with like 100 elements.
Set<String> originalSet; //[1....100] size is 100
From originalSet, (m) subset of elements of some size(n) with some starting index(i) have to retrieved.
Example:
m = 4, n = 45, i = 1
Following have to be retrieved
subset1[1-45], subset2[46-90], subset3[91-35], subset4[36-80]
Whats the best way of doing this.
First of all, Set is unordered so it does not make sense to talk about indices etc. List would make more sense here.
Next, you have to be explicit about what you mean by "best". Performance on insertion? Random access? Creation of your n-from-i subset? These are important question to choose the implementation.
I think two primary options would be linked list with special handling of the last element in subList operation or an array-based list.
Assuming your set has some notion of order, you could write this with
Iterable<Iterable<String>> slices =
Iterables.limit(
Iterables.partition(
Iterables.skip(
Iterables‌​.cycle(originalSet),
i),
n),
m);
If you wanted a set out of this, you'd have to do a transform or something; if you have Java 8 that'd just be something like Iterables.transform(..., ImmutableSet::copyOf).

Sort ArrayList data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm preparing for an exam. I can read the CSV file but I don't know how to sort a data line.
For example, if "SchoolName.csv" has
Cayuga,Elkhart,Slocum,Westwood
Neches,Palestine,central
Dibolon
Lufkin,Holiday
After I sort the data the output should be
Dibolon // becuase it only contain one name.
Lufkin,Holiday
Neches,Palestine,central
Cayuga,Elkhart,Slocum,Westwood
So you want to sort based on number of elements. Assuming you have those elements in a Collection<String> csvlines:
csvlines.stream()
.sorted(Comparator.comparing(line -> StringUtils.countMatches(line, ",")))
.collect(Collectors.toList())
If you want to sort each row based on the number of names, you could do it like this
// Read the file's lines
List<String> lines = Files.readAllLines(pathToFile);
List<String> sortedLines = lines.stream().sorted(Comparator.comparing(line -> line.split(",").length)).collect(Collectors.toList());
Define a comparator, That is a class extending comparator and pass it as an argument to arraylistname.sort(comparator);
A comparator returns a negative integer, zero, or a positive integer if the first argument is less than, equal to, or greater than the second.
so
class CompareByLength implements Comparator {
compare(list a, list b) {
return a.size()-b.size();
}
}
alternatively use b.size()-a.size() if you want your list ordered the other way round.

Categories

Resources